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
}

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Note that ExecuteScalar returns object. You will need to cast the result to int.
And you have to type-cast the result.
His problem is, how to read the output of the query. How does this have so many up votes without the answer?
@Chris - This is the answer - surely?
Can you make an edit showing how to access the count(*) stored in the ddListCmd? I think he will accept your answer after that!
|
0
int result=ddlistCmd.ExecuteScalar();

Comments

-1

Try:

myReader.Read();
count= int.Parse(myReader[0].ToString());

1 Comment

RTFM ;) for exactly THIS there is a better wy (ExecuteScalar)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.