I have a database table and I am querying this table to check if a value already exists (for a login application).
I have a data adapter, but I do not know how to check if the data adapter contains the value I am looking for...
Here is the c#:
string connectionString = @"Data Source=.\SQLEXPRESS;Database=Employee;Integrated Security=true";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter da = new SqlDataAdapter("SELECT UName FROM EVUSERS WHERE UName = '" + userName + "'", connection);
da.Fill(dt);
//this message displays proving the adapter contains values
if(da != null)
{
MessageBox.Show("name exists");
}
}
I just would like to know how I can say something like
if (da contains username) { do something }
Thank you.
You have to use the table's Rows.Count
property.
But you should really use sql-parameters to prevent sql-injection!
SqlDataAdapter da = new SqlDataAdapter("SELECT UName FROM EVUSERS WHERE UName = @UName", connection);
da.SelectCommand.Parameters.AddWithValue("@UName", userName);
da.Fill(dt);
if(dt.Rows.Count > 0)
{
MessageBox.Show("name exists");
}