0

Basically I am writing some code for update the profile only. But i got this error and it says "Object reference not set to an instance of an object". But I've been trying to find this error for 2 days but still I stuck on this error. Please help me the solve this error :( Thanks..

Output:

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 Username=@username, Password=@Pwd, StaffName=@staff, Email=@email, PhoneNo=@phone, Title=@title ";
            sql += " WHERE StaffId=@id";

            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;
            }
        }
    }
sya15
  • 1
  • 3

1 Answers1

0

You are not doing a null check on any of the Sessions. If StaffId or Username do not exists it will throw that error. Try this first.

            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;
VDWWD
  • 32,238
  • 19
  • 56
  • 70