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.
Submitted by: Administrator<?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
Submitted by: Administrator
<!-- 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
Submitted by: Administrator
Read Online ASP.NET 2.0 Job Interview Questions And Answers
Top ASP.NET 2.0 Questions
☺ | Web service support |
☺ | What types of data validation events are commonly seen in the client-side form validation? |
☺ | In A Page I have gridview with Options of select and delete using hyperlink when i am selecting any one of then it has to open another page how can it? |
☺ | How to rename a table using sql queries? |
☺ | Can a .NET web application consume Java web service? |
Top Dot Net Technologies Categories
☺ | MSF Interview Questions. |
☺ | .Net Architecture Interview Questions. |
☺ | ASP.Net MVC Interview Questions. |
☺ | Entity Framework Interview Questions. |
☺ | C# (Sharp) Programming Language Interview Questions. |