Dears, I'm designing a report in C# using windows form linked to SQL Server table. The result of the query is usually GP% numbers like : 30% or 40% and so on. These numbers are string by the way. I want to put the SQL Server result "30% for example in the array in c#. if no problem with the number it will be added simply like this :
conn.Open();
SqlCommand cmd43 = new SqlCommand(Product1ProfitPercentQuery, conn);
SqlDataReader rd43 = cmd43.ExecuteReader();
while (rd43.Read())
{
string Product1= rd43.GetString(0);
Products.Add(Product1);
}
rd43.Close();
conn.Close();
/But the problem is that some times the query's result is Null. The array will not accept to put Null result. I want to put a condition in the array reader as the following but its't working :/
conn.Open();
SqlCommand cmd43 = new SqlCommand(Product1ProfitPercentQuery, conn);
SqlDataReader rd43 = cmd43.ExecuteReader();
while (rd43.Read())
{
string Product1= rd43.GetString(0);
if (Product1=="Null")
{
Products.Add("0");
}
else if (Product1!= "Null")
{
Products.Add(Product1);
}
}
rd43.Close();
conn.Close();
How can i write this condition please. Thanks in advance.
You can use the HasRows
property of the SqlDataReader to see if the recordest was populated or null, and you could wrap your while
loop within it.
conn.Open();
SqlCommand cmd43 = new SqlCommand(Product1ProfitPercentQuery, conn);
SqlDataReader rd43 = cmd43.ExecuteReader();
if (rd43.HasRows) {
while (rd43.Read())
{
string Product1= rd43.GetString(0);
Products.Add(Product1);
}
}
rd43.Close();
conn.Close();