Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How can I use a connection string stored in the web.config file

How can I use a connection string stored in the web.config file

Scheduled Pinned Locked Moved ASP.NET
questiondatabasesql-serversysadmin
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Big Ralph

    How can I use a connection string stored in the web.config file to get a datatable from the sql server 2005 ? thx in advance. BiG RaLpH

    A Offline
    A Offline
    alexfromto
    wrote on last edited by
    #2

    Hi. Your connection string of course might be different but here's what you do with that. Put the following inside <configuration></configuration> right after let's say </system.web> (Make sure it doesnt go inside system.web node) <appSettings> <add key="SQLConString" value="packet size=4096;data source=SERVER;persist security info=True;user id=USERUD;password=PASSWORD;initial catalog=DATABASE"/> </appSettings> In your code do the following to get connection: Dim sConn As String sConn = ConfigurationSettings.AppSettings("SQLConString") Dim con As New System.Data.SqlClient.SqlConnection(sConn) con.Open() .... .... .... con.Close() That's it. -- modified at 9:20 Wednesday 7th November, 2007

    B 1 Reply Last reply
    0
    • A alexfromto

      Hi. Your connection string of course might be different but here's what you do with that. Put the following inside <configuration></configuration> right after let's say </system.web> (Make sure it doesnt go inside system.web node) <appSettings> <add key="SQLConString" value="packet size=4096;data source=SERVER;persist security info=True;user id=USERUD;password=PASSWORD;initial catalog=DATABASE"/> </appSettings> In your code do the following to get connection: Dim sConn As String sConn = ConfigurationSettings.AppSettings("SQLConString") Dim con As New System.Data.SqlClient.SqlConnection(sConn) con.Open() .... .... .... con.Close() That's it. -- modified at 9:20 Wednesday 7th November, 2007

      B Offline
      B Offline
      Big Ralph
      wrote on last edited by
      #3

      Thx Now how can I get the table in that database lets name the table "Tabl1". I want to copy it and put it in a instance of the table. i.e.: dim table as datatable "table = Table1" thx BiG RaLpH

      A V 2 Replies Last reply
      0
      • B Big Ralph

        Thx Now how can I get the table in that database lets name the table "Tabl1". I want to copy it and put it in a instance of the table. i.e.: dim table as datatable "table = Table1" thx BiG RaLpH

        A Offline
        A Offline
        alexfromto
        wrote on last edited by
        #4

        Use connection to connect to database. Create datagrid, dataset and dataadapter. Fill your dataset using dataadapter and assing dataset to your datagrid. NOTE: dg is the datagrid on your page. You can AutoGenerateColumns(True or False). If you do just bind dataset to datagrid and that's it. If you want to play around you will have to built custom columns for your datagrid. 'Open connection con.Open() Dim strSQL As String = "SELECT * FROM Tabl1" 'Create command and assign timeout value Dim cmdSelect As New System.Data.SqlClient.SqlCommand(strSQL, con) 'Create data set Dim ds As New DataSet 'Created adapter and fill dataset Dim da As New System.Data.SqlClient.SqlDataAdapter(cmdSelect) da.Fill(ds, "ANYTABLENAME") 'Verify if data exists If ds.Tables(0).Rows.Count > 0 Then 'Create dataview and assign sort by field and direction Dim dv As DataView = ds.Tables("ANYTABLENAME").DefaultView dg.DataSource = dv 'Bind data to datagrid dg.DataBind() End If 'Close connection con.Close()

        B 1 Reply Last reply
        0
        • B Big Ralph

          Thx Now how can I get the table in that database lets name the table "Tabl1". I want to copy it and put it in a instance of the table. i.e.: dim table as datatable "table = Table1" thx BiG RaLpH

          V Offline
          V Offline
          VenkataRamesh
          wrote on last edited by
          #5

          Dim sConn As String sConn = ConfigurationSettings.AppSettings("SQLConString") Dim con As New System.Data.SqlClient.SqlConnection(sConn) Dim selectCMD As SqlCommand = New SqlCommand("SELECT * FROM Customers", con) selectCMD.CommandTimeout = 30 Dim custDA As SqlDataAdapter = New SqlDataAdapter custDA.SelectCommand = selectCMD con.Open() Dim custDT As DataTable = New DataTable custDA.Fill(custDT, "Customers") con.Close() Now u have customers table in custDT .. Regards, Ramesh.

          B 2 Replies Last reply
          0
          • A alexfromto

            Use connection to connect to database. Create datagrid, dataset and dataadapter. Fill your dataset using dataadapter and assing dataset to your datagrid. NOTE: dg is the datagrid on your page. You can AutoGenerateColumns(True or False). If you do just bind dataset to datagrid and that's it. If you want to play around you will have to built custom columns for your datagrid. 'Open connection con.Open() Dim strSQL As String = "SELECT * FROM Tabl1" 'Create command and assign timeout value Dim cmdSelect As New System.Data.SqlClient.SqlCommand(strSQL, con) 'Create data set Dim ds As New DataSet 'Created adapter and fill dataset Dim da As New System.Data.SqlClient.SqlDataAdapter(cmdSelect) da.Fill(ds, "ANYTABLENAME") 'Verify if data exists If ds.Tables(0).Rows.Count > 0 Then 'Create dataview and assign sort by field and direction Dim dv As DataView = ds.Tables("ANYTABLENAME").DefaultView dg.DataSource = dv 'Bind data to datagrid dg.DataBind() End If 'Close connection con.Close()

            B Offline
            B Offline
            Big Ralph
            wrote on last edited by
            #6

            thx alot but I'm having the error that con has not been initialized. BiG RaLpH

            1 Reply Last reply
            0
            • V VenkataRamesh

              Dim sConn As String sConn = ConfigurationSettings.AppSettings("SQLConString") Dim con As New System.Data.SqlClient.SqlConnection(sConn) Dim selectCMD As SqlCommand = New SqlCommand("SELECT * FROM Customers", con) selectCMD.CommandTimeout = 30 Dim custDA As SqlDataAdapter = New SqlDataAdapter custDA.SelectCommand = selectCMD con.Open() Dim custDT As DataTable = New DataTable custDA.Fill(custDT, "Customers") con.Close() Now u have customers table in custDT .. Regards, Ramesh.

              B Offline
              B Offline
              Big Ralph
              wrote on last edited by
              #7

              thx alot but I'm having the error that con has not been initialized. BiG RaLpH

              V 1 Reply Last reply
              0
              • B Big Ralph

                thx alot but I'm having the error that con has not been initialized. BiG RaLpH

                V Offline
                V Offline
                VenkataRamesh
                wrote on last edited by
                #8

                are you running on a sql Server? If so shouldn't you do any authentication? User Id=;Password=; check out the connection string in web.config

                B 1 Reply Last reply
                0
                • V VenkataRamesh

                  are you running on a sql Server? If so shouldn't you do any authentication? User Id=;Password=; check out the connection string in web.config

                  B Offline
                  B Offline
                  Big Ralph
                  wrote on last edited by
                  #9

                  No I don't have any ID or Password this is y I'm amazed. is there anything else that might go wrong that may coz this error. BiG RaLpH

                  V 1 Reply Last reply
                  0
                  • B Big Ralph

                    No I don't have any ID or Password this is y I'm amazed. is there anything else that might go wrong that may coz this error. BiG RaLpH

                    V Offline
                    V Offline
                    VenkataRamesh
                    wrote on last edited by
                    #10

                    how r u connecting to sqlserver? windows authentication.... just paste the connection string and ur code.. i will let u know any prob's in that..

                    1 Reply Last reply
                    0
                    • V VenkataRamesh

                      Dim sConn As String sConn = ConfigurationSettings.AppSettings("SQLConString") Dim con As New System.Data.SqlClient.SqlConnection(sConn) Dim selectCMD As SqlCommand = New SqlCommand("SELECT * FROM Customers", con) selectCMD.CommandTimeout = 30 Dim custDA As SqlDataAdapter = New SqlDataAdapter custDA.SelectCommand = selectCMD con.Open() Dim custDT As DataTable = New DataTable custDA.Fill(custDT, "Customers") con.Close() Now u have customers table in custDT .. Regards, Ramesh.

                      B Offline
                      B Offline
                      Big Ralph
                      wrote on last edited by
                      #11

                      the connection string is: My code is the code u gave me: Dim sConn As String sConn = System.Configuration.ConfigurationManager.AppSettings("NameofConnection") Dim con As New System.Data.SqlClient.SqlConnection con.ConnectionString = sConn Dim selectCMD As SqlCommand = New SqlCommand("SELECT * FROM Table$", con) selectCMD.CommandTimeout = 30 Dim custDA As SqlDataAdapter = New SqlDataAdapter custDA.SelectCommand = selectCMD con.Open() Dim custDT As DataTable = New DataTable custDA.Fill(custDT) con.Close() the .fill doesn't take the 2 parameters I'm using asp.net 2.0 (maybe there is a difference) BiG RaLpH

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups