0

I am trying to create a select statement in C# to check if the value inserted into a textbox (userName) is in an existing SQL database. I have a database called Employee containing a table called EVUSERS and it has a column called UName.

In my code I have a method which takes the value from a textbox called UserBox. I would like to know if there is a temporary table where the select is stored which I can compare the textbox value to.

Here is the code:

private void CheckLoginExist()
{
            String userName = UserBox.Text;

            string connectionString = @"Data Source=.\SQLEXPRESS;Database=Employee;Integrated Security=true";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = connection.CreateCommand())
                {

                    command.CommandText = "SELECT UName FROM EVUSERS WHERE UName = @UName";
                    command.Parameters.AddWithValue("@UName", userName);
                    connection.Open();
                    command.ExecuteNonQuery();
                    connection.Close();
                }
            }
}

I currently have a select but I am not sure how to display it and I am able to connect to the DB.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 1
    Your returning a single value (or null) so use `ExecuteScalar` as `ExecuteNonQuery` is for non-queries (which includes `select`) – Alex K. Jan 23 '14 at 15:21
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders Jan 24 '14 at 16:01

2 Answers2

1

You need ExecuteScalar, not ExecuteNonQuery

...
connection.Open();
var name = command.ExecuteScaclar().ToString();
connection.Close();

if (name != null) {
  MessageBox.Show("This name already exists");
  return;
}
Marko Juvančič
  • 5,792
  • 1
  • 25
  • 41
0
private void CheckLoginExist()
{
    DataTable dt = new DataTable();
    String userName = UserBox.Text;

   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  + "'", _conn);                 
         da.Fill(dt); 
   }
}

Now you can do whatever you want to do with this DataTable.

Abdur Rahim
  • 3,975
  • 14
  • 44
  • 83
  • Thanks. How would I perhaps, display in an if statement displaying a messagebox if it returns true? What would be the argument that I would use? if(userName == this.userName) { MessageBox.Show("Match"); } thanks –  Jan 23 '14 at 15:42
  • How can I test this, to see if I have an existing username in the database. If I enter a value into the textbox which exists in the database, What can parameter I use that will check it. If I want a messagebox to pop up saying that username exists? Thank you again –  Jan 23 '14 at 15:47