473,549 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=LocalH ost;database=Us ers;uid=geir;pw d=geir");

//Open database connection
con.Open();

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

//Create a sqlCommand to insert textbox values into sql-database
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandT ext = "INSERT INTO tblUsers(Userna me,Password)
VALUES(strUn, strPw)";
sqlcmd.Connecti on = con;

try
{
sqlcmd.ExecuteN onQuery();
}
catch(SqlExcept ion ex)
{
lblInfo.Text = "ExecuteNonQuer y failed because: \n" +
"\n" +
ex.Message;
}
finally
{
con.Close();
}

Oct 2 '06 #1
4 3546
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.CommandT ext = "INSERT INTO tblUsers(Userna me,Password)" +
" VALUES(@UserId, @Password)";

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Krij" <gs***@start.no wrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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=LocalH ost;database=Us ers;uid=geir;pw d=geir");

//Open database connection
con.Open();

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

//Create a sqlCommand to insert textbox values into sql-database
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandT ext = "INSERT INTO tblUsers(Userna me,Password)
VALUES(strUn, strPw)";
sqlcmd.Connecti on = con;

try
{
sqlcmd.ExecuteN onQuery();
}
catch(SqlExcept ion ex)
{
lblInfo.Text = "ExecuteNonQuer y 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.CommandT ext = "INSERT INTO tblUsers(Userna me,Password)" +
" VALUES(@UserId, @Password)";

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Krij" <gs***@start.no wrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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=LocalH ost;database=Us ers;uid=geir;pw d=geir");

//Open database connection
con.Open();

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

//Create a sqlCommand to insert textbox values into sql-database
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandT ext = "INSERT INTO tblUsers(Userna me,Password)
VALUES(strUn, strPw)";
sqlcmd.Connecti on = con;

try
{
sqlcmd.ExecuteN onQuery();
}
catch(SqlExcept ion ex)
{
lblInfo.Text = "ExecuteNonQuer y 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.no wrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.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.Command Text = "INSERT INTO tblUsers(Userna me,Password)" +
" VALUES(@UserId, @Password)";

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Krij" <gs***@start.no wrote in message
news:11******* *************** @m73g2000cwd.go oglegroups.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=LocalH ost;database=Us ers;uid=geir;pw d=geir");

//Open database connection
con.Open();

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

//Create a sqlCommand to insert textbox values into sql-database
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandT ext = "INSERT INTO tblUsers(Userna me,Password)
VALUES(strUn, strPw)";
sqlcmd.Connecti on = con;

try
{
sqlcmd.ExecuteN onQuery();
}
catch(SqlExcept ion ex)
{
lblInfo.Text = "ExecuteNonQuer y 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
3012
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 Runtime? I just purchased Office 2003 Professional. Is Access 2003 Runtime included with that or not? It APPEARS that the only way I can get...
10
2204
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 unfriendly behavior. I want (need) to find out what the pro's (like you guys) are doing to circumvent some of the difficulties encountered when...
17
2046
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 use any one of Access 97, 2000, 2002, 2003 or 97 runtime. So that I'm not lingering too far behind, I'm thinking of developing in 2003, using 2000...
8
34589
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 web apps? Any help is greatly appreciated. Thanks,
1
1580
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 Search(int,int); void Sort(int,int);
2
3349
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 DB2.TEST1 encounter error sql0348 but we re-create trigger DB2.TRG2 before DB2.TRG1 and re-insert data
3
2508
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 Runtime>.AddService(trackingService); trackingService.IsTransactional = false; trackingService.UseDefaultProfile = true; This works just fine.
6
4217
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 several fields in the main data table based on the value of one of it's fields, and 3 is table the PDA database creates to sync cases. I've been using...
2
2683
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, # ctrl+v to paste, and ctrl+/ to select all # count words in a text and show the first ten items # by decreasing frequency
0
7451
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7960
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...
1
7475
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
6048
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...
1
5372
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...
0
3501
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...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
766
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.