hi friends
How with the application state, we can store the connection string and how to use it in other WebPages?
thanks
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Aug 25, 2011, 10:12 AM
Here is your desired code.
Application State: - We can declare variables, objects in the application state. If we assign any value in the application state it can be globally accessed in any WebPages of the current application.
Program
First add a Global Application Class to the current application.
Global.asax code
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ Application Language="VB" %>
Default.aspx code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
Default.aspx.vb code
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Application("sqlcon").open()
Application("sqlconstring") = "select * from employee"
Application("sqlcom") = New SqlCommand(Application("sqlconstring"), Application("sqlcon"))
Application("sqldataadapter") = New SqlDataAdapter(Application("sqlcom"))
Application("sqldataset") = New DataSet
Application("sqldataadapter").fill(Application("sqldataset"), "employee")
GridView1.DataSource = Application("sqldataset")
GridView1.DataMember = "employee"
GridView1.DataBind()
Application("sqlcon").close()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
End Class
Thanks
If this post helps you mark it as answer