1
votes

i have been designing a web page in asp.net and my project is successful. and i published my web application.However, below error occurs.How can I clear it?

Server Error in '/sarav' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web >request. Please review the stack trace for more information about the error and where it >originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: An unhandled exception was generated during the execution of the current web request. >Information regarding the origin and location of the exception can be identified using >the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.] Login.btnLogin_Click(Object sender, EventArgs e) +155 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent (String eventArgument) +7 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746

How to clear errorenter image description here

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {

        SqlDataAdapter da = new SqlDataAdapter();
        SqlConnection Cnn = new SqlConnection();
        DataSet ds = new DataSet();
        string constr = null;

        SqlCommand cmd = new SqlCommand();
        if (IsValid != null)
        {
            constr = ConfigurationManager.ConnectionStrings["PhotostudioConnectionString"].ConnectionString;
            Cnn.ConnectionString = constr;
            try
            {
                if (Cnn.State != ConnectionState.Open)
                    Cnn.Open();
            }
            catch (Exception ex)
            {
                string str1 = null;
                str1 = ex.ToString();
            }
            cmd.Connection = Cnn;

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.CommandText = "UspGetDataUsrmast";

            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@usrname", txtUsername.Text);
            cmd.Parameters.AddWithValue("@passwrd", txtPassword.Text);

            da.SelectCommand = cmd;
            try
            {
                da.Fill(ds);

               //  cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                string  strErrMsg =  ex.Message;

                //throw new ApplicationException("!!! An Error Occurred While Inserting Record. " + ex.Message);

            }
            finally
            {
                da.Dispose();
                cmd.Dispose();
                Cnn.Close();
                Cnn.Dispose();
            }

            if (ds.Tables[0].Rows.Count > 0)
            {
               Msg.Text = "Login Successfully";
               // da.Dispose();
               // cmd.Dispose();
               // Cnn.Close();
               // Server.Transfer("Home.aspx");
               Response.Redirect("PhotosettingsReport.aspx");
            }
            else
            {
                Msg.Text = "Login Failed";
            }
        }
    }
}
1
show some code, so you might get help.adt
if with clear you mean how to fix this error, then just to let you know that it happening because somewhere in your source code you are using a variable which is not initialized yet and it must be something in btnLogin_Click event of your login pageWaqas
You have a null pointer exception there and the best way to "clear" the error is to remove the bug from your app ;)Icarus
Post your btnLogin_Click code, please.Tim
Please share some of the code in aspx.cs file of the web page you are having error in.And please make sure you share Login.btnLogin_Click.Bastardo

1 Answers

1
votes

Without seeing the code, it's impossible to even guess as to what object is throwing this error.

Run the website in Visual Studio in debug mode (F5), and put a breakpoint at btnLogin_Click. When you click the button, it will break and allow you to step into the code. Step through the code until you find the line that is throwing the exception - that is the object that is null.

You're either trying to access something declared in the method that has not been initialized, or an object outside the method that has not been initialized.

Stepping through the code will quickly show you what's wrong.

UPDATE

I'd check txtUserName and txtPassword to be sure they have values. Also, are you sure you're getting results into the ds.Tables[0]? Calling Rows.Count on ds.Tables[0] could also result in that error if there are no tables.

Also, check your Web.config file - make sure that "PhotostudioConnectionString" is spelled correctly, including the case of the letters.

Why are you checking for IsValid != null? I think if (!IsValid) would be more proper. IsValid will either be true or false, it will never be null, so your code will always pass that check.