I have this code
protected void Page_Load(object sender, EventArgs e)
{
try
{
string connstr = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand cmdLoad = new SqlCommand(" *Connection string* ");
cmdLoad.CommandType = System.Data.CommandType.Text;
cmdLoad.Connection = conn;
SqlDataReader rd = cmdLoad.ExecuteReader();
StringBuilder resultsHtml = new StringBuilder();
while (rd.Read())
{
resultsHtml.Append("<tr class'odd gradeX'>");
resultsHtml.Append("<td>" + rd["region_location"].ToString() + "</td>");
resultsHtml.Append("</tr>");
}
resultsPlaceHolder.Controls.Add(new Literal { Text = resultsHtml.ToString() });
rd.Dispose();
conn.Close();
}
catch (Exception ex)
{
Response.Write("Cant connect to database");
throw ex;
}
}
In my web.config, I enter the correct connection string and of course it will run and it will execute the try right? However, when I change the connection string (something not in the database) I can't get the Response.Write(); to display something to the browser. How can I display something to the screen if there is an exception in the try?
Response.Redirectto display another page containing exception message stored in session state, instead of usingResponse.Writewhich will write on same page.throw exis going to interrupt your manual response. The ASP pipelines has its own error handling. Remove thethrow ex(it also should bethrow, usually - otherwise you wipe the stack strace)