1

I want to select rows from my SQL Server table (it has 3 columns) and to display them into my Datagridview. My problem about null values. How can I pass null values? (Sometimes the first column and the second column are empty. ).

Below is my code:

    string cm = "SELECT column1, column2, column3 FROM mytablename WHERE column1='" 
   + sIS[mSI] + "' AND column2='" + sR[mSSI] + "' OR column1='" + sR[mSSI] 
   + "' AND column2='" + sIS[mSI] + "'";

Note: I edited my code.

heymycoder
  • 79
  • 1
  • 8

2 Answers2

2

You should be using parameters to pass values into a SQL statement or else you will be vulnerable to SQL injection attacks. You can create a SqlCommand object, create SqlParameter objects, then set values on them. If any of your values are null, then you can pass DBNull.Value as the parameter value.

SqlCommand cmd = con.CreateCommand();
string cm = "SELECT column1, column2, column3 "
cm += "FROM mytablename "
cm += "WHERE column1=@mSI "
cm += "AND column2=@mSSI OR column1=@mYSI AND column2=@mSI";
cmd.CommandText = cm;

for(int mSSI=0; mSSI<sR.Count(); mSSI++)
{
   cmd.Parameters.AddWithValue("@mSI ", sR[mSI]);

   // check for a null value
   if (sr[mSSI] == null)
   {
     cmd.Parameters.AddWithValue("@mSSI", DBNull.Value);
   }
   else
   { 
     cmd.Parameters.AddWithValue("@mSSI", sR[mSSI]);
   }

   cmd.Parameters.AddWithValue("@mYSI", sR[mYSI]);

   SqlDataAdapter sd = new SqlDataAdapter(cmd);
   DataTable dat = new DataTable();
   sd.Fill(dat);
   // clear parameters after each iteration
   cmd.Parameters.Clear()
}
Cam Bruce
  • 5,632
  • 19
  • 34
-1

I think the answer to your question is using "DBNull.Value" as mentioned above, something like this;

            string cm = "SELECT column1, column2, column3 FROM mytablename WHERE column1='"
           + DBNull.Value + "' AND column2='" + DBNull.Value + "' OR column1='" + sR[mSSI]
           + "' AND column2='" + DBNull.Value + "'";
  • You used only sR[mSSI], I will not select only for it. I need to select also sIS[mSI]. My command row is; string cm = "SELECT column1, column2, column3 FROM mytablename WHERE column1='" + sIS[mSI] + "' AND column2='" + sR[mSSI] + "' OR column1='" + sR[mSSI] + "' AND column2='" + sIS[mSI] + "'"; – heymycoder Jan 17 '19 at 19:34
  • I think there is a misunderstanding here; I gave that string as an example to show how you can pass "null" values in an SQL command. I was not checking or trying to fix your query. Anyways, a regular select query should bring empty values. I don't think you need a complex "WHERE" condition. – Orhun Karapinar Jan 17 '19 at 19:41
  • Sometimes the first column and the second column are empty. How can fix that null values in my "string cm = "SELECT column1, column2, column3 FROM mytablename WHERE column1='" + sIS[mSI] + "' AND column2='" + sR[mSSI] + "' OR column1='" + sR[mSSI] + "' AND column2='" + sIS[mSI] + "'";" row. I think I can't understand to your code, but thank you for answer. By the way, the answers must be complete. Because, I already don't know the correct answer and If you post missing code, I don't understand it sir. – heymycoder Jan 17 '19 at 19:50