Tuesday, March 13, 2012

The login failed. Login failed for user

Hi

I am trying to capture and display a user-friendly message when my database/server is offline

In order to do the above task, I have taken my database offline and put in the following code to log and show an error has occurred.

PLEASE bear in mind the database is offline and that's what I am trying to test for

C# for RequestDetails.aspx.cs

using System.IO;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

publicpartialclass_Default : System.Web.UI.Page

{

protectedvoid Page_Load(object sender,EventArgs e)

{

try

{

Console.WriteLine("Executing the try statement.");

//throw new SqlException();

}

catch ( System.Data.SqlClient.SqlException test)

{

Console.WriteLine("{0} Caught exception #1.", test);

Exception objErr = Server.GetLastError().GetBaseException();

//string err = "Error Caught in Application_Error event\n" +

// "Error in: " + Request.Url.ToString() +

// "\nError Message:" + objErr.Message.ToString() +

// "\nStack Trace:" + objErr.StackTrace.ToString();

//EventLog.WriteEntry("Sample_WebApp", err, EventLogEntryType.Error);

////Tools.Log("sadsadsadas", err);

Response.Write("error" + test.Message +"<br>");

Tools.Log("log test ", test); //Logs error

Tools.SendEmail("Email test ", test); /Emails Error

}

}

When I view the page I get

Exception Details:System.Data.SqlClient.SqlException: Cannot open database "CRM" requested by the login. The login failed.
Login failed for user 'ABCDXXXSYSTEMS\ysmith'.

Can anyone tell me how to trap and action database/server offline errors please? I know I am missing a big part of a puzzle

Thanks in advance

Try the following:

protectedvoid Page_Load(object sender,EventArgs e)

{

try

{

SqlConnection Cn =newSqlConnection("connection string here");

Cn.Open();

}

catch (SqlException Ex)

{

Response.Write (

"Database is unavailable.");

}

}


What I would like to do is

publicvoid Page_Error(object sender,EventArgs e)

{

Exception objError = Server.GetLastError().GetBaseException();string strError ="<b>Error Has Been Caught in Page_Error event</b><hr><br>" +"<br><b>Error in: </b>" + Request.Url.ToString() +"<br><b>Error Message: </b>" + objError.Message.ToString() +"<br><b>Stack Trace:</b><br>" +

objError.StackTrace.ToString();

Session["ErrorMessage"] ="Cannot open CRM";

Server.ClearError();

Response.Redirect("error.aspx?ErrorMessage" + Session["ErrorMessage"].ToString());

}

in the error page display the Session["ErrorMessage"]

protectedvoid Page_Load(object sender,EventArgs e)

{

ErrorLabel.Text = Session["ErrorMessage"].ToString();}}

}

But I get error System.NullReferenceException: Object reference not set to an instance of an object.

at line:

ErrorLabel.Text = Session["ErrorMessage"].ToString();}}

thanks in advance


Try to check if session isnt null before your set the text of the ErrorLabel control.

maybe when your page loads, there isnt any error, meanwhile you are identifying the session in the page_error which didnt get executed.

protectedvoid Page_Load(object sender,EventArgs e)

{

if(Session["ErrorMessage"] != null)

{

ErrorLabel.Text = Session["ErrorMessage"].ToString();

}

}

HC


Try opening a connection to the database which would indicate connectivity or not.

Thanks guys

I did try the suggestions without much joy. The session["ErrorMessage"] = ""

Also it would be so much easier if I could read the status code.

Thanks in advance

0 comments:

Post a Comment