Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How should I modify this code to Update my record in the database?
C#
private void KlantenInfoUploaden()
        {
            string Organisatie = TX_Organisatie.Text;
            string BezoekAdres = TX_BezoekAdres.Text;
            String BezoekPostCode = TX_BezoekPostCode.Text;
            string BezoekPlaats = TX_BezoekPlaats.Text;

            string FactuurAdres = TX_FactuurAdres.Text;
            string FactuurPostCode = TX_FactuurPostCode.Text;
            string FactuurPlaats = TX_FactuurPlaats.Text;

            string Telefoon = TX_Telefoon.Text;
            string Fax = TX_Fax.Text;
            string Email = TX_Email.Text;
            string Website = TX_Website.Text;

            const string queryKLantBewerkenID = "Update Organisatie, bezoek_adres, bezoek_postcode, bezoek_plaats, factuur_adres, factuur_postcode, factuur_plaats, telefoon, fax, mail, website FROM Klanten WHERE [IDklanten] = @KlantID";

            using (SqlConnection connection = new SqlConnection(connectionstring))
            using (SqlCommand command = new SqlCommand(queryKLantBewerkenID, connection))
            { 
                command.Parameters.AddWithValue("@KlantID", XX_klantID.Text);

                try {
                    connection.Open();
                    
              //HELP!


                }
                catch(Exception exp) {
                    MessageBox.Show("FOUT");
                }
                
            }
        }
Posted
Updated 15-Dec-15 20:44pm
v2

Your SQL command is not valid: If I rip out most of it so it;s more readable then it comes down to:
SQL
Update Organisatie FROM Klanten WHERE [IDklanten] = @KlantID
Which is the format for a SELECT command not an update.
You need:
SQL
UPDATE Klanten SET Organisatie=@ORG WHERE [IDklanten] = @KlantID
And to supply the "@ORG" value via a parameter in the same way you do @KlantID.
Then a command.ExecuteNonQuery will do the update.
 
Share this answer
 
C#
command.ExecuteNonQuery();

seems a reasonable choice.
 
Share this answer
 
Comments
MaikelO1 15-Dec-15 11:01am    
Where do I add this?
phil.o 15-Dec-15 11:09am    
Inside the try..catch block seems a good idea.
But you should have a look at solution 2 before, which hold a good advise about your update query itself.
OriginalGriff 15-Dec-15 11:07am    
Have you looked at the SQL command?
phil.o 15-Dec-15 11:13am    
No, I must confess I just gave the query a miss and went directly to the //HELP comment. Thanks for pointing it out :)
OriginalGriff 15-Dec-15 11:25am    
You're welcome! :laugh:

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