0

I have a data grid from which i got the values from a CSV file,im trying to validade each row of the grid before inserting it to my database.
I want to check if a specific row of the Grid contains null values or special character if not then insert that specific row.
My problem is my first if condition that is not returning any error possible

for(int i=0;i<=da.Tables["Stocks"].Rows.Count-1;i++)
{
  if (row is null)
   {
      MessageBox.Show("message of row containing wrong character or null");                            
        
   }
    else
        {
            //insert into database
          cmd.CommandText = "Insert  into Test values('" + da.Tables["data"].Rows[i].ItemArray.GetValue(0) + "','" + da.Tables["data"].Rows[i].ItemArray.GetValue(1) + "','" + da.Tables["data"].Rows[i].ItemArray.GetValue(2) + "')";
                            cmd.ExecuteNonQuery();
                            
                        }										
                   

                    }
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Momerath
0

First off, don't check <= and also subtract one from the value being checked. It's just < and no subtraction needed (line 1 of your code).

Second, you are checking if the row is null, not the items in the row. The row will never be null as that would mean the row doesn't exist. If it didn't exist, it wouldn't be in the Rows collection :)

You'll need to iterate through each of the items in the row and check if they are null, empty or have special characters.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.