title: I want to insert data into a SQL Server databse using C#
body: <p>I want to insert data into a SQL Server database using C#. </p>
  
  <p>There is no error in my code. But after entering the data I traverse. It shows me the data. But when I open the database table. I can't see the inserted data. I initialize the table in the time of creation with 2 rows. But when I traverse through form I can't see those rows. </p>
  
  <p>After so many trial an error. I see that database connection breaks down whenever I run it. I saw that the "green plug" on the database icon went to "red cross" after clicking F5. Is that the reason? If it is then what should be the solution ? </p>
  
  <p>Guys please help me. I am new at C#.</p>
  
  <p>This is <code>insert.cs</code>:</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;
  
  namespace employeDB1
  {
  public partial class Insert : Form
  {
      public Insert()
      {
          InitializeComponent();
      }
  
      DatabaseConnection objConnect;
      string conString;
      int inc ;
      int MaxRows;
      DataSet ds = new DataSet();
      DataRow dRow;
  
      public void Insert_Load(object sender, EventArgs e)
      {
          try
          {
              objConnect = new DatabaseConnection();
              conString = Properties.Settings.Default.EmployeeConnectionString;
              objConnect.connection_string = conString;
              objConnect.Sql = Properties.Settings.Default.SQL;
              ds = objConnect.GetConnection;
              MaxRows = ds.Tables[0].Rows.Count;
              inc = MaxRows - 1;
              //NavigateRecords();
          }
          catch (Exception err)
          {
              MessageBox.Show(err.Message);
          }
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
              DataRow row = ds.Tables[0].NewRow();
              row[0] = int.Parse(textBox1.Text);
              row[1] = textBox2.Text;
              row[2] = textBox3.Text;
              row[3] = int.Parse(textBox4.Text);
              ds.Tables[0].Rows.Add(row);
          try
          {
  
              objConnect.UpdateDatabase(ds);
              MaxRows = MaxRows + 1;
              inc = MaxRows - 1;
              MessageBox.Show("RECORD INSERTED");
  
          }
          catch(Exception err)
          {
              MessageBox.Show(err.Message);
          }
      }
  
      private void NavigateRecords()
      {
          dRow = ds.Tables[0].Rows[inc];
          textBox1.Text = dRow.ItemArray.GetValue(0).ToString();
          textBox2.Text = dRow.ItemArray.GetValue(1).ToString();
          textBox3.Text = dRow.ItemArray.GetValue(2).ToString();
          textBox4.Text = dRow.ItemArray.GetValue(3).ToString();
      }
  
      private void button2_Click(object sender, EventArgs e)
      {
          if (inc&gt;0)
          {
              inc--;
              NavigateRecords();
  
          }
          else
          {
              MessageBox.Show("First Record");
          }
      }
   }
  }
  </code></pre>
  
  <p>This is the database connection class:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace employeDB1
  {
  class DatabaseConnection
  {
      private string sql_string;
      private string strCon;
      System.Data.SqlClient.SqlDataAdapter da_1;
  
      public string Sql
      {
          set { sql_string = value; }
      }
  
      public string connection_string
      {
          set { strCon = value; }
      }
  
      public System.Data.DataSet GetConnection
      {
          get { return MyDataSet(); }
      }
  
      private System.Data.DataSet MyDataSet()
      {
          System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);
          con.Open();
          da_1 = new System.Data.SqlClient.SqlDataAdapter(sql_string, con);
          System.Data.DataSet dat_set = new System.Data.DataSet();
          da_1.Fill(dat_set, "Table_Data_1");
          con.Close();
          return dat_set;
      }
  
      public void UpdateDatabase(System.Data.DataSet ds)
      {
          System.Data.SqlClient.SqlCommandBuilder cb = new System.Data.SqlClient.SqlCommandBuilder(da_1);
          cb.DataAdapter.Update(ds.Tables[0]);
      }
  }
  </code></pre>