asp with gloabal.asax file

Last post 08-02-2007 1:53 AM by d4dennis@inspir3. 5 replies.

Sort Posts:

  • asp with gloabal.asax file

    08-01-2007, 11:51 PM

    hi,

    i have few forms in my form.i dont want to repeat coding for database connectivity.i want to make it as standard in one file.so that in future if any changes in database i can access that file and modify it.

    im using global.aspx file here

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)

    ' Fires when the session is started

    Session("ConnectionPath") = "DRIVER={SQL server};SERVER=localhost;UID=sa;PWD=;DATABASE=mas;"

    End Sub

    now in my forms how can i access this connection path without repeatin the above code again in my dim con = new sql(...) i dont want to do like this..

  • Re: asp with gloabal.asax file

    08-02-2007, 12:16 AM

    Hi There,

    To access session, all you need to do it:

    example:

    SqlConnection scon = New SqlConnection(Session("ConnectionPath") );

    The above method is not recommended, you can store your connectionstring in web.config

    example:

    1) Add connection string to web.config

    <?xml version="1.0"?>

    <configuration>

    <.....

     

    <
    connectionStrings>

    <add name="MyConnectionString" connectionString="Data Source=localhost;Initial Catalog=DatabaseName;Persist Security Info=True;User ID=Username;Password=Password" providerName="System.Data.SqlClient"/>

    </connectionStrings>

    2) Get connectionstring from web.config

    sqlConnection sConn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);

    To secure connectionstring in web.config

    http://msdn2.microsoft.com/en-us/library/dx0f3cf2(VS.80).aspx

    DC517
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved.
  • Re: asp with gloabal.asax file

    08-02-2007, 12:39 AM

    hi,thanks

     but in con , im getting error that

    Dim con As New SqlConnection

    Dim com, com1 As New SqlCommand

    con= new SqlConnection(configurationManager.ConnectionStrings["misConnectionString"].ConnectionString);

    value of type system.settings .connection string settings collection cannot converted to string..

     

  • Re: asp with gloabal.asax file

    08-02-2007, 12:56 AM
    Answer

    Hi There,

    Example given is in C#, hence VB version should be something like this

    Dim con As New SqlConnection

    Dim com, com1 As New SqlCommand

    con= new SqlConnection(configurationManager.ConnectionStrings("misConnectionString").ConnectionString)

    Hope it helps!

    DC517
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved.
  • Re: asp with gloabal.asax file

    08-02-2007, 1:46 AM

    Thanks buddy,it works

  • Re: asp with gloabal.asax file

    08-02-2007, 1:53 AM

    No probs, its my pleasure.

    DC517
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved.
Page 1 of 1 (6 items)
Microsoft Communities
Page view counter