how to update table data in sql database
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
install SQL Server Express (and you've already done that anyway)
install SQL Server Management Studio Express
create your database in SSMS Express, give it a logical name (e.g.
ASPNETDB
)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 '@' .
Comments
-
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 almost 12 yearsNo 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 almost 12 yearsI'd be willing to guess that your update matches 0 rows
-
Kamil Będkowski almost 12 yearsIn 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 almost 12 yearsput "@fn" similarly "@fl" for all your column name hope so it will help you
-
Admin almost 12 yearsplease try to mention the error that you got
-
Adil Mammadov almost 12 yearsHave you tried cmd.BindByName=true??
-
Eshant Sahu almost 12 yearserror is : Incorrect syntax near ','.
-
-
Damith almost 12 years
@
is not must, issue is something else