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 to get a datatable from the sql server 2005 ? thx in advance. BiG RaLpH
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 -
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 -
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
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()
-
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
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.
-
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()
-
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.
-
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
-
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
-
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
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..
-
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.
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