Ah yes ... he didn't really answer your original question explicitly, did
he? Although he gave you enough to put it together pretty easily.
Here's his insert:
cmd.CommandText = "insert into fred (a,b) values (1,'a')";
Now, if you want to put the value of a textbox into field b in his example,
you would replace the value 'a' with the textbox value. That would be the
Text property of the textbox, so if the texbox were named myTextBox, you'd
have:
cmd.CommandText = String.Format(" insert into fred (a,b)
values(1,'{0}') ",myTextBox.Tex t);
Of course this is static SQL which is inefficient and sets you up for SQL
injection attacks and would also blow up if for example the user happened to
type an apostrophe into the textbox, so what you would really want to do is
a parameterized query. So his original code would be something like this:
System.Data.Sql Client.SqlConne ction cn = new
System.Data.Sql Client.SqlConne ction(....);
cn.Open();;
System.Data.Sql Client.SqlComma nd cmd=new
System.Data.Sql Client.SqlComma nd(cn);
cmd.CommandText = "insert into fred (a,b) values (@a,@b)";
cmd.Parameters. Add("@a",SqlDbT ype.Int).Value = 1;
cmd.Parameters. Add("@b",SqlDbT ype.Varchar,20) .Value = myTextBox.Text;
cmd.ExecuteNonQ uery();
cn.Close();
Ignoring, of course, validation in the textbox, error handling, the exact
schema of your database, etc. But this should give you the basic direction.
--Bob
"Ricardo Luceac" <r_******@hotma il.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Ok, thx, but if I want to insert the text of a textbox, will this
works???
cmd.CommandText = "insert into fred (name) values
("+txtName.Text .ToString()+")" ;
Am I right???
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!