how to update table data in sql database

13,045

Solution 1

As I've said before on this site - the whole User Instance and AttachDbFileName= approach is flawed - at best! Visual Studio will be copying around the .mdf file and most likely, your UPDATE works just fine - but you're just looking at the wrong .mdf file in the end!

If you want to stick with this approach, then try putting a breakpoint on the con.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.

The real solution in my opinion would be to

  1. install SQL Server Express (and you've already done that anyway)

  2. install SQL Server Management Studio Express

  3. create your database in SSMS Express, give it a logical name (e.g. ASPNETDB)

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:

    Data Source=.\\SQLEXPRESS;Database=ASPNETDB;Integrated Security=True
    

    and everything else is exactly the same as before...

Solution 2

cmd.Parameters.AddWithValue("@fn", TextBox3.Text);

You have to specify the parameter name along with '@' .

Share:
13,045
Eshant Sahu
Author by

Eshant Sahu

I love to solve problems with code :)

Updated on November 22, 2022

Comments

  • Eshant Sahu
    Eshant Sahu over 1 year

    I am using asp.net for my project , and I am using the following code , but its not working correctly

     SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\WEB_PROJECT\\App_Data\\ASPNETDB.MDF;Integrated Security=True;User Instance=True");
     SqlCommand cmd = new SqlCommand();
     cmd.Connection = con;
     con.Open();
     cmd.CommandText = "UPDATE info SET fname = @fn, lname = @fl, phone= @ph, recoveryq=@rq, recoverya=@ra WHERE username = @un";  
    
     cmd.Parameters.AddWithValue("fn", TextBox3.Text);
     cmd.Parameters.AddWithValue("fl", TextBox4.Text);
     cmd.Parameters.AddWithValue("ph", TextBox5.Text);
     cmd.Parameters.AddWithValue("rq",TextBox6.Text);
     cmd.Parameters.AddWithValue("ra",TextBox2.Text);
     cmd.Parameters.AddWithValue("un",line);        
     cmd.ExecuteNonQuery();
     con.Close();
    

    Advice plzz i m confused !!! :(

    • El Ronnoco
      El Ronnoco almost 12 years
      No one will be able to help you unless you explain why it's "not working". Please post the error you receive or explain in what way it is not doing what you want.
    • KingCronus
      KingCronus almost 12 years
      I'd be willing to guess that your update matches 0 rows
    • Kamil Będkowski
      Kamil Będkowski almost 12 years
      In my opinion the way You have chosen is a bit obsolete. Consider use some ORM (like Entity Framework). It makes Your developing much easier and faster.
    • Admin
      Admin almost 12 years
      put "@fn" similarly "@fl" for all your column name hope so it will help you
    • Admin
      Admin almost 12 years
      please try to mention the error that you got
    • Adil Mammadov
      Adil Mammadov almost 12 years
      Have you tried cmd.BindByName=true??
    • Eshant Sahu
      Eshant Sahu almost 12 years
      error is : Incorrect syntax near ','.
  • Damith
    Damith almost 12 years
    @ is not must, issue is something else