2016-08-06 12 views
0

Grundsätzlich schreibe ich Code, um nur das Profil zu aktualisieren. Aber ich habe diesen Fehler und es heißt "Objektreferenz nicht auf eine Instanz eines Objekts festgelegt". Aber ich habe versucht, diesen Fehler für 2 Tage zu finden, aber ich steckte immer noch auf diesen Fehler. Bitte helfen Sie mir die diesen Fehler zu beheben :(Thanks ..Problem beim Fixieren dieser Objektreferenz, die nicht auf eine Objektinstanz festgelegt wurde

Ausgang:

view output

Asp.net.cs Code:

public partial class EditProfile : System.Web.UI.Page 
    { 
     SqlConnection conn = null; 
     SqlCommand cmd = null; 
     string connectionString = null; 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
       SqlDataReader dr = null; 

       connectionString = ConfigurationManager.ConnectionStrings["LeaveManagementCS"].ConnectionString; 

       conn = new SqlConnection(connectionString); 

       string sql = "SELECT * FROM Staff"; 

       string id = Session["StaffId"].ToString(); 

       Session["StaffId"] = id; 

       try 
       { 
        cmd = new SqlCommand(sql, conn); 

        conn.Open(); 

        dr = cmd.ExecuteReader(); 

        dr.Read(); 

        id = dr["StaffId"].ToString(); 
        tbStaffName.Text = dr["StaffName"].ToString(); 
        tbPassword.Text = dr["Password"].ToString(); 
        tbEmail.Text = dr["Email"].ToString(); 
        tbPhoneNo.Text = dr["PhoneNo"].ToString(); 
        ddlTitle.SelectedItem.Text = dr["Title"].ToString(); 

        dr.Close(); 
       } 
       catch (Exception ex) 
       { 
        lblOutput.Text = "Error Message:" + ex.Message; 
       } 
       finally 
       { 
        if (conn != null) 
         conn.Close(); 
       } 
      } 
     } 

     protected void btnUpdate_Click(object sender, EventArgs e) 
     { 
      connectionString = ConfigurationManager.ConnectionStrings["LeaveManagementCS"].ConnectionString; 

      conn = new SqlConnection(connectionString); 

      string sql = "UPDATE Staff SET [email protected], [email protected], [email protected], [email protected], [email protected], [email protected] "; 
      sql += " WHERE [email protected]"; 

      string title= ddlTitle.SelectedItem.Text; 

      string id = Session["StaffId"].ToString(); 

      Session["StaffId"] = id; 

      string username = Session["Username"].ToString(); 

      Session["Username"] = username; 

      try 
      { 
       cmd = new SqlCommand(sql, conn); 

       if(username != null) 
       { 
        cmd.Parameters.AddWithValue("@username", username); 
       } 

       cmd.Parameters.AddWithValue("@id", id); 
       cmd.Parameters.AddWithValue("@staff", tbStaffName.Text); 
       cmd.Parameters.AddWithValue("@Pwd", tbPassword.Text); 
       cmd.Parameters.AddWithValue("@email", tbEmail.Text); 
       cmd.Parameters.AddWithValue("@phone", tbPhoneNo.Text); 
       cmd.Parameters.AddWithValue("@title", title); 

       conn.Open(); 

       int rows = cmd.ExecuteNonQuery(); 

       if (rows > 0) 
       { 
        lblOutput.Text = "Record update successfully"; 
       } 
      } 
      catch (Exception ex) 
      { 
       lblOutput.Text = "Error Message: " + ex.Message; 
      } 
      finally 
      { 
       if (conn != null) 
        conn.Close(); 
      } 

      try 
      { 
       SqlDSEditProfile.Update(); 
       lblOutput.Text = "Application update"; 
      } 
      catch (Exception ex) 
      { 
       lblOutput.Text = ex.Message; 
      } 
     } 
    } 
+3

im Debug-Modus starten, Zeile für Zeile debuggen. Sie werden Ihre Ausnahme finden. –

+0

Ändern Sie .ToString() in Convert.ToString(). –

Antwort

0

Sie keine Nullprüfung auf jeder tun Wenn keine StaffId oder kein Benutzername vorhanden ist, wird dieser Fehler ausgegeben. Versuchen Sie dies zuerst.

  string id = String.Empty; 
      if (Session["StaffId"] != null) 
      { 
       id = Session["StaffId"].ToString(); 
      } 
      Session["StaffId"] = id; 

      string username = String.Empty; 
      if (Session["Username"] != null) 
      { 
       username = Session["Username"].ToString(); 
      } 
      Session["Username"] = username;