asp.net Technique Question

Discussion in 'ASP .Net' started by =?Utf-8?B?Q2hhcmxpZSBEaXNvbg==?=, Feb 21, 2004.

  1. 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.
     
    =?Utf-8?B?Q2hhcmxpZSBEaXNvbg==?=, Feb 21, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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.
     
    Ismail Rajput, Feb 21, 2004
    #2
    1. Advertising

  3. you may use global.ascx for all the pages
    "Charlie Dison" <> wrote in message
    news:...
    > 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.
     
    Ismail Rajput, Feb 21, 2004
    #3
  4. =?Utf-8?B?Q2hhcmxpZSBEaXNvbg==?=

    Martha[MSFT] Guest

    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" <> wrote in message
    news:...
    > 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.
     
    Martha[MSFT], Feb 21, 2004
    #4
  5. =?Utf-8?B?Q2hhcmxpZSBEaXNvbg==?=

    Martha[MSFT] Guest

    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" <> wrote in message
    news:...
    > 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.
     
    Martha[MSFT], Feb 21, 2004
    #5
  6. 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.
     
    Yan-Hong Huang[MSFT], Feb 23, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Swersky

    ASP.NET Technique Headcheck

    Dave Swersky, Aug 13, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    331
    Dave Swersky
    Aug 13, 2003
  2. vMike
    Replies:
    3
    Views:
    409
    =?Utf-8?B?YWRvbGYgZ2FybGlj?=
    Nov 19, 2004
  3. Brian Henry
    Replies:
    7
    Views:
    3,330
    =?Utf-8?B?a2Zyb3N0?=
    Dec 16, 2005
  4. NickName
    Replies:
    2
    Views:
    337
    NickName
    Nov 7, 2006
  5. Nick Keighley

    Re: A question of programming technique in C

    Nick Keighley, Nov 5, 2008, in forum: C Programming
    Replies:
    3
    Views:
    293
    Ian Collins
    Nov 5, 2008
Loading...

Share This Page