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.reply to my e mail id.

Showing Answers 1 - 3 of 3 Answers

pankaj shama

  • Oct 19th, 2005
 

ASP.Net

  Was this answer useful?  Yes

Ranjit Srinivasan

  • Oct 21st, 2005
 

<?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

  Was this answer useful?  Yes

aruna

  • Apr 3rd, 2006
 

by specifying the connectionstring in web.config

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions