Home > DeveloperSection > Forums > how achieve batch update data
Kenny Tangnde

Total Post:68

Points:476
Posted on    January-31-2012 11:52 AM

 C# C# 
Ratings:


 1 Reply(s)
 1480  View(s)
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;

        }
   }



James Smith

Total Post:48

Points:336
Posted on    February-01-2012 1:04 AM

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.

Don't want to miss updates? Please click the below button!

Follow MindStick