473,385 Members | 1,712 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,385 software developers and data experts.

INSER at runtime?

Hi!

Can anybody tell me what I'm missing here?

I'm trying to insert a new record into a sql-database from code at
runtime (not stored procedure),
but get the following error message:

"The 'strUn' is not permitted in this context.
Only constants, expressions or variables allowed here.
Column names are not permitted."

//Create sql connection
SqlConnection con = new SqlConnection
("server=LocalHost;database=Users;uid=geir;pwd=gei r");

//Open database connection
con.Open();

//Create variables to hold values from textboxes
string strUn = txtUsername.Text;
string strPw = txtPassword.Text;

//Create a sqlCommand to insert textbox values into sql-database
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandText = "INSERT INTO tblUsers(Username,Password)
VALUES(strUn, strPw)";
sqlcmd.Connection = con;

try
{
sqlcmd.ExecuteNonQuery();
}
catch(SqlException ex)
{
lblInfo.Text = "ExecuteNonQuery failed because: \n" +
"\n" +
ex.Message;
}
finally
{
con.Close();
}

Oct 2 '06 #1
4 3540
Hi, Krij

You should use parameters for an SqlCommand to pass the values of some
variables to SQL Server. For more information, see the example in the
following page:
http://msdn2.microsoft.com/en-us/lib...d.prepare.aspx

Razvan

Oct 2 '06 #2
The reason you are getting the error is because you are passing C# variable
names in the SQL statement and SQL Server has no idea what those are. I
completely agree with Razvan's suggestion to use parameters. You can then
use those parameters in your SQL statement:

sqlcmd.CommandText = "INSERT INTO tblUsers(Username,Password)" +
" VALUES(@UserId, @Password)";

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Krij" <gs***@start.nowrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hi!

Can anybody tell me what I'm missing here?

I'm trying to insert a new record into a sql-database from code at
runtime (not stored procedure),
but get the following error message:

"The 'strUn' is not permitted in this context.
Only constants, expressions or variables allowed here.
Column names are not permitted."

//Create sql connection
SqlConnection con = new SqlConnection
("server=LocalHost;database=Users;uid=geir;pwd=gei r");

//Open database connection
con.Open();

//Create variables to hold values from textboxes
string strUn = txtUsername.Text;
string strPw = txtPassword.Text;

//Create a sqlCommand to insert textbox values into sql-database
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandText = "INSERT INTO tblUsers(Username,Password)
VALUES(strUn, strPw)";
sqlcmd.Connection = con;

try
{
sqlcmd.ExecuteNonQuery();
}
catch(SqlException ex)
{
lblInfo.Text = "ExecuteNonQuery failed because: \n" +
"\n" +
ex.Message;
}
finally
{
con.Close();
}

Oct 2 '06 #3
Hi!

Thanks for answering. Maybe it is the wrong forum to bring up this
question?

But you did both point out a very important idea for me. This was only
a training case for writing code rather than using the built in toolbox
components in C#.

Of course I'll follow your guidance in a real time app.

:-)
Dan Guzman wrote:
The reason you are getting the error is because you are passing C# variable
names in the SQL statement and SQL Server has no idea what those are. I
completely agree with Razvan's suggestion to use parameters. You can then
use those parameters in your SQL statement:

sqlcmd.CommandText = "INSERT INTO tblUsers(Username,Password)" +
" VALUES(@UserId, @Password)";

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Krij" <gs***@start.nowrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hi!

Can anybody tell me what I'm missing here?

I'm trying to insert a new record into a sql-database from code at
runtime (not stored procedure),
but get the following error message:

"The 'strUn' is not permitted in this context.
Only constants, expressions or variables allowed here.
Column names are not permitted."

//Create sql connection
SqlConnection con = new SqlConnection
("server=LocalHost;database=Users;uid=geir;pwd=gei r");

//Open database connection
con.Open();

//Create variables to hold values from textboxes
string strUn = txtUsername.Text;
string strPw = txtPassword.Text;

//Create a sqlCommand to insert textbox values into sql-database
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandText = "INSERT INTO tblUsers(Username,Password)
VALUES(strUn, strPw)";
sqlcmd.Connection = con;

try
{
sqlcmd.ExecuteNonQuery();
}
catch(SqlException ex)
{
lblInfo.Text = "ExecuteNonQuery failed because: \n" +
"\n" +
ex.Message;
}
finally
{
con.Close();
}
Oct 3 '06 #4
Maybe it is the wrong forum to bring up this
question?
Questions directly related to using SQL Server APIs are appropriate here.
In cases like this, it's difficult to know where the problem may be since
the symptom is a SQL Server error message.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Krij" <gs***@start.nowrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hi!

Thanks for answering. Maybe it is the wrong forum to bring up this
question?

But you did both point out a very important idea for me. This was only
a training case for writing code rather than using the built in toolbox
components in C#.

Of course I'll follow your guidance in a real time app.

:-)
Dan Guzman wrote:
>The reason you are getting the error is because you are passing C#
variable
names in the SQL statement and SQL Server has no idea what those are. I
completely agree with Razvan's suggestion to use parameters. You can
then
use those parameters in your SQL statement:

sqlcmd.CommandText = "INSERT INTO tblUsers(Username,Password)" +
" VALUES(@UserId, @Password)";

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Krij" <gs***@start.nowrote in message
news:11**********************@m73g2000cwd.googleg roups.com...
Hi!

Can anybody tell me what I'm missing here?

I'm trying to insert a new record into a sql-database from code at
runtime (not stored procedure),
but get the following error message:

"The 'strUn' is not permitted in this context.
Only constants, expressions or variables allowed here.
Column names are not permitted."

//Create sql connection
SqlConnection con = new SqlConnection
("server=LocalHost;database=Users;uid=geir;pwd=gei r");

//Open database connection
con.Open();

//Create variables to hold values from textboxes
string strUn = txtUsername.Text;
string strPw = txtPassword.Text;

//Create a sqlCommand to insert textbox values into sql-database
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandText = "INSERT INTO tblUsers(Username,Password)
VALUES(strUn, strPw)";
sqlcmd.Connection = con;

try
{
sqlcmd.ExecuteNonQuery();
}
catch(SqlException ex)
{
lblInfo.Text = "ExecuteNonQuery failed because: \n" +
"\n" +
ex.Message;
}
finally
{
con.Close();
}

Oct 3 '06 #5

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

Similar topics

10
by: Lauren Wilson | last post by:
Ok I have searched the MS website for info on this. I am totally confused. If I want to deploy an Access 2003 app and allow my users to run it using Access 2003 Runtime, where do I get the...
10
by: MLH | last post by:
I am concerned. I have recently moved to A97 from Access 2.0 where I rolled out runtime apps with the ADT. Now, the Office Pro Developer's Edition with Access 97 is what I'm using. I find some...
17
by: Owen Jenkins | last post by:
I have an Access application that is being used by 150+ clients. I develop in 97, convert to 2000 and distribute as a 97 or 2000 mde, or 97 runtime. This limits me to 97 functions. My clients may...
8
by: Subra Mallampalli | last post by:
Hi, I am trying to use <runtime> section within the web.config file. However, the contents of the <runtime> section seem to be ignored. What am i missing here? Is <runtime> section not used by...
1
by: flash | last post by:
Need hlep to delete array only because all function is ok (inser, sort andsearch) #include <iostream.h> //functions prototype void Insert(int,int); void Delete(int,int); int...
2
by: wugon.net | last post by:
Problem: after inser trigger encounter error sql0348 Env:db2 v8 + fp 13 + win xp Description: we build two after insert triggers DB2.TRG1, DB2.TRG2 on base table DB2.TEST1, insert data into...
3
by: =?Utf-8?B?R3JhaGFt?= | last post by:
I've added 2 tracking services to the wf runtime; one is the standard SqlTrackingService: trackingService = new SqlTrackingService(<trackingConnectionString>); <workflow...
6
by: SMcK | last post by:
I have a PDA-based (Syware Visual CE) database which I need to sync to an Access database. The Access database contains three tables: 1 is the data itself, 2 is a linked table that prefills...
2
by: alivip | last post by:
when I wont to inser (anyting I print) to the textbox it will not inser it just print then hanging # a look at the Tkinter Text widget # use ctrl+c to copy, ctrl+x to cut selected text, #...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.