Click here to Skip to main content
15,887,435 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
my Update button is not working whta is the correct code of update button This is my update button code.


C#
private void button2_Click(object sender, EventArgs e)
 {
     try
     {
         if (textBox1.Text == "" || textBox2.Text == "" || comboBox1.Text== ""|| textBox3.Text == "")
         {
             MessageBox.Show("Missing information");
         }
         else
         {

             Con.Open();
             if (int.TryParse(textBoxCId.Text, out int CId))
             { 
             SqlCommand cmd = new SqlCommand("UPDATE EventTbl SET CName=@CN,CPhone=@CP,Events=@E,Date=@D WHERE CId=@key", Con);
             cmd.Parameters.AddWithValue("@CN", textBox1.Text);
             cmd.Parameters.AddWithValue("@CP", textBox2.Text);
             cmd.Parameters.AddWithValue("@E", comboBox1.Text);
             cmd.Parameters.AddWithValue("@D", textBox3.Text);
             cmd.Parameters.AddWithValue("@key", CId);
             cmd.ExecuteNonQuery();
             Con.Close();
             MessageBox.Show("Record Update Sucessfully");
             DisplayEvent();
             }
             else
             {
                 MessageBox.Show("Invalid CId. Please enter a valid integer value.");
             }

         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
     finally { Con.Close(); }
 }


What I have tried:

I tried some ways but still it not working.
Posted
Updated 26-Dec-23 1:13am
v2
Comments
Mike Hankey 26-Dec-23 6:49am    
What is not working?
Is the event handler being called?
How are you calling the handler?
OriginalGriff 26-Dec-23 7:15am    
"It's not working" is one of the most useless problem descriptions we get: it tells us absolutely nothing about the problem. We don't know if you get an error message, or the wrong data, or even that that code compiles successfully!
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
So tell us what happens when you run that code, what you expected to happen, how you checked what happened. Help us to help you!
Use the "Improve question" widget to edit your question and provide better information.
[no name] 26-Dec-23 12:48pm    
You should do a "select" before your "update" to confirm the record you want to update actually exists. And why are you doing an "open" connection outside your "if"?
Richard MacCutchan 27-Dec-23 6:45am    
Why are you storing Date values in text form, instead of proper Date types? And why do you not check the reurn value from the call t ExecuteNonQuery? TRy fixing those, and then run the code through the debugger to see exactly what is happening.
PIEBALDconsult 27-Dec-23 10:14am    
I see a number of issues which should be addressed:
First, don't have the database code directly in the UI code -- make a separate object/project for all the Database Access Layer. Then call that from the UI layer.
Second, have the button (button2) disabled until the textboxes and comboboxes have have values. For this, in the changed handlers for each of the boxes, set the Enabled property of the button to true only if the boxes have appropriate values. I usually write an UpdateUI Method to do that which all the other handlers call. All of that validation should happen before the button can even be pressed. Including checking to determine the existence of the ID.
Third, you need to check the state of the connection in the finally before just trying to close it. No need to close the connection inside the try if the finally will do it anyway.
Fourth, can you make the textBoxCId a NumericUpDown instead?

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