Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET ADO.NET
hey...
look this code why it doesnt work ?
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 2-Oct-12 5:24am
Edited 10-Mar-13 5:38am
v2
Comments
sjelen at 2-Oct-12 12:09pm
   
And what is the error you get?
source.compiler at 2-Oct-12 15:51pm
   
i dont get error... just my codes dnt work

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

In place of
 
 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:
 
 cmd.Parameters.AddWithValue("@Name",txtName.Text.Trim());
  Permalink  
v3
Comments
source.compiler at 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)

  Print Answers RSS
0 OriginalGriff 383
1 Richard Deeming 180
2 ChauhanAjay 176
3 Sergey Alexandrovich Kryukov 171
4 Yogesh Kumar Tyagi 165
0 Sergey Alexandrovich Kryukov 6,252
1 OriginalGriff 5,498
2 CPallini 2,473
3 Richard MacCutchan 1,627
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 10 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100