简体   繁体   中英

When i click the update button to update database values in SQL C# All Rows Are Affected by the same value

This is my screen shot

Bellow Is My Code For Update Button Click Event!

    {
        try
    {

        con = new SqlConnection(cs.ConDB);
        con.Open();
        string cb = "Update tblFees set Salutation= '" + cmbSalutation.Text + "' , Name= '" + tbName.Text + "',Sex = '" + cmbSex.Text + "', Date ='" + Date.Text + "',Fees_Amount='" + cmbFeesAmount.Text + "',Fees_Status='" + radioButton1.Checked + "'";
        cmd = new SqlCommand(cb);
        cmd.Connection = con;
        cmd.ExecuteReader();
        con.Close();
        MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);

        btnUpdate.Enabled = false;
        btnSave.Enabled = true;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    try
    {
        con = new SqlConnection(cs.ConDB);
        con.Open();
        cmd = new SqlCommand("SELECT * From tblFees", con);
        SqlDataAdapter myDA = new SqlDataAdapter(cmd);
        DataSet myDataSet = new DataSet();
        myDA.Fill(myDataSet, "tblFees");
        dataGridView1.DataSource = myDataSet.Tables["tblFees"].DefaultView;
        con.Close();


    }catch (Exception ex)
        {
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,                          MessageBoxIcon.Error);}`

Please Solve my problem i am new in the programming world any help would be greatly appreciated

you should specify the rows using where condition

For example you can modify your query something like the following. I assume cboUser is a combo box there you can select particular user, so that the data updated for that selected user only.

SqlConnection con = new SqlConnection();
con.Open();
string cb = "Update [tblFees] set Salutation=@Salutation, Name=@Name,Sex =@Sex where tblFeesPK=@pk'";
SqlCommand cmd = new SqlCommand(cb, con);
cmd.Parameters.AddWithValue("@Salutation", cmbSalutation.Text);
cmd.Parameters.AddWithValue("@Name", tbName.Text);
cmd.Parameters.AddWithValue("@Sex", cmbSex.Text);
cmd.Parameters.AddWithValue("@pk", cboUser.SelectedValue);
cmd.ExecuteNonQuery();

If you want to update details based on name means: you can give name in where condition. but it's not a proper way. so use primary key(since name may have duplicate values)

You need to change the statement as:

string cb = "Update tblFees set Salutation= '" + cmbSalutation.Text + "' , Name= '" + tbName.Text + "',Sex = '" + cmbSex.Text + "', Date ='" + Date.Text + "',Fees_Amount='" + cmbFeesAmount.Text + "',Fees_Status='" + radioButton1.Checked + "' where Name= '" + tbName.Text + "'";

You need to add where Name= '" + tbName.Text + "';

Now it will update those rows where Name matches

Also as un-lucky said u should use parameterized queries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM