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();
                            
                        }										
                   

                    }

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.