Click here to Skip to main content
15,917,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi developers,
I am vishal sharma.
I load a csv file into a dataset, view it in datagridview. I am working on a replace method.

My problem is that my code for replaceing wildcards from datagridview is working properly but after executing the code an error message is show on screen by a message box

I think I'm not writing the code properly, so please write them properly.
My code is:

public void Button1_Click(object sender, EventArgs e)
{
  try
  {
    for(int i= 0;i<DGV.Rows.Count;i++)
    {
      foreach(DataGridViewCell cell in DGV.Rows[i].Cells)
      {
        cell.Value = cell.Value.ToString().Replace(comma.ToString(), slash.ToString());
        cell.Value = cell.Value.ToString().Replace(colon.ToString(), Blank.ToString());
      }
    }
  }
  catch(System.Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
}


The error that shows up after executing this code:

" object refrence not set to an instance of an object "

Please developers, solve this problem Immediately
I will be thankfull to you.
Posted
Updated 11-Nov-10 18:23pm
v2

Does this work?
maybe cell.Value is null and this throws your exception.

C#
public void Button1_Click(object sender, EventArgs e)
{
  try
  {
    for(int i= 0;i<DGV.Rows.Count;i++)
    {
      foreach(DataGridViewCell cell in DGV.Rows[i].Cells)
      {
        if(cell.Value == null)
          continue;

        cell.Value = cell.Value.ToString().Replace(comma.ToString(), slash.ToString());
        cell.Value = cell.Value.ToString().Replace(colon.ToString(), Blank.ToString());
      }
    }
  }
  catch(System.Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
}
 
Share this answer
 
v2
better put a breakpoint and check each line for null.
 
Share this answer
 

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