Tuesday, September 27, 2011

Connection Test Page in Asp.net

Following is a simple connection test page. At some point we need to test why the website is not working, so we can use this page. <%@ Page Language="C#" %> <%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">private void TestConnection() {
// Exampe of connection string server=DevSrv;packet size=4096;user id=test1;persist security info=True;initial catalog=Northwind;password=test2;" SqlConnection dbConnection = new SqlConnection("<Enter your connection string information here>"); // Example of sql statement SELECT COUNT(RegionID) IsPresent From Region
String sql="<Enter your sql statement here>";
int i =0; try {
dbConnection.Open();
SqlCommand cmd=new SqlCommand(sql,dbConnection); cmd.CommandType=CommandType.Text;i = (
int) (cmd.ExecuteScalar()); Response.Write("Connection Test was sucessfull");
}
catch(Exception ex) {
Response.Write(ex.Message);
}
finally {
if (dbConnection.State == ConnectionState.Open) dbConnection.Close();
}

}
protected void Button1_Click(object sender, EventArgs e) {
TestConnection();
}
</script> <html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Connection Test Page</title> </head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Test Connection"
Width="251px" />

</div>
</form> </body>
</html>

No comments:

Post a Comment

LIveCharts2: Charts for Windows and web

Charts for Windows and web including .Net MAUI  LiveCharts - LiveCharts2 (lvcharts.com)