views:

308

answers:

2

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?

+1  A: 
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);
Noon Silk
+1 for giving an answer that reduced the possibility of a SQL Injection Attack.
Colin Mackay
+1  A: 

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);
Wael Dalloul
agree with you. anyway.... the parameters should be passed with the parameters collection on the command to prevend sql injection and get better performance.
gsharp
yes you are right about the parameters, it should be done by parameters to avoid injection, and I wrote it very fast just to let him know where is the problem.
Wael Dalloul
typo in query was my issue
ooo