title: Getting Data From Sql Server 2008 with C#
body: <p>I'm trying to make a login facility for Windows Forms Application project. I'm using  Visual Studio 2010 and MS Sql Server 2008.</p>
  
  <p>I referenced this article:
   <a href="http://www.codeproject.com/Articles/4416/Beginners-guide-to-accessing-SQL-Server-through-C" rel="nofollow noreferrer">http://www.codeproject.com/Articles/4416/Beginners-guide-to-accessing-SQL-Server-through-C</a></p>
  
  <p>Here is my database table named user:
  <img src="https://i.stack.imgur.com/EEg4w.jpg" alt="enter image description here"></p>
  
  <p>I have <code>TextBox1</code> <strong>for user name</strong> , <code>TextBox2</code> <strong>for user password</strong> and <code>Button1</code> <strong>for starting login process</strong>. Here is my code for <code>Button1_Click</code> method:</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
  {
      string kullaniciAdi; // user name
      string sifre; // password
  
      SqlConnection myConn = new SqlConnection();
      myConn.ConnectionString = "Data Source=localhost; database=EKS; uid=sa; pwd=123; connection lifetime=20; connection timeout=25; packet size=1024;";
      myConn.Open();
      try 
      {
          SqlDataReader myReader;
          string myQuery = ("select u_password from user where u_name='" + textBox1.Text + "';");
          SqlCommand myCommand = new SqlCommand(myQuery,myConn);
          myReader = myCommand.ExecuteReader();
          while (myReader.Read())
          {
              sifre = myReader["u_password"].ToString();
          }
      }
      catch (Exception x) 
      {
          MessageBox.Show(x.ToString());
      }
      myConn.Close();
  }
  </code></pre>
  
  <p>I don't have much experience with C# but i think i'm missing something small to do it right. Below i share exception message that i catched. Can you show me what i'm missing? (line 33 is <code>myReader = myCommand.ExecuteReader();</code>)</p>
  
  <p><img src="https://i.stack.imgur.com/dCP19.jpg" alt="enter image description here"></p>
  
  <p>Considerin given answers, i updated my try block as in below but it still does not work.</p>
  
  <pre><code>try
  {
      SqlDataReader myReader;
      string myQuery = ("select u_password from [user] where u_name=@user");
      SqlCommand myCommand = new SqlCommand(myQuery, myConn);
      myCommand.Parameters.AddWithValue("@user", textBox1.Text);
      myReader = myCommand.ExecuteReader();
      while (myReader.Read())
      {
          sifre = myReader["u_password"].ToString();
      }
  
      if (textBox2.Text.Equals(sifre))
      {
          Form2 admnPnl = new Form2();
          admnPnl.Show();
      }
  }
  </code></pre>
  
  <p>After changing whole code as below by sine's suggestion, screenshot is also below:
  And i think, somehow i cannot assign password in database to the string sifre.</p>
  
  <p>code:</p>
  
  <pre><code>string sifre = "";
  var builder = new SqlConnectionStringBuilder();
  builder.DataSource = "localhost";
  builder.InitialCatalog = "EKS";
  builder.UserID = "sa";
  builder.Password = "123";
  
  using (var conn = new SqlConnection(builder.ToString()))
  {
      using (var cmd = new SqlCommand())
      {
          cmd.Connection = conn;
          cmd.CommandText = "select u_password from [user] where u_name = @u_name";
          cmd.Parameters.AddWithValue("@u_name", textBox1.Text);
          conn.Open();
  
          using (var reader = cmd.ExecuteReader())
          {
              while (reader.Read())
              {
                  var tmp = reader["u_password"];
                  if (tmp != DBNull.Value)
                  {
                      sifre = reader["u_password"].ToString();
                  }
              }
  
              if (textBox2.Text.Equals(sifre))
              {
                  try
                  {
                      AdminPanel admnPnl = new AdminPanel();
                      admnPnl.Show();
                  }
                  catch (Exception y)
                  {
                      MessageBox.Show(y.ToString());
                  }
              }
              else
              {
                  MessageBox.Show("incorrect password!");
              }
          }
      }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/aYrOB.jpg" alt="enter image description here"></p>