Hi All
I am developing C# windows application where I have to read excel file and write to .ini file but one cell has null value which I value to put default 1. Every thing is ok the problem to put 1 in the place null value. If it is possible please help below I am giving codeforeach (DataGridViewRow r in dg.SelectedRows)
{
fs = new FileStream(fp, FileMode.Open, FileAccess.Read);
StreamReader rd = new StreamReader(fs);
rd.BaseStream.Seek(0, SeekOrigin.Begin);
PCL = rd.ReadToEnd().ToString();
rd.Close();
fs.Close();
string dat="";
for (int i = 0; i < r.Cells.Count; i++)
{
if (r.Cells[i].Value != null)
{
dat = r.Cells[i].Value.ToString();
if (dg.Columns[i].HeaderText == "NOC")
{
if (r.Cells[4].Value == "")
{
PCL = Microsoft.VisualBasic.Strings.Replace(PCL, "NOC", Microsoft.VisualBasic.Strings.Format(int.Parse(dat), "0000").ToString(), 1, -1, Microsoft.VisualBasic.CompareMethod.Text);// here I think I have to do some thing which make default value 1
}
else
{
PCL = Microsoft.VisualBasic.Strings.Replace(PCL, "NOC", Microsoft.VisualBasic.Strings.Format(int.Parse(dat), "0000").ToString(), 1, -1, Microsoft.VisualBasic.CompareMethod.Text);
}
}
else
{
PCL = Microsoft.VisualBasic.Strings.Replace(PCL , "" + dg.Columns[i].HeaderText.ToString() + "", dat, 1, -1, Microsoft.VisualBasic.CompareMethod.Text);
}
}
}
PCL = Microsoft.VisualBasic.Strings.Replace(PCL, "NOC","0001", 1, -1, Microsoft.VisualBasic.CompareMethod.Text);
if (r.Cells[0].Value != null)
{
PCL1 = PCL1 + PCL + CrLf;
PCL = "";
}
else
{
PCL = "";
}
}
Thanks & Regards
Indrajit Dasgupta
解决方案Hi,
I think you should make a function to trapped said error:
Example:
public static string IifStr(object p)
{
string retVal = string.Empty;
if (p != DBNull.Value || p != null)
{
retVal = Convert.ToString(p);
}
return retVal;
}
//Overload IifStr
public static string IifStr(string p)
{
string retVal = string.Empty;
if (!string.IsNullOrEmpty(p))
{
retVal = Convert.ToString(p);
}
return retVal;
}
IEnumerable dtRow = exDT.AsEnumerable();
var lstExcelData = dtRow.Select(s => new Claim
{
Remarks = IifStr(s.Field("REMARKS")),
ClaimType = IifStr(s.Field("TRANSTYPE"))
}).ToList();
Hope this could help...
Regards,