473,698 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Example for basic parameterized INSERT in asp.net 2.0?

I'm new to ASP.NET and working with the 2.0 beta. As a classic ASP
developer I have a lot of code that looks like this:

sql = "insert into x values("
sql = sql & val1 & ", " & val2 & ", ' " & strVal3 & " ', " & val4 & ")"

Which of course gets very messy. As I understand it there is a way to use a
placeholder string with question marks like "values(?, ?, ?, ?)" and then
set the value of each ? placeholder with variable names. This would relieve
the messiness caused by worrying about missing quotes and readability
issues, etc.

Can someone provide a basic code snippet that demonstrates how to do this?
I know there are all sorts of toolbar things that can be dragged and dropped
to create some of this visually. But my need is so basic I just want to
have the minimal code to create a db connection, execute an insert, and
perhaps retrieve the @@identity value associated with the inserted row.

Thanks in advance!

Steve
Nov 19 '05 #1
4 6074
You should really be using a parametised stored procedure
Something like:

SQL
CREATE PROCEDURE exampleInsert
@val1 varchar(200),
@val2 varchar(200)
AS
INSERT INTO x VALUE(@val1, @val2)

c#

SqlCommand cmd = new SqlCommand("exa mpleInsert", MyConnection);
//MyConnection must already be declared as SqlConnection
cmd.Parameters. Add(new SqlPArameters(" @val1", SqlDbType.VarCh ar,
200)).Value = val1; //val 1 would be your value
cmd.Parameters. Add(new SqlPArameters(" @val2", SqlDbType.VarCh ar,
200)).Value = val2;
MyConnection.Op en()
cmd.ExecuteNonQ uery();
MyConnection.Cl ose();

HTH
"Steve Franks" <pl****@postrep lyhere.com> wrote in message
news:cM******** ************@co mcast.com...
I'm new to ASP.NET and working with the 2.0 beta. As a classic ASP
developer I have a lot of code that looks like this:

sql = "insert into x values("
sql = sql & val1 & ", " & val2 & ", ' " & strVal3 & " ', " & val4 & ")"

Which of course gets very messy. As I understand it there is a way to use
a placeholder string with question marks like "values(?, ?, ?, ?)" and
then set the value of each ? placeholder with variable names. This would
relieve the messiness caused by worrying about missing quotes and
readability issues, etc.

Can someone provide a basic code snippet that demonstrates how to do this?
I know there are all sorts of toolbar things that can be dragged and
dropped to create some of this visually. But my need is so basic I just
want to have the minimal code to create a db connection, execute an
insert, and perhaps retrieve the @@identity value associated with the
inserted row.

Thanks in advance!

Steve

Nov 19 '05 #2
Thank you so much!. Unfortunately I cannot use stored procedures right now
but hope to down the road.

In the meantime how would I update the code snippet you provided to work
through all ASP.NET without a stored procedure?

Thank you again,

Steve

"Grant Merwitz" <gr***@workshar e.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
You should really be using a parametised stored procedure
Something like:

SQL
CREATE PROCEDURE exampleInsert
@val1 varchar(200),
@val2 varchar(200)
AS
INSERT INTO x VALUE(@val1, @val2)

c#

SqlCommand cmd = new SqlCommand("exa mpleInsert", MyConnection);
//MyConnection must already be declared as SqlConnection
cmd.Parameters. Add(new SqlPArameters(" @val1", SqlDbType.VarCh ar,
200)).Value = val1; //val 1 would be your value
cmd.Parameters. Add(new SqlPArameters(" @val2", SqlDbType.VarCh ar,
200)).Value = val2;
MyConnection.Op en()
cmd.ExecuteNonQ uery();
MyConnection.Cl ose();

HTH
"Steve Franks" <pl****@postrep lyhere.com> wrote in message
news:cM******** ************@co mcast.com...
I'm new to ASP.NET and working with the 2.0 beta. As a classic ASP
developer I have a lot of code that looks like this:

sql = "insert into x values("
sql = sql & val1 & ", " & val2 & ", ' " & strVal3 & " ', " & val4 & ")"

Which of course gets very messy. As I understand it there is a way to
use a placeholder string with question marks like "values(?, ?, ?, ?)"
and then set the value of each ? placeholder with variable names. This
would relieve the messiness caused by worrying about missing quotes and
readability issues, etc.

Can someone provide a basic code snippet that demonstrates how to do
this? I know there are all sorts of toolbar things that can be dragged
and dropped to create some of this visually. But my need is so basic I
just want to have the minimal code to create a db connection, execute an
insert, and perhaps retrieve the @@identity value associated with the
inserted row.

Thanks in advance!

Steve


Nov 19 '05 #3
been a while since i done that.
Something like:

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO x VALUE(@val1, @val2)";
cmd.Connection = MyConnection; //conn already set up
cmd.Parameters. Add(new SqlParameter("@ val1", SqlDbType.VarCh ar, 200)).Value
= val1;
cmd.Parameters. Add(new SqlParameter("@ val2", SqlDbType.VarCh ar, 200)).Value
= val2;

"Steve Franks" <pl****@postrep lyhere.com> wrote in message
news:h9******** ************@co mcast.com...
Thank you so much!. Unfortunately I cannot use stored procedures right
now but hope to down the road.

In the meantime how would I update the code snippet you provided to work
through all ASP.NET without a stored procedure?

Thank you again,

Steve

"Grant Merwitz" <gr***@workshar e.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
You should really be using a parametised stored procedure
Something like:

SQL
CREATE PROCEDURE exampleInsert
@val1 varchar(200),
@val2 varchar(200)
AS
INSERT INTO x VALUE(@val1, @val2)

c#

SqlCommand cmd = new SqlCommand("exa mpleInsert", MyConnection);
//MyConnection must already be declared as SqlConnection
cmd.Parameters. Add(new SqlPArameters(" @val1", SqlDbType.VarCh ar,
200)).Value = val1; //val 1 would be your value
cmd.Parameters. Add(new SqlPArameters(" @val2", SqlDbType.VarCh ar,
200)).Value = val2;
MyConnection.Op en()
cmd.ExecuteNonQ uery();
MyConnection.Cl ose();

HTH
"Steve Franks" <pl****@postrep lyhere.com> wrote in message
news:cM******** ************@co mcast.com...
I'm new to ASP.NET and working with the 2.0 beta. As a classic ASP
developer I have a lot of code that looks like this:

sql = "insert into x values("
sql = sql & val1 & ", " & val2 & ", ' " & strVal3 & " ', " & val4 & ")"

Which of course gets very messy. As I understand it there is a way to
use a placeholder string with question marks like "values(?, ?, ?, ?)"
and then set the value of each ? placeholder with variable names. This
would relieve the messiness caused by worrying about missing quotes and
readability issues, etc.

Can someone provide a basic code snippet that demonstrates how to do
this? I know there are all sorts of toolbar things that can be dragged
and dropped to create some of this visually. But my need is so basic I
just want to have the minimal code to create a db connection, execute an
insert, and perhaps retrieve the @@identity value associated with the
inserted row.

Thanks in advance!

Steve



Nov 19 '05 #4
Check this out - might be helpful:
How to avoid writing SQL for ADO.NET DataAdapter(By Dejan Grujic )
http://www.codeproject.com/cs/databa...andBuilder.asp
"Steve Franks" wrote:
I'm new to ASP.NET and working with the 2.0 beta. As a classic ASP
developer I have a lot of code that looks like this:

sql = "insert into x values("
sql = sql & val1 & ", " & val2 & ", ' " & strVal3 & " ', " & val4 & ")"

Which of course gets very messy. As I understand it there is a way to use a
placeholder string with question marks like "values(?, ?, ?, ?)" and then
set the value of each ? placeholder with variable names. This would relieve
the messiness caused by worrying about missing quotes and readability
issues, etc.

Can someone provide a basic code snippet that demonstrates how to do this?
I know there are all sorts of toolbar things that can be dragged and dropped
to create some of this visually. But my need is so basic I just want to
have the minimal code to create a db connection, execute an insert, and
perhaps retrieve the @@identity value associated with the inserted row.

Thanks in advance!

Steve

Nov 19 '05 #5

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

Similar topics

1
6072
by: gary b | last post by:
Hello When I use a PreparedStatement (in jdbc) with the following query: SELECT store_groups_id FROM store_groups WHERE store_groups_id IS NOT NULL AND type = ? ORDER BY group_name
7
3667
by: Wayne Wood | last post by:
i posted this problem on microsoft.public.excel.programming, but there's no one replied till now. because this issue is time critical, i put it here to try my fortune :) ------------------------------------------------------------------------ I am just a fresh man for C# programming, but i found that C# have no support for optional parameter or parameterized property, which are supported in VB.NET.
4
4335
by: Gilberto Campos | last post by:
Hi all. I am having a strange problem. I am developping an application that acceses an Access db through Jet (.UDL files). I have writen parametric INSERT queries that work fine. I am now trying to write a parametric UPDATE query but I always get a return error code that the language manual translates to:
8
12922
by: deko | last post by:
I'm trying to open a Recordset based on a parameterized query. I'm kind of new to parameterized queries, so I'm sure I'm missing something simple. Set qdfs = db.QueryDefs Set qdf = qdfs("qryInvoices") qdf.Parameters("prmInv") = strInvoice qdf.Parameters("prmCid") = lngCustomerID Set rst = db.OpenRecordset("qryInvoices")
11
3789
by: anony | last post by:
Hello, I can't figure out why my parameterized query from an ASP.NET page is dropping "special" characters such as accented quotes & apostrophes, the registered trademark symbol, etc. These symbols insert without problem from query analyzer, so that suggests it's something within ASP.NET. I've tried using .NET textbox web controls as well as html textareas. I have a test database set up with 4 fields: varchar, nvarchar, text, and...
10
1881
by: ryan.mclean | last post by:
Hi all, I am new to using sql server and parameterized sql. I am hoping to be returned the value of a column that has been inserted. Here is my statement strSqlInsetrtTrack = _ "INSERT INTO TRACK (CASE_NUM, CERT_NUM, CLMT_NUM, BROKER_NAME, " + _ "CONTRACT_SEQ, TRACK_COMMENTS, DATE_OVER_SPEC, LAST_TOUCHED) " + _ "VALUES ('" + strCase + "','" + strCert + "'," + _ "'" + strClmt + "',@BrokerName,'" + strContractSeq + "',@TrackComments," +...
0
2216
by: Tom | last post by:
Looking for some help with stored procedure call issues. Conceptually, I need to pass a data structure as the sole parameter to the Oracle stored procedure. Sounds simple enough....but how? First, looking at this from the stored procedure side, I see a few things the procedure can do: 1) receive a CLOB/BLOB 2) receive a user defined data type 3) receive multiple params, one for each distinct data element
5
1520
by: dancer | last post by:
Using Asp.net 1.1 Can somebody tell me why the code following gives this error message: Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: BC30456: 'String' is not a member of 'System.Data.OleDb.OleDbType'.
23
2471
by: =?Utf-8?B?TWlrZTE5NDI=?= | last post by:
This is an example that is supposed to work in VB http://support.microsoft.com/kb/175512/en-us After spending a couple of hours downloading and installing VB Express 2008 after someone told me it was easy, the thing doesn't work any better. These two instruction lines are not functional 3. Place a Common Dialog control on the form. 4. From the Insert menu, select Module to add a single code module to the project. There is no Common...
0
8678
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9166
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
9030
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...
1
8899
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7737
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...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
2333
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.