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;
}
}