Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Giving below update code for update all data but suppose sometime need for update ROLLNUMBER with all data So please suggest correct code. my code is

C#
private void Update_Button_Click(object sender, EventArgs e)
        {
            
            SqlConnection con = new SqlConnection(cs); string updateQuery = ("UPDATE Student_Info SET ROLLNUMBER = @ROLLNUMBER,NAME = @NAME,GENDER = @GENDER,AGE = @AGE,DATEOFBIRTH=@DATEOFBIRTH,FEE =@FEE WHERE ROLLNUMBER = @ROLLNUMBER ");
            SqlCommand cmd = new SqlCommand(updateQuery, con);
            cmd.Parameters.AddWithValue("@ROLLNUMBER",RollnumberTextBox.Text);
cmd.Parameters.AddWithValue("@NAME", NameTextBox.Text);
cmd.Parameters.AddWithValue("@GENDER", Gender_ComBoBox.SelectedItem);
cmd.Parameters.AddWithValue("@AGE", Age_TextBox.Text);
cmd.Parameters.AddWithValue("@DATEOFBIRTH", DobTimePicker.Value);
cmd.Parameters.AddWithValue("@FEE", FeeTextBox.Text);
            
            try
            {
                con.Open();
                int a = cmd.ExecuteNonQuery();
                if(a>0)
                {
                    MessageBox.Show("DATA UPDATED SUCESSFULLY");
                }
                
                con.Close();
            }
            catch (Exception EX)
            {

                MessageBox.Show(EX.Message);
            }
        }


What I have tried:

i will serach this query in google and youtube but cannot find this result so many link will be show but did not find solution .
Posted
Updated 10-Mar-23 22:20pm
v2
Comments
Graeme_Grant 10-Mar-23 23:40pm    
This is not your first question, it is your 7th. Please learn to correctly format code blocks for readability. Press the improve question link and update.

The question really is "why are you trying to change a roll number?"
Normally, that is the "official ID" that identifies a student and all the data about him or her.
Changing that is dangerous: it can result in duplications, in data being attached to the wrong student, in all sorts of database integrity failures.

But there is a logic problem with the whole idea:
Quote:
My form contain only 1 TextBox for ROLLNUMBER.
If your form only has one roll number, then you have no value to look for to find the existing record if it contains the new number; or no value to set it to if it contains the original value which identifies the record.
You need two values; without both all you can do is change the roll number to the same value, or not find the existing record!

So FreedMalloc is right: you need to pass two different values through in order to get this to work.
 
Share this answer
 
Comments
kalpeshdelta 11-Mar-23 7:27am    
actually a mistake for explain problem. first search by ROLLNUMBER to search student detail.so ROLLNUMBER and student detail show in respective textbox and combobox then suppose need change ROLLNUMBER the ROLLNUMBER SHOW IN rollnumber textbox. e.g. suppose ROLLNUMBER is 3 show in textbox and this rollnumber change to 5 how to do this. so i can try to declare a variable to store old ROLLNUMBER
kalpeshdelta 11-Mar-23 7:27am    
actually a mistake for explain problem. first search by ROLLNUMBER to search student detail.so ROLLNUMBER and student detail show in respective textbox and combobox then suppose need change ROLLNUMBER the ROLLNUMBER SHOW IN rollnumber textbox. e.g. suppose ROLLNUMBER is 3 show in textbox and this rollnumber change to 5 how to do this. so i can try to declare a variable to store old ROLLNUMBER
OriginalGriff 11-Mar-23 8:13am    
Then *you* need to save the original roll number, and pass it through along with the new value. Make a class level variable, and store it there when you know what it is!
OriginalGriff 11-Mar-23 8:15am    
But as I said: changing the roll number may affect quite a lot of your DB - and you need to be aware that SQL Server is a multiuser database, so any changes have to be considered in that light. It's a dangerous idea, and I have no clue why you think it's something you should provide.
If I understand your question, you'll need 2 ROLLNUMBER values. One is the new value you want to update in the Student_Info table and the other is the current ROLLNUMBER value in the table to use in the WHERE clause. Which also means a new control on your form so the user can enter it and matching cmd.Parameters.AddWithValue statements.
string updateQuery = ("UPDATE Student_Info SET ROLLNUMBER = @NEWROLLNUMBER,NAME = @NAME,GENDER = @GENDER,AGE = @AGE,DATEOFBIRTH=@DATEOFBIRTH,FEE =@FEE WHERE ROLLNUMBER = @CURRENTROLLNUMBER ");
 
Share this answer
 
Comments
kalpeshdelta 11-Mar-23 1:03am    
Sir No My form contain only 1 TextBox for ROLLNUMBER.
it is possible to declare variable to store old ROLLNUMBER and is true to code for this
OriginalGriff 11-Mar-23 1:37am    
:thumbsup:

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