473,545 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insert sql string

How can I make a insert sql string to insert direct in a table(not a
dataset) using the textbox.text propert??

Nov 16 '05 #1
10 11170
This is pretty close. You may also be able to do cn.Execute sqlstring.
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 (1,'a')";
cmd.ExecuteNonS calar();
cn.Close();

Nov 16 '05 #2
hi
you can also use
cmd.ExecuteNonQ uery()

regards
Ansil
Trivandrum

"firebalrog " wrote:
This is pretty close. You may also be able to do cn.Execute sqlstring.
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 (1,'a')";
cmd.ExecuteNonS calar();
cn.Close();

Nov 16 '05 #3
He meant ExecuteNonQuery (). No such thing as ExecuteNonScala r(). ;-)

--Bob

"Ansil MCAD" <An*******@disc ussions.microso ft.com> wrote in message
news:E5******** *************** ***********@mic rosoft.com...
hi
you can also use
cmd.ExecuteNonQ uery()

regards
Ansil
Trivandrum

"firebalrog " wrote:
This is pretty close. You may also be able to do cn.Execute sqlstring.
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 (1,'a')";
cmd.ExecuteNonS calar();
cn.Close();

Nov 16 '05 #4
hi
you can also use
cmd.ExecuteNonQ uery()

regards
Ansil
Trivandrum

"firebalrog " wrote:
This is pretty close. You may also be able to do cn.Execute sqlstring.
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 (1,'a')";
cmd.ExecuteNonS calar();
cn.Close();

Nov 16 '05 #5
He meant ExecuteNonQuery (). No such thing as ExecuteNonScala r(). ;-)

--Bob

"Ansil MCAD" <An*******@disc ussions.microso ft.com> wrote in message
news:E5******** *************** ***********@mic rosoft.com...
hi
you can also use
cmd.ExecuteNonQ uery()

regards
Ansil
Trivandrum

"firebalrog " wrote:
This is pretty close. You may also be able to do cn.Execute sqlstring.
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 (1,'a')";
cmd.ExecuteNonS calar();
cn.Close();

Nov 16 '05 #6


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!
Nov 16 '05 #7


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!
Nov 16 '05 #8
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!

Nov 16 '05 #9
Ricardo Luceac <r_******@hotma il.com> wrote:
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???


Well, that will work in some situations. In others, it could do
horrible things to your database, if people put dodgy things into the
text box. You should use command parameters.

See http://www.pobox.com/~skeet/csharp/faq/#db.parameters

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2503
by: Howard Hinnant | last post by:
I recently asked for a survey of multimap insert with hint behavior, in support of a paper I'm writing concerning lwg issue 233. My sincere thanks to Beman Dawes, Raoul Gough, Russell Hind, Bronek Kozicki, Nicola Musatti, John Potter and Maxim Yegorushkin for helping with that survey. Since I started work on this paper at least two people I...
7
14756
by: tano | last post by:
Hello, I have to insert a char in the middle of a string, I have written two functions but I don't know what is the better? The problem is: if I use malloc() I copy all the string with the new char in the middle every time, with realloc() the part of the string before the position where the char has to be inserted is not changed if realloc...
7
6651
by: kosta | last post by:
hello! one of my forms communicates with a database, and is supposed to add a row to a table using an Insert statement... however, I get a 'oledb - syntax error' exception... I have double checked, and the insert works fine (tried to use it from access)... im using visual C# express 2k5... what could be wrong? thanks!
11
22994
by: Chris Fink | last post by:
I have setup an Oracle table which contains a blob field. How do I insert data into this field using C# and ADO.net?
20
5600
by: Guadala Harry | last post by:
In an ASCX, I have a Literal control into which I inject a at runtime. litInjectedContent.Text = dataClass.GetHTMLSnippetFromDB(someID); This works great as long as the contains just client-side HTML, CSS, etc. What I want to do is somehow insert a *server control* into the , then set the server control's properties at runtime.
3
3432
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all the necessary records in it when testing it. I get the error "No value given for one or more required parameters." when I try to update the...
2
2392
by: Polyhedron_12 | last post by:
I am having problems calling functions in general in VB. I keep getting alot of errors. Can anybody help me out with this? I put the error message on the same line that it says it is at. I believe that I am not calling the function correctly. The MyInsertMethod function is the function that comes in the Web Matrix Toolbox <%@ Page...
3
5147
by: mahajanvit | last post by:
Hi one and all I got this problem during my project. So in order to solve this I made a very small application. I am trying to insert using SP and sqldatasource control. I know that while using sqldatasource control, there is no need of opening and closing a connection. Also there is no need to write connection string. When i am selecting...
6
3444
by: rn5a | last post by:
During registration, users are supposed to enter the following details: First Name, Last Name, EMail, UserName, Password, Confirm Password, Address, City, State, Country, Zip & Phone Number. I am using MS-Access 2000 database table for this app. Note that the datatype of all the fields mentioned above are Text. Apart from the above columns,...
0
2138
ak1dnar
by: ak1dnar | last post by:
There is a Error getting while i am entering records using this jsp file. <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ include file="../Connections/conn.jsp" %> <% // *** Edit Operations: declare variables // set the form action variable String MM_editAction =...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7419
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7756
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1879
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
703
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.