Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 code

foreach (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, "<fe />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, "<fe />NOC", Microsoft.VisualBasic.Strings.Format(int.Parse(dat), "0000").ToString(), 1, -1, Microsoft.VisualBasic.CompareMethod.Text);
                }
            }
            else
            {
                PCL = Microsoft.VisualBasic.Strings.Replace(PCL , "<fe />" + dg.Columns[i].HeaderText.ToString() + "", dat, 1, -1, Microsoft.VisualBasic.CompareMethod.Text);
            }
        }
    }
                
    PCL = Microsoft.VisualBasic.Strings.Replace(PCL, "<fe />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
Posted
Updated 30-Nov-11 22:42pm
v2
Comments
[no name] 1-Dec-11 4:44am    
EDIT: Added pre tags and indentation

1 solution

Hi,

I think you should make a function to trapped said error:

Example:

C#
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;
}


C#
IEnumerable<DataRow> dtRow = exDT.AsEnumerable();
var lstExcelData = dtRow.Select(s => new Claim
{
    Remarks = IifStr(s.Field<string>("REMARKS")),
    ClaimType = IifStr(s.Field<string>("TRANSTYPE"))
}).ToList();


Hope this could help...

Regards,
 
Share this answer
 
Comments
Michel [mjbohn] 1-Dec-11 6:15am    
You must use AND. if object is not DBNull it still can be null.
if (p != DBNull.Value && p != null)
Al Moje 1-Dec-11 20:52pm    
Hi,
Thanks for your correction.
Meant should be…

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900