473,407 Members | 2,314 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,407 software developers and data experts.

Best Practice for Inserting Data into SQL in 2.0

I have a simple form. I would like to insert the values from the form
into a SQL table. What is the best way to do it?
I assume that using a stored procedure is preferable to using the
UpdateCommand="Insert into..."
When using a stored procedure, is it better to use a SqlDataSource or
an ObjectDataSource? Is it better to make it formview and use
asp:Parameter or not put it in a formview and use asp:FormParameter, or
is there a better way?

Apr 13 '06 #1
2 1710
I think the best way is to use a stored procedure with an SqlCommand object.
It's faster and safer.
Suppose your stored proc looks like this:

CREATE PROC [your_procedure_name]
@Param1 datatype,
@Param2 datatype,
@Param3 datatype,
@Param4 datatype
AS
INSERT INTO table_name (field1, field2, field3, field4) VALUES (@Param1,
@Param2, @Param3, @Param4)
GO

Then your code should look like this:

SqlConnection oCn = new SqlConnection(yourconnectionstring);
oCn.Open();
SqlCommand oCmd = oCn.CreateCommand();

oCmd.Parameters.Add("@Param1", SqlDbType.datatype, size).Value =
"param1_value"'
oCmd.Parameters.Add("@Param2", SqlDbType.datatype, size).Value =
"param2_value"'
oCmd.Parameters.Add("@Param3", SqlDbType.datatype, size).Value =
"param3_value"'
oCmd.Parameters.Add("@Param4", SqlDbType.datatype, size).Value =
"param4_value"'

oCmd.ExecuteNonQuery();
oCn.Close()

oCn.Dispose();
oCmd.Dispose();

I also suggest to you used try...catch...finally statements.

<ho********@yahoo.com> wrote in message
news:11*********************@z34g2000cwc.googlegro ups.com...
I have a simple form. I would like to insert the values from the form
into a SQL table. What is the best way to do it?
I assume that using a stored procedure is preferable to using the
UpdateCommand="Insert into..."
When using a stored procedure, is it better to use a SqlDataSource or
an ObjectDataSource? Is it better to make it formview and use
asp:Parameter or not put it in a formview and use asp:FormParameter, or
is there a better way?

Apr 13 '06 #2
Michael -
Thanks for your response. Do you use .NET 2.0? If so, any thoughts on
my questions?

Is it better to use a SqlDataSource or an ObjectDataSource?

Is it better to make it formview and use asp:Parameter or not put it
in a formview and use asp:FormParameter?

Is there a better way?

I know how to use stored procedures. The question is where should the
SP be - and what is the best way to call it?

Apr 18 '06 #3

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

Similar topics

11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
1
by: James E | last post by:
I have a question about best practices of how to deal with lookup data from my C# apps. On a couple of occasions I have come across a problem where I have to automate inserting a record into a...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
3
by: Luminal | last post by:
greetings i'm developing an C# application using Access as database and I'm having problems inserting data containing the ' char and the " char. What is the best practice to insert this chars on...
2
by: Joe Bloggs | last post by:
I have a general question on best practice regarding data access. I have the code below, a static method defined in a class that I use in a data layer dll. The method takes a string as its...
2
by: Fraser | last post by:
Hi, I'm needing to update an xml file by inserting a new node. First I need to load the xml into a XmlDocument from file. In the first run, the file won't exist and I will have to create a...
17
by: | last post by:
I have an app that retrieves data from an Access database. At the moment I have the SQL string as a Const in my app. I understand this is not best practice. I don't want the user to have access to...
3
by: cbrown | last post by:
I am rebuilding an existing application that relies on an SQL DB. The app is a scheduling/employee management program. My question pertains to best practices in dotnet and database. I use a 3...
13
by: Alan Silver | last post by:
Hello, MSDN (amongst other places) is full of helpful advice on ways to do data access, but they all seem geared to wards enterprise applications. Maybe I'm in a minority, but I don't have those...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
0
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...
0
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...

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.