leora
leora

Reputation: 196689

how to determine if zero records are return in SqlDataAdapter

i have the following code

  string connString = ConfigurationManager.ConnectionStrings["XXXConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connString);
        conn.Open();

        SqlDataAdapter SQLDataAdapter1 = new SqlDataAdapter("SELECT * FROM EVENTSignUp WHERE (MembmerEmail = " + userInfo.Email + ")", conn);
        DataTable dtResult1 = new DataTable();
        SQLDataAdapter1.Fill(dtResult1);

but if there are no records returned, i simply get an exception at:

        SQLDataAdapter1.Fill(dtResult1);

how do i determine if there are no records returned from this query?

Upvotes: 0

Views: 3408

Answers (3)

Arsman Ahmad
Arsman Ahmad

Reputation: 2221

You can also check it by using SqlDataReader. Here is a simple example to check if SqlDataReader has result or not by this:

String con_string = "Your Connection String Here";
String strSQL = "Your Query Here";
using (SqlDataReader objNameDR = SQLHelper.ExecuteReader(con_string, CommandType.Text, strSQL, null)) {

    if (objNameDR.HasRows) {
        // Having Rows
    }
    else{
       // Having No Row (Zero Record)
    }

}

Upvotes: 0

Noon Silk
Noon Silk

Reputation: 55132

dtResult1.Rows.Count > 0

-- edit

Didn't follow read the post; notice you are getting an exception on .Fill. Obviously my code snippet here will not help you with that. As others have asked; what is the exception?

-- edit:

And, as others have noted, your query should be of the form:

SqlCommand command = new SqlCommand(@"
select
    *
from
    EVENTSignUp
where
    MemberEmail = @MemberEmail
");

SqlParameter param = new SqlParameter("@MemberEmail", SqlDbType.NVarChar);
param.Value = userInfo.Email;

command.Parameters.Add(param);

SqlDataAdapter dtResult1 = new SqlDataAdapter(command);
DataTable dtResult1 = new DataTable();
SQLDataAdapter1.Fill(dtResult1);

Upvotes: 3

Wael Dalloul
Wael Dalloul

Reputation: 23044

I think the problem not in records returend by SqlDataAdapter because even if it's empty it will not generate exception. the problem in your query because email field is varchar and it should be like this:

SqlDataAdapter SQLDataAdapter1 = new SqlDataAdapter("SELECT * FROM EVENTSignUp WHERE (MembmerEmail = '" + userInfo.Email + "')", conn);

Upvotes: 2

Related Questions