HOW ACHIEVE BATCH UPDATE DATA

Kenny Tangnde

Total Post:68

Points:476
Posted by  Kenny Tangnde
C# 
C#
 1904  View(s)
Ratings:
Rate this:

hi all,

I am trying to achieve batch update data, but failed to achieve, give correction, the code is as follows
thank in advance.

 public partial class UpdateTranData : Form
    {
        public UpdateTranData()
        {
            InitializeComponent();
        }
        SqlDataAdapter adapter;
        DataSet ds;
        string connectionString = @"data source=StudentDB;initial catalog=MYDB;user=sa;password=PASSWORD;";
        SqlConnection sqlCnn;
        SqlCommand sqlCmd;
        string sql = null;
        private void UpdateTranData_Load(object sender, EventArgs e)
        {
            sql="SELECT *FROM TRAN_INFO";
            sqlCnn = new SqlConnection(connectionString);
            sqlCnn.Open();
            adapter = new SqlDataAdapter(sql, sqlCnn);// specifing SQL statement and Database connection
            ds = new DataSet();//creating instance of DataSet
            adapter.Fill(ds); //filling DataSet
            dataGridView1.DataSource = ds.Tables[0];//Binding DataGridView with DataSet
           
        }
       
      private void btnUpdate_Click(object sender, EventArgs e)
       {
          
           SqlCommandBuilder scb = new SqlCommandBuilder(adapter);
           if (MessageBox.Show("Are you sure?","Warning",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)==DialogResult.Yes)
           {
               try
               {
                   adapter.Update(ds.Tables[0]);
                   sqlCnn.Close();
                   MessageBox.Show("Data updated successful!");
                  
               }
               catch (Exception)
               {

                   MessageBox.Show("Data updated defeat!");
                   sqlCnn.Close();
               }
           }
          private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
           txtID.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
           txtStart_Station.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
           txtEnd_Station.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
           txID.Enabled = false;

        }
   }

  1. James Smith

    Post:48

    Points:336
    Re: how achieve batch update data

    Hi Aken,
    I checked your code. It's working fine. Could you tell me exception or error which you are getting in program so I can understand exact problem.

    Thanks.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!