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

Making functions and procedures in ASP.NET

In asp 3.0 I used an procedure (that I included) to save and update
articles, like this:

<%
Sub ConnObj(SQL)
set cn = server.CreateObject("ADODB.Connection")
cn.Open ConnString
cn.Execute SQL
cn.Close
Set cn = nothing
End Sub

'Just call the procedure with SQL an input:
call ConnObj(SQL)
%>
I would like to to the same thing i ASP.NET, but dont know the right way to
do it. I wonder what to do with the "parameters", since the SQL needs to be
an input?
In this procedure, I would write "Dim SQL as string = inputSQL". But I dont
know what to do with the parameters? Is there a smart way to make a
procedure that I can use to execute a SQL, or do I need to write the entire
code each time?

PS: I need a procedure I can use for different SQL's (and databasefields)...
if it is possible without making a new "spesific" procedure based on what I
need to save?
------------------------
Example:
-----------------------

Private Sub SaveArticle(inputSQL)
Dim conn As New SqlConnection(variables.ConnString)
Dim SQL as string = "insert into tblEmployee (fname,lname) values
(@fname,@lname)"
Dim cmd As New SqlCommand(SQL, conn)

Dim parameter1 As New SqlParameter("@fname", Me.txtFname.Text)
cmd.Parameters.Add(parameter1)

Dim parameter2 As New SqlParameter("@lname", Me.txtLname.Text)
cmd.Parameters.Add(parameter2)

cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
cmd.Dispose()
conn.Dispose()
End Sub


Feb 14 '06 #1
2 1196

public shared sub ExecuteRawSql(byval sql as string)
dim connection as new
SqlConnection(_ConnectionStringFromConfigOrSomethi ng)
dim command as new SqlCommand(sql, connection)
try
connection.Open()
command.ExecuteNonQuery()
finally
connection.Dispose()
command.Dispose()
end try
end sub

Of course, if you are serious about having a reusable data access layer,
consider getting the Enterprise LIbrary Data Access Application. This is a
free .NET library provided by microsoft for just what you are trying to do.

http://aspnet.4guysfromrolla.com/articles/070203-1.aspx
http://aspnet.4guysfromrolla.com/articles/030905-1.aspx
http://codebetter.com/blogs/david.ha.../09/59533.aspx
Karl
--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Øyvind Isaksen" <oy****@sorenso.no> wrote in message
news:eH**************@TK2MSFTNGP14.phx.gbl...
In asp 3.0 I used an procedure (that I included) to save and update
articles, like this:

<%
Sub ConnObj(SQL)
set cn = server.CreateObject("ADODB.Connection")
cn.Open ConnString
cn.Execute SQL
cn.Close
Set cn = nothing
End Sub

'Just call the procedure with SQL an input:
call ConnObj(SQL)
%>
I would like to to the same thing i ASP.NET, but dont know the right way
to do it. I wonder what to do with the "parameters", since the SQL needs
to be an input?
In this procedure, I would write "Dim SQL as string = inputSQL". But I
dont know what to do with the parameters? Is there a smart way to make a
procedure that I can use to execute a SQL, or do I need to write the
entire code each time?

PS: I need a procedure I can use for different SQL's (and
databasefields)... if it is possible without making a new "spesific"
procedure based on what I need to save?
------------------------
Example:
-----------------------

Private Sub SaveArticle(inputSQL)
Dim conn As New SqlConnection(variables.ConnString)
Dim SQL as string = "insert into tblEmployee (fname,lname) values
(@fname,@lname)"
Dim cmd As New SqlCommand(SQL, conn)

Dim parameter1 As New SqlParameter("@fname", Me.txtFname.Text)
cmd.Parameters.Add(parameter1)

Dim parameter2 As New SqlParameter("@lname", Me.txtLname.Text)
cmd.Parameters.Add(parameter2)

cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
cmd.Dispose()
conn.Dispose()
End Sub

Feb 14 '06 #2
Thank you, this was very good information for me!!

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:ee*************@TK2MSFTNGP09.phx.gbl...

public shared sub ExecuteRawSql(byval sql as string)
dim connection as new
SqlConnection(_ConnectionStringFromConfigOrSomethi ng)
dim command as new SqlCommand(sql, connection)
try
connection.Open()
command.ExecuteNonQuery()
finally
connection.Dispose()
command.Dispose()
end try
end sub

Of course, if you are serious about having a reusable data access layer,
consider getting the Enterprise LIbrary Data Access Application. This is a
free .NET library provided by microsoft for just what you are trying to
do.

http://aspnet.4guysfromrolla.com/articles/070203-1.aspx
http://aspnet.4guysfromrolla.com/articles/030905-1.aspx
http://codebetter.com/blogs/david.ha.../09/59533.aspx
Karl
--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Øyvind Isaksen" <oy****@sorenso.no> wrote in message
news:eH**************@TK2MSFTNGP14.phx.gbl...
In asp 3.0 I used an procedure (that I included) to save and update
articles, like this:

<%
Sub ConnObj(SQL)
set cn = server.CreateObject("ADODB.Connection")
cn.Open ConnString
cn.Execute SQL
cn.Close
Set cn = nothing
End Sub

'Just call the procedure with SQL an input:
call ConnObj(SQL)
%>
I would like to to the same thing i ASP.NET, but dont know the right way
to do it. I wonder what to do with the "parameters", since the SQL needs
to be an input?
In this procedure, I would write "Dim SQL as string = inputSQL". But I
dont know what to do with the parameters? Is there a smart way to make a
procedure that I can use to execute a SQL, or do I need to write the
entire code each time?

PS: I need a procedure I can use for different SQL's (and
databasefields)... if it is possible without making a new "spesific"
procedure based on what I need to save?
------------------------
Example:
-----------------------

Private Sub SaveArticle(inputSQL)
Dim conn As New SqlConnection(variables.ConnString)
Dim SQL as string = "insert into tblEmployee (fname,lname) values
(@fname,@lname)"
Dim cmd As New SqlCommand(SQL, conn)

Dim parameter1 As New SqlParameter("@fname", Me.txtFname.Text)
cmd.Parameters.Add(parameter1)

Dim parameter2 As New SqlParameter("@lname", Me.txtLname.Text)
cmd.Parameters.Add(parameter2)

cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
cmd.Dispose()
conn.Dispose()
End Sub


Feb 14 '06 #3

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

Similar topics

2
by: JingleBEV | last post by:
Hi all, I am trying not to use global variable to maintain data consistency. Some procedures and functions will require to pass the recordset object for processing and functions may also return...
7
by: BlueDragon | last post by:
The place where I work is moving to MS SQL Server from Lotus Notes. I have done a lot of coding in Lotus Notes, and have, I suppose, intermediate skills in basic SQL -- queries, insert, updates,...
2
by: David Emme | last post by:
Access 97 I have a number of SELECT statements which contain references to user-defined VBA functions. These typically work as expected, but occasionally, on one user's machine or another,...
6
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the...
85
by: masood.iqbal | last post by:
I know that this topic may inflame the "C language Taleban", but is there any prospect of some of the neat features of C++ getting incorporated in C? No I am not talking out the OO stuff. I am...
0
by: Zlatko Matiæ | last post by:
Hello. While I was working with Access Projects (Access front-end with MSDE) I was able to call stored procedures by using ADO command and parameters object. Now I am trying to migrate my...
3
by: Anil Gupte | last post by:
Hopefully this list is newbie-friendly. I have a conceptual questions. I am learning VB and came across the description in a book I am using that describes procedures vs. functions. I understand...
0
by: ddddd | last post by:
AM using the JDBC interface metho getProcedures() to get the stored procedures from the Database... Since this is common interface method am trying out with two different set of databases namely...
1
by: svkreddy | last post by:
Dear All, Please provide the simple information about stored procedures and stored functions in microsoft sql server2005. Please give the information about Nested procedures and nested funtions....
1
by: mansi sharma | last post by:
Functions ia a block of code that performs some task & returns value. Can somebody tell me What are Stored Procedures? I found abt Stored procedues from the Net-->Stored Procedures is a group of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.