473,756 Members | 2,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Syntax problem Inserting variables into database

Hi there

I am a newbie to ASP.Net - Please Help!
I am trying to insert the values of my variables into a database.
If I try the following it works perfectly:
string insertQuery = "INSERT into test(name,surna me,email) VALUES('Bob',
'Sly', 'b*****@yahoo.c om')";

but instead of inputing the values directly, I want to insert them as
variables like so:
string insertQuery = "INSERT into test (name,surname,e mail)
VALUES(name,sur name,email)";

The problem is that SQL requires ' ' around the values like this:
string insertQuery = "INSERT into test (name,surname,e mail)
VALUES('name',' surname','email ')";

If I do it this way the values are taken literaly so the actual words
name,surname,em ail are entered into the database instead of their values?

Please can you tell me how I can insert the varibles values into my database

Maybe my code will explain things more clearly ............

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="Syst em.Data" %>
<%@ Import Namespace="Syst em.Data.SqlClie nt" %>
<html>
<head>
<title>Insertin g Data into a Database</title>
<script language="C#" runat="server">

void Page_Load()
{
string name;
name="Bob";
string surname;
surname="Sly";
string email;
email="'b*****@ yahoo.com'";
string connectionStr =
@"server=localh ost;uid=tempuse r1;pwd=tempuser 1;trusted_conne ction=true;data b
ase=desertdolla r";

string insertQuery = "INSERT into test(name,surna me,email) VALUES(name,
surname, email)";

SqlConnection connectObj = new SqlConnection(c onnectionStr);
SqlCommand commandObj = new SqlCommand(inse rtQuery,connect Obj);

commandObj.Conn ection.Open();
commandObj.Exec uteNonQuery();
commandObj.Conn ection.Close();
}

</script>
</head>
<body>
<h2>
Inserting Data into a Database
</h2>
</body>
</html>
Nov 18 '05 #1
2 2581
Hi There,

Please make the following changes and it should work.

string insertQuery = "INSERT into test (name,surname,e mail) VALUES( '" +
name +"','"+ surname + "','" + email + "')";

HTH
Ashish M Bhonkiya


"altergothe n" <ju******@webne t.za.net> wrote in message
news:BN******** ************@is .co.za...
Hi there

I am a newbie to ASP.Net - Please Help!
I am trying to insert the values of my variables into a database.
If I try the following it works perfectly:
string insertQuery = "INSERT into test(name,surna me,email) VALUES('Bob',
'Sly', 'b*****@yahoo.c om')";

but instead of inputing the values directly, I want to insert them as
variables like so:
string insertQuery = "INSERT into test (name,surname,e mail)
VALUES(name,sur name,email)";

The problem is that SQL requires ' ' around the values like this:
string insertQuery = "INSERT into test (name,surname,e mail)
VALUES('name',' surname','email ')";

If I do it this way the values are taken literaly so the actual words
name,surname,em ail are entered into the database instead of their values?

Please can you tell me how I can insert the varibles values into my database
Maybe my code will explain things more clearly ............

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="Syst em.Data" %>
<%@ Import Namespace="Syst em.Data.SqlClie nt" %>
<html>
<head>
<title>Insertin g Data into a Database</title>
<script language="C#" runat="server">

void Page_Load()
{
string name;
name="Bob";
string surname;
surname="Sly";
string email;
email="'b*****@ yahoo.com'";
string connectionStr =
@"server=localh ost;uid=tempuse r1;pwd=tempuser 1;trusted_conne ction=true;data b ase=desertdolla r";

string insertQuery = "INSERT into test(name,surna me,email) VALUES(name,
surname, email)";

SqlConnection connectObj = new SqlConnection(c onnectionStr);
SqlCommand commandObj = new SqlCommand(inse rtQuery,connect Obj);

commandObj.Conn ection.Open();
commandObj.Exec uteNonQuery();
commandObj.Conn ection.Close();
}

</script>
</head>
<body>
<h2>
Inserting Data into a Database
</h2>
</body>
</html>

Nov 18 '05 #2

"altergothe n" <ju******@webne t.za.net> wrote in message news:BN******** ************@is .co.za...
Hi there

I am a newbie to ASP.Net - Please Help!
I am trying to insert the values of my variables into a database.
If I try the following it works perfectly:
string insertQuery = "INSERT into test(name,surna me,email) VALUES('Bob',
'Sly', 'b*****@yahoo.c om')";

but instead of inputing the values directly, I want to insert them as
variables like so:
string insertQuery = "INSERT into test (name,surname,e mail)
VALUES(name,sur name,email)";

The problem is that SQL requires ' ' around the values like this:
string insertQuery = "INSERT into test (name,surname,e mail)
VALUES('name',' surname','email ')";

If I do it this way the values are taken literaly so the actual words
name,surname,em ail are entered into the database instead of their values?

Please can you tell me how I can insert the varibles values into my database

Maybe my code will explain things more clearly ............

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="Syst em.Data" %>
<%@ Import Namespace="Syst em.Data.SqlClie nt" %>
<html>
<head>
<title>Insertin g Data into a Database</title>
<script language="C#" runat="server">

void Page_Load()
{
string name;
name="Bob";
string surname;
surname="Sly";
string email;
email="'b*****@ yahoo.com'";
string connectionStr =
@"server=localh ost;uid=tempuse r1;pwd=tempuser 1;trusted_conne ction=true;data b
ase=desertdolla r";

string insertQuery = "INSERT into test(name,surna me,email) VALUES(name,
surname, email)";

SqlConnection connectObj = new SqlConnection(c onnectionStr);
SqlCommand commandObj = new SqlCommand(inse rtQuery,connect Obj);

commandObj.Conn ection.Open();
commandObj.Exec uteNonQuery();
commandObj.Conn ection.Close();
}

</script>
</head>
<body>
<h2>
Inserting Data into a Database
</h2>
</body>
</html>


You want "parameters ".

1) use as a query
string insertQuery = "INSERT into test (name,surname,e mail)
VALUES(@name,@s urname,@email)" ;

2) add parameters with the values
commandObj.Para meters.Add("@na me", name);
(etc)

This way you will have no problems with names like "O'Brien" etc.
Hans Kesting

Nov 18 '05 #3

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

Similar topics

8
3352
by: Jan van Veldhuizen | last post by:
The UPDATE table FROM syntax is not supported by Oracle. I am looking for a syntax that is understood by both Oracle and SqlServer. Example: Table1: id name city city_id 1 john newyork null
8
4336
by: tom | last post by:
I am new to SQL administration. >From a list of IDs that are the primary key in one table (i.e. Customer Table), I want to make changes in tables that use those IDs as a foreign key. Basically I want to say: If fk_ID is in list then do these statements to that record
1
4527
by: Scott Chapman | last post by:
I am working with Python (psycopg). I have HTML with embedded Python that I'm inserting into a database and it could contain any character. Single quotes, at least, must be escaped (to two single quotes, right?) before inserting it into Postgres. This poses a problem when I get the data out of the table. It could have originally contained two single quotes together and I replace them with one single quote in the unescaping process.
1
2036
by: Erica | last post by:
Hi, I have searched everywhere, and I can't seem to find the answer to my problem. I am trying to write a very simple piece of ASP code to insert a record into a field in a database. Ultimately it will be for an admin form that takes user input, but for now I just want to get the most intrinsic part working. This is the code: <% ' Declare variables Dim strURL ' The URL of this page so the form will work no matter
2
525
by: altergothen | last post by:
Hi there I am a newbie to ASP.Net - Please Help! I am trying to insert the values of my variables into a database. If I try the following it works perfectly: string insertQuery = "INSERT into test(name,surname,email) VALUES('Bob', 'Sly', 'bobsly@yahoo.com')"; but instead of inputing the values directly, I want to insert them as variables like so:
0
3006
by: pd123 | last post by:
I'm new to C# and .net and I'm trying to create a form that will register users in a sql server database. I have the following code but when I run the code I get an error " The name 'Peter' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted. Where Peter is the value entered in the textbox for firstname (fnameTBox) I'm sure the problem is something obvious but I...
7
4190
by: Ryan | last post by:
Ok.. here's my situation. I have a program that handles a database full of people. Users of the program have the ability to send out notifications to these people. Pretty standard notifications, the program tracks when notifications are sent out and who sends them. Currently these notifications are all sent via email. Now the program users also want the ability to send these notifications via standard snail mail. (I'll make a popup...
6
6003
by: ewpatton | last post by:
Good day, I've been trying to work with SQL and an Access database in order to handle custom user profiles. I haven't had any trouble reading from my database, but inserting new entries into it has been troublesome to say the least. My ASP.NET script is supposed to execute an INSERT INTO statement in order to add a user to the database. Here is a sample:
2
2143
by: kisalabs | last post by:
I have been trying to insert a new record into an access database and cannot get any of the scripts to work.. I can access the db and pull up/display records but cannot get one added. Please help. (info to be inserted comes from form) Inserting Records : <form action="a.asp" method="post"> FirstName : <input type="text" name="FirstName"><br> LastName : <input type="text" name="LastName"><br> Email : <input type="text"...
0
10046
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8723
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6542
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
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
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.