title: SQL update failing to update database
body: <p>This is my code behind for data update. But it is not updating in my database. Don't know why. Any suggestion pls.
  I've check the database connection and it is working fine. I didn't declare the connection string using {..}.
  Actually I didn't get any error message for the insert. I got an record update message. But in my database, it is not updating. </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.Data.SqlClient;
  using System.Configuration;
  
  
  namespace StudentDataDisplay2
  {
      public partial class Form1 : Form
      {
          SqlConnection conn = new SqlConnection(@"Data Source=localhost;Initial Catalog=TestData;Integrated Security=True");
  
          public Form1()
          {
              InitializeComponent();
              this.Text = "Student Data Display Form";
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
  
          }
  
          public void display_data()
          {
              conn.Open();//establish connection
              SqlCommand cmd = conn.CreateCommand();
              //cmd.CommandType = CommandType.Text();
              cmd.CommandText = "SELECT * from StudentDetails";
              cmd.ExecuteNonQuery();
              DataTable dt = new DataTable();
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              da.Fill(dt);
              dataGridView1.DataSource = dt;
  
              conn.Close();
  
          }
          private void btnInsert_Click(object sender, EventArgs e)
          {
              conn.Open();//establish connection
              SqlCommand cmd = conn.CreateCommand();
              //cmd.CommandType = CommandType.Text();
              cmd.CommandText = "INSERT INTO StudentDetails VALUES (@Name,@Subject)";
              cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = textBox1.Text; //add values in textbox1 and store in db
              cmd.Parameters.Add("@Subject", SqlDbType.NVarChar).Value = textBox2.Text; //add values in textbox2 and store in db
              cmd.ExecuteNonQuery();
              conn.Close();
  
              display_data();
  
              MessageBox.Show("Record added");
          }
  
          private void btnDisplay_Click(object sender, EventArgs e)
          {
              display_data();
          }
  
          private void btnDelete_Click(object sender, EventArgs e)
          {
              conn.Open();//establish connection
              SqlCommand cmd = conn.CreateCommand();
              //cmd.CommandType = CommandType.Text();
              cmd.CommandText = "DELETE FROM StudentDetails WHERE Name= @Name";
              cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = textBox1.Text; //add values in textbox1 and store in db
              cmd.ExecuteNonQuery();
              conn.Close();
  
              display_data();
  
              MessageBox.Show("Record deleted");
          }
  
          private void btnUpdate_Click(object sender, EventArgs e)
          {
              //conn.Open();//establish connection
              SqlCommand cmd = conn.CreateCommand();
  
              cmd.CommandText = "UPDATE StudentDetails SET Name = @Name WHERE Subject = @Subject";
              cmd.Parameters.AddWithValue("@Name", textBox1.Text);
              cmd.Parameters.AddWithValue("@Subject", textBox2.Text);
  
              conn.Open();
              cmd.ExecuteNonQuery();
              conn.Close();
  
              display_data();
  
              MessageBox.Show("Record updated!");
          }
  
          private void buttonSearch_Click(object sender, EventArgs e)
          {
              conn.Open();//establish connection
              SqlCommand cmd = conn.CreateCommand();
              //cmd.CommandType = CommandType.Text();
              cmd.CommandText = "DELETE FROM StudentDetails WHERE Name= @Name";
              cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = textBox1.Text; //add values in textbox1 and store in db
              cmd.ExecuteNonQuery();
              conn.Close();
  
  
              display_data();
  
              MessageBox.Show("Search completed!");
          }
      }
  }
  </code></pre>