Using Connection Strings in asp.net 2.0



Using Connection Strings in asp.net 2.0 Using connection strings is something you should consider using, as it means that you can not only hide your database credentials where no one can access them, but it will also save you time if you have to make any changes.

First, you need to add some content to your 'web.config' file as detailed in this article.

Then in your aspx.vb file you need to add the below at the top of the page:

At the very top of the page insert the following at the very top of the page:
Imports System.Data
Imports System.Data.SQLClient
Then in your sub add the below:
Dim MyConnection As SQLConnection
MyConnection = New SqlConnection()
MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
An example of of this in use:
Private Sub Page_Load(sender as Object, e as EventArgs)
 If Not Page.IsPostBack then
  BindData()
 End If
End Sub
Private Sub BindData()
 Dim MyConnection As SQLConnection
 MyConnection = New SqlConnection()
 MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
 Dim sql As String
 sql="SELECT * FROM table_name"
 Dim resultsDataSet as New DataSet()
 Dim myDataAdapter as SqlDataAdapter =
 New SqlDataAdapter(sql, MyConnection)
 myDataAdapter.Fill(resultsDataSet)
 DataGrid1.DataSource = resultsDataSet
 DataGrid1.DataBind()
End Sub


Send a link to this page via email Send a link to this page via email