473,734 Members | 2,788 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 6078
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
6076
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
3670
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
4337
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
12926
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
3801
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
1891
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
2218
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
2477
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
8776
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
9310
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
9236
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
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6735
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2180
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.