2016-08-06 12 views
0

基本的に私はプロファイルのみを更新するためのコードを書いています。しかし、私はこのエラーを持って、それは "オブジェクトのインスタンスに設定されていないオブジェクト参照"と言います。しかし、私はこのエラーを2日間見つけようとしていましたが、まだこのエラーを抱えていました。私はこのエラーを解決する助けてください:(おかげ..このオブジェクトの参照がオブジェクトのインスタンスに設定されていない問題を解決する

出力:

view output

Asp.net.csコード:あなたがいずれかでヌルチェックを行っていない

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

デバッグモードで起動し、行ごとにデバッグします。あなたはあなたの例外を見つけるでしょう。 –

+0

.ToString()をConvert.ToString()に変更します。 –

答えて

0

StaffIdまたはUsernameが存在しない場合は、エラーをスローします。

  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; 
関連する問題