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 375
1 Prakriti Goyal 242
2 Yogesh Kumar Tyagi 224
3 Sergey Alexandrovich Kryukov 158
4 CPallini 150
0 OriginalGriff 6,742
1 Sergey Alexandrovich Kryukov 5,479
2 Maciej Los 3,474
3 Peter Leow 3,313
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | 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