asp.net Technique Question

G

Guest

Hi there
Lets say that I put up a form which asks to create a user id and password. I place a button on the form and in the onclick I call two separate subroutines, The first checks to see if the userid already exists, the second will create the user Id. I'm going to need to connect to the database for both operations, my question is, can I create a connection object good for both operations. I guess the real question is the scope of the connection object I create. Can I define the connection object on the form level as I would in VB6 so that the connection object is available in both subroutines? Does asp.net have anything like a VB6 module where all forms can access the same code. Thanks in advance.
 
I

Ismail Rajput

you can define it

Dim ConnectionString As String =
"server=(local);database=pubs;trusted_connection=true"

Dim SelectCommand As String = "SELECT * from Authors"

then call it anywhere in the ASP.NET page

Charlie Dison said:
Hi there,
Lets say that I put up a form which asks to create a user id and
password. I place a button on the form and in the onclick I call two
separate subroutines, The first checks to see if the userid already exists,
the second will create the user Id. I'm going to need to connect to the
database for both operations, my question is, can I create a connection
object good for both operations. I guess the real question is the scope of
the connection object I create. Can I define the connection object on the
form level as I would in VB6 so that the connection object is available in
both subroutines? Does asp.net have anything like a VB6 module where all
forms can access the same code. Thanks in advance.
 
I

Ismail Rajput

you may use global.ascx for all the pages
Charlie Dison said:
Hi there,
Lets say that I put up a form which asks to create a user id and
password. I place a button on the form and in the onclick I call two
separate subroutines, The first checks to see if the userid already exists,
the second will create the user Id. I'm going to need to connect to the
database for both operations, my question is, can I create a connection
object good for both operations. I guess the real question is the scope of
the connection object I create. Can I define the connection object on the
form level as I would in VB6 so that the connection object is available in
both subroutines? Does asp.net have anything like a VB6 module where all
forms can access the same code. Thanks in advance.
 
M

Martha[MSFT]

You can keep the connection on the form level. Just declare the connection
variable at the top and use it in both the subroutines. Connect to the
database in the first subroutine and don't close it. Use the same connection
from your second subroutine and close it in that sub.
But why do you need to call two subroutines? Why don't you do both the
checking and the adding in the db in the OnClick?

Hope this helps




Charlie Dison said:
Hi there,
Lets say that I put up a form which asks to create a user id and
password. I place a button on the form and in the onclick I call two
separate subroutines, The first checks to see if the userid already exists,
the second will create the user Id. I'm going to need to connect to the
database for both operations, my question is, can I create a connection
object good for both operations. I guess the real question is the scope of
the connection object I create. Can I define the connection object on the
form level as I would in VB6 so that the connection object is available in
both subroutines? Does asp.net have anything like a VB6 module where all
forms can access the same code. Thanks in advance.
 
M

Martha[MSFT]

You can keep the connection on the form level. Just declare the connection
variable at the top and use it in both the subroutines. Connect to the
database in the first subroutine and don't close it. Use the same connection
from your second subroutine and close it in that sub.
But why do you need to call two subroutines? Why don't you do both the
checking and the adding in the db in the OnClick?

Hope this helps


Charlie Dison said:
Hi there,
Lets say that I put up a form which asks to create a user id and
password. I place a button on the form and in the onclick I call two
separate subroutines, The first checks to see if the userid already exists,
the second will create the user Id. I'm going to need to connect to the
database for both operations, my question is, can I create a connection
object good for both operations. I guess the real question is the scope of
the connection object I create. Can I define the connection object on the
form level as I would in VB6 so that the connection object is available in
both subroutines? Does asp.net have anything like a VB6 module where all
forms can access the same code. Thanks in advance.
 
Y

Yan-Hong Huang[MSFT]

Hi,

I agree with Martha on it. Although we can declare it and use it in
different subroutines, it is not recommended. In ASP.NET programming, it is
recommended to open and close DB connection as soon as possible to release
resource. The DB connection is fetched from connection pool to improve
performance.

For more details, pleaes refer to MSDN article"
".net data access architecture guide"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/
daag.asp

This document provides guidelines for implementing an ADO.NET-based data
access layer in a multi-tier .NET-based application. It focuses on a range
of common data access tasks and scenarios, and presents guidance to help
you choose the most appropriate approaches and techniques.

Hope that helps.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top