468,248 Members | 1,533 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,248 developers. It's quick & easy.

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 2305
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Jan van Veldhuizen | last post: by
2 posts views Thread by altergothen | last post: by
6 posts views Thread by ewpatton | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.