Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Web config

Status
Not open for further replies.

JJ297

Programmer
Jun 27, 2007
30
0
0
US
Could someone tell me how to reference my datasource from web config in my code behind page. I have this displayed now which is giving out too much info:

Dim conn As New Data.SqlClient.SqlConnection("Data Source=seb2a54;Initial Catalog=SpdtLibrary;User ID=Training;Password=people")
 
Thanks for this but I'm a newbie and don't know what I should put in place of

Dim conn As New Data.SqlClient.SqlConnection("Data Source=seb2a54;Initial Catalog=SpdtLibrary;User ID=Training;Password=people")

Should I put...

Dim conn As New Data.SQLClient.SqlConnection = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString
 
I tried that now I'm getting and error:

NullReferenceExpection was unhandled by user code

object reference nto set to an instance of an object.

Use the "new" keyword to create an object instance.

Any ideas?
 
if you place at the beginning of your codebehind, you can consume the variable within each of your routines.
This is an example, the RunThis() is not complete, nor best practice.

regarding your error, i bolded the line as it "should" be

Code:
Public Class yourclass_cb
    Inherits System.Web.UI.Page

    Dim conString As String = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString

    Sub Page_Load()
        If Not Page.IsPostBack Then
             RunThis()
        End If
    End Sub

    Sub RunThis()
        [b]Dim myConnection As New SqlConnection(conString)[/b]
        Dim myCommand As New SqlCommand("sp_MyStoredProc", myConnection)
        myCommand.CommandType = CommandType.StoredProcedure       
        Dim da As New SqlDataAdapter(myCommand)
        Dim ds As New DataSet()
        da.Fill(ds)
        myConnection.Close()
    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top