Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey...
look this code why it doesnt work ?
C#
string sqlc = System.Configuration.ConfigurationManager.ConnectionStrings["ChattersDB"].ConnectionString;
System.Data.SqlClient.SqlConnection sc = new    System.Data.SqlClient.SqlConnection(sqlc);
string sqlcommand = "UPDATE UserPubInf SET Name = @Name WHERE Username = 'ali'";//+ "'" + Session["UsernameSession"].ToString() + "'";
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlcommand, sc);
 System.Data.SqlClient.SqlParameter sp = new System.Data.SqlClient.SqlParameter("@Name", System.Data.SqlDbType.NVarChar, 50);
sp.Value = txtName.Text ;
cmd.Parameters.Add(sp);
sc.Open();
cmd.ExecuteNonQuery();
sc.Close();


i want to modify column by textbox named txtName .... but it doesnt work whereas when i dnt use cvariable it work as well ...
i need use variable to define my column value....
please elp
Posted
Updated 10-Mar-13 5:38am
v2
Comments
sjelen 2-Oct-12 12:09pm    
And what is the error you get?
ali_heidari_ 2-Oct-12 15:51pm    
i dont get error... just my codes dnt work

1 solution

In place of

c3
 System.Data.SqlClient.SqlParameter sp = new System.Data.SqlClient.SqlParameter("Name", System.Data.SqlDbType.NVarChar, 50);
sp.Value = txtName.Text ;
cmd.Parameters.Add(sp);

You can write:

C#
cmd.Parameters.AddWithValue("@Name",txtName.Text.Trim());
 
Share this answer
 
v3
Comments
ali_heidari_ 2-Oct-12 16:08pm    
i dnt know why , but it didnt work at all, if i define a string value it works but when i use a variable it doesnt work at all ... its problem is with variables :S

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