Eshant Sahu
Eshant Sahu

Reputation: 360

how to update table data in sql database

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 !!! :(

Upvotes: 1

Views: 13135

Answers (2)

marc_s
marc_s

Reputation: 755157

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...

Upvotes: 3

Tirumudi
Tirumudi

Reputation: 443

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

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

Upvotes: 2

Related Questions