473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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,surname,email) VALUES('Bob',
'Sly', 'b*****@yahoo.com')";

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

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

If I do it this way the values are taken literaly so the actual words
name,surname,email 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="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Inserting 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=localhost;uid=tempuser1;pwd=tempuser1;tru sted_connection=true;datab
ase=desertdollar";

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

SqlConnection connectObj = new SqlConnection(connectionStr);
SqlCommand commandObj = new SqlCommand(insertQuery,connectObj);

commandObj.Connection.Open();
commandObj.ExecuteNonQuery();
commandObj.Connection.Close();
}

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

Please make the following changes and it should work.

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

HTH
Ashish M Bhonkiya


"altergothen" <ju******@webnet.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,surname,email) VALUES('Bob',
'Sly', 'b*****@yahoo.com')";

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

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

If I do it this way the values are taken literaly so the actual words
name,surname,email 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="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Inserting 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=localhost;uid=tempuser1;pwd=tempuser1;tru sted_connection=true;datab ase=desertdollar";

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

SqlConnection connectObj = new SqlConnection(connectionStr);
SqlCommand commandObj = new SqlCommand(insertQuery,connectObj);

commandObj.Connection.Open();
commandObj.ExecuteNonQuery();
commandObj.Connection.Close();
}

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

Nov 18 '05 #2

"altergothen" <ju******@webnet.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,surname,email) VALUES('Bob',
'Sly', 'b*****@yahoo.com')";

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

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

If I do it this way the values are taken literaly so the actual words
name,surname,email 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="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Inserting 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=localhost;uid=tempuser1;pwd=tempuser1;tru sted_connection=true;datab
ase=desertdollar";

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

SqlConnection connectObj = new SqlConnection(connectionStr);
SqlCommand commandObj = new SqlCommand(insertQuery,connectObj);

commandObj.Connection.Open();
commandObj.ExecuteNonQuery();
commandObj.Connection.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,email)
VALUES(@name,@surname,@email)";

2) add parameters with the values
commandObj.Parameters.Add("@name", 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
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 ...
8
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. ...
1
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...
1
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...
2
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...
0
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...
7
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,...
6
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...
2
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. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.