Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i have a form which contains one textbox and one data gridview. i have a button which saves
data into the db successfully. i have another button which i want to use in updating this record
in the db. how do i do this, please help me out

code for saving

C#
private void btnSave_Click(object sender, EventArgs e)
{
    string StrQuery;

    try
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]))
        {
            conn.ConnectionString = "Data Source=user-PC;Initial Catalog=MUCGPROJECT;User ID=sa;Password=mike";
            using (SqlCommand comm = new SqlCommand())
            {
                comm.Connection = conn;
                //conn.Open();
                for (int i = 0; i < dgvDistrict.Rows.Count; i++)
                {
                    StrQuery = @"INSERT INTO DistrictnT VALUES ('" + this.txtDistrict.Text + "', '" + dgvDistrict.Rows[i].Cells["clmTowns"].Value + "');";
                    comm.CommandText = StrQuery;
                    conn.Open();
                    comm.ExecuteNonQuery();
                    conn.Close();
                }
                cleardgv();
                MessageBox.Show("Record is successfully Added");
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Posted
v2
Comments
Richard C Bishop 11-Apr-13 16:51pm    
Just do the same thing you did with the INSERT statement, but just use an UPDATE statement instead. You are, however, leaving yourself open to SQL injection which could ruin your database. I would look into parameterized queries to avoid future trouble.
ZurdoDev 12-Apr-13 8:09am    
You have code that saves data but you can't update data? I'm confused. Where are you stuck?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900