How To Create A DB Connection At One Place/page So That We Can Use That Connection For All Pages/forms/windows.what R The Steps Ned To Be Performed. If Question Not Clear,let Me Know.
Answers

Question and Answer:

  Home  ASP.NET 2.0

⟩ How to create a DB connection at one place/page so that we can use that connection for all pages/forms/windows.what r the steps ned to be performed. if question not clear,let me know.

<?xml version="1.0" encoding="utf-8" ?>

<!-- Web.Config Configuration File -->

<configuration>

<appSettings>

<add key="ConnectionString"

value="server=localhost;database=Northwind;uid=sa;password=secret;" />

</appSettings>

<system.web>

<customErrors mode="Off"/>

</system.web>

</configuration>

Imports System

Imports System.Web

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Data

Imports System.Data.SqlClient

Imports System.Configuration

Public Class ConnString : Inherits Page

Protected dataGrid As DataGrid

Protected Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)

Dim sqlConn As SqlConnection

Dim sqlCmd As SqlCommand

Dim strConnection As String

Try

'Get connection string from Web.Config

strConnection = ConfigurationSettings.AppSettings("ConnectionString")

sqlConn = New SqlConnection(strConnection)

sqlCmd = New SqlCommand("SELECT * FROM Customers WHERE " _

& "(CompanyName LIKE 'A%') OR (CompanyName LIKE 'B%')", sqlConn)

sqlConn.Open()

dataGrid.DataSource = sqlCmd.ExecuteReader()

dataGrid.DataBind()

Catch ex As Exception

Response.Write(ex.ToString & "<br>")

Finally

sqlConn.Close()

End Try

End Sub

End Class

 207 views

More Questions for you: