Click here to Skip to main content
15,914,221 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all.
I have a web form that has 5 text boxes in it. The text boxes can be filled out and saved to the database. I have an existing table that has columns with data already in them. I added 4 new columns to the table. I am trying to save the new data that the user just entered into the 5 text boxes to the existing record without changing or saving the same record twice. Is there a way to do that? Here is what I have so far.

C#
protected void ButtonSave_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand("Update Table22 (INST_ID, TOTAL_REVE, INSTRUCTIO, RESEARCH, PUBLIC_SER) values (@INST_ID, @TOTAL_REVE, @INSTRUCTIO, @RESEARCH, @PUBLIC_SER)", con);
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
            cmd.Parameters.AddWithValue("@TOTAL_REVE", TextBoxFY09_2.Text);
            cmd.Parameters.AddWithValue("@INSTRUCTIO", TextBoxFY09_3.Text);
            cmd.Parameters.AddWithValue("@RESEARCH", TextBoxFY09_4.Text);
            cmd.Parameters.AddWithValue("@PUBLIC_SER", TextBoxFY09_5.Text);
            
            

            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}


Is this right so far? Am I on the right track or am I way off?
Posted

No, it's not. You wrote an UPDATE query like you would have done for an INSERT query. But that does not work like this.

The best is to have the primary key of the record you want to update. Then the UPDATE query should look like:
C#
"UPDATE Table22 SET TOTAL_REVE = @TOTAL_REVE, INSTRUCTIO = @INSTRUCTIO, RESEARCH = @RESEARCH, PUBLIC_SER = @PUBLIC_SER WHERE INST_ID = @INST_ID"


Hope this helps. Good luck.
 
Share this answer
 
v3
Comments
Computer Wiz99 5-Mar-14 9:57am    
Hey phil.o, Thanks for the info. In this code will the INST_ID be inserted twice or is it just using it to update the right record? What about the textboxes? I don't have to get the data from the textboxes?
phil.o 5-Mar-14 10:08am    
I decently cannot answer these questions with the little knowledge I have of your project...
But there seems to remain a doubt in your mind between an UPDATE query and an INSERT query, isn't it?
Edit:
Actually you need to get parameters values out of your textboxes; the way you did it (parameterized query) is just fine. You just have to change the query and see what's happening.
Computer Wiz99 5-Mar-14 10:15am    
Ok. Yeah there is a little doubt yes. I have never did an Update query before. But I will test this and add in other code to see what happens and get back to you.
phil.o 5-Mar-14 10:18am    
No problem.
Do not hesitate to have a quick review on INSERT and UPDATE queries; there:
INSERT (Transact-SQL)
UPDATE (Transact-SQL)
Maciej Los 5-Mar-14 13:06pm    
5ed!
Please, see my answer ;)
With relation to phil.o[^] answer, i would recommend you to use stored procedures[^] instead the query in code behind, for example:

SQL
CREATE PROCEDURE UpdateSomething
    @InputParameter1 DataType,
    @InputParameter1 DataType,
    ...
    @InputParameterN DataType,
AS
BEGIN
    UPDATE TableName
    SET Field1 = @InputParameter1,
    Field2 = @InputParameter2,
    ....
    FieldN = @InputParameterN

END


For further information, please see: How to: Execute a Stored Procedure that Returns Rows[^] and follow the related links on the left side of window.

ASP.NET sites should be protected from SQL Injection[^].
How To: Protect From SQL Injection in ASP.NET[^]
Stop SQL Injection Attacks Before They Stop You[^]
SQL Injection and how to avoid it[^]
 
Share this answer
 
v2
Comments
phil.o 6-Mar-14 2:20am    
5ed also :)
Maciej Los 6-Mar-14 13:51pm    
Thank you, Phil ;)

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