Click here to Skip to main content
15,513,571 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I have a WindowsForms app using MS SQL database.
I am trying to update a row (I have a datagridview, but I am updating the row from a child form). So I was hoping to be able to reference my ID textbox in this child form, so that it'll update the row with the same ID in the database. But I am not sure how to write that UPDATE string.

This is my code:

C#
public static string myString = "what do I write here?"

public void EditRow() {

using (SqlConnection sqlCon = new SqlConnection(ConnectionString.connectiongString))
using (SqlCommand sqlCmd = new SqlCommand(); {

    sqlCon.Open();
    sqlCmd.Connection = sqlCon;
    sqlCmd.CommandText = "myString";
    sqlCmd.Parameters.AddWithValue("@myvalue1", textBox1.Text.ToString());
    sqlCmd.Parameters.AddWithValue("@myvalue2", textBox2.Text.ToString());
    sqlCmd.Parameters.AddWithValue("@myvalue3", textBox3.Text.ToString());
    sqlCmd.ExecuteNonQuery();
}


What I have tried:

I use the same code with a INSERT string for adding a new row, which works fine.
I've looked through documentation and tried implementing solutions from similar questions, but I can't seem to get it just right.
This is my current string, I'm just not sure what to add after the WHERE...

C#
public static string redigerLøyvehaver = "UPDATE Løyvehaver2 SET Foretaksnavn = @Foretaksnavn, Foretaksnummer = @Foretaksnummer, Adresse = @Adresse, Postnummer = @Postnummer, Poststed = @Poststed, BIC = @BIC, IBAN = @IBAN WHERE ......";
Posted
Updated 10-May-21 2:48am
Comments
FranzBe 10-May-21 6:53am    
In case your ID sits in textbox1, you might want to name it so:
sqlCmd.Parameters.AddWithValue("@licenseeID", textBoxLicenseeID.Text.ToString());

In the SQL Update command you would write
"UPDATE ... WHERE columnNameWithID = @licenseeID "

I would not recommend to have special chars in tablenames, you are likely to regret this at some point of time.
Flidrip 10-May-21 7:22am    
FranzBe, thank you so much, that worked like a charm.
I am a bit embarrassed, because I thought I had already tried that, but I must've done something wrong....

When you say special characters, you must mean the 'ø', which is a part of the norwegian alphabet :-D you might be right, it could be smart to "de-nationalize" the names.

Thanks alot!

1 solution

The basic form is:
SQL
UPDATE MyTable SET <column name> = <new value> WHERE <ID column name> = <ID Value>

So code like this changes a single row:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("UPDATE myTable SET myColumn1=@C1, myColumn2=@C2 WHERE Id=@ID", con))
        {
        com.Parameters.AddWithValue("@ID", id);
        com.Parameters.AddWithValue("@C1", myValueForColumn1);
        com.Parameters.AddWithValue("@C2", myValueForColumn2);
        com.ExecuteNonQuery();
        }
    }
 
Share this answer
 

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