4
    String dd_webCofig = ConfigurationManager.ConnectionStrings["server132"].ConnectionString;
    SqlConnection ddlistconn = new SqlConnection(dd_webCofig);
    ddlistconn.Open();

    string ddlist = "select count(*) from jud_order where complex_name=@a and case_no=@b and sign=@c and jud_order_date=@d and user_code=@e";
    SqlCommand ddlistCmd = new SqlCommand(ddlist, ddlistconn);
    ddlistCmd.Parameters.AddWithValue("a", "a");
    ddlistCmd.Parameters.AddWithValue("b", "a");
    ddlistCmd.Parameters.AddWithValue("c", "a");
    ddlistCmd.Parameters.AddWithValue("d", "a");
    ddlistCmd.Parameters.AddWithValue("e", "a");

    SqlDataReader myReader = ddlistCmd.ExecuteReader();

I am having the above query which returns number of rows, now my problem is how t read the output of the query? What i want is

 if(count=0)
{ 
   //Do
} 
else if(counnt >0)
{
    //Do something else
}
Ishan
  • 3,908
  • 28
  • 83
  • 151

3 Answers3

13

You want to use ExecuteScalar(); instead which will return a single result.

So this line:

ddlistCmd.ExecuteReader();

should be:

ddlistCmd.ExecuteScalar();

which you can then assign to count after type casting the result.

m.edmondson
  • 29,632
  • 26
  • 117
  • 199
0
int result=ddlistCmd.ExecuteScalar();
Oleks
  • 31,334
  • 11
  • 76
  • 131
Zo Has
  • 12,193
  • 21
  • 81
  • 147
-2

Try:

myReader.Read();
count= int.Parse(myReader[0].ToString());
Oleks
  • 31,334
  • 11
  • 76
  • 131
Varun Goel
  • 319
  • 3
  • 15