Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
namespace RERMS.HR
{
    public partial class LateAttendance : System.Web.UI.Page
    {
        Button btnSave;
        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as SiteMaster).SaveClicked += new EventHandler(Save_Click);
            (this.Master as SiteMaster).DeleteClicked += new EventHandler(Delete_Click);
            if (!IsPostBack)
            {  LoadGridView();
            }
            if (Request.QueryString["LateAttendanceApprovalID"] == null || Request.QueryString["LateAttendanceApprovalID"].ToString() == "")
            {}
            else if (Request.QueryString["LateAttendanceApprovalID"].ToString() != null || Request.QueryString["LateAttendanceApprovalID"].ToString() != "")
            {
                LateAttendanceApprovalForEdit();
            }
 }
 private void LateAttendanceApprovalForEdit()
        {
            btnSave = this.Master.FindControl("btnSave") as Button;
            if (btnSave.Text != "Update")
            {
                var LateRepository = new LateAttendanceRepository();
                var LateApprovalDto = new LateAttendanceApprovalDto();
                LateApprovalDto.LateAttendanceApprovalID = Convert.ToString(Request.QueryString["LateAttendanceApprovalID"]);
                LateApprovalDto = LateRepository.LoadLateAttendanceForEdit(LateApprovalDto).AttendanceApproval;
                txtApproveDate.Text = Convert.ToString(LateApprovalDto.ApprovalDate);
                txtArrivingTime.Text = Convert.ToString(LateApprovalDto.ArrivingTime);
                txtRemarks.Text = Convert.ToString(LateApprovalDto.Remarks);
                ddlEmployee.SelectedValue = LateApprovalDto.Employee.EmployeeID;
                btnSave.Text = "Update";
            }
        }

        private void LoadGridView()
        {
            LateAttendanceRepository LateRepository = new LateAttendanceRepository();
            LateRepository.LoadGridView(grdLateAttendance);
          
        }
void Save_Click(object sender, EventArgs e)
        {
            if (btnSave.Text == "Update")
            {
                UpdateLateAttendance();
            }
            else if (btnSave.Text == "Save")
            {
                SaveLateAttendance();
            }
             LoadGridView();
        }
        private void SaveLateAttendance()
        {
            var LateRepository = new LateAttendanceRepository();
            var LateApprovalDto = new LateAttendanceApprovalDto();
            var Company = new CompanyDto();
            var employeeDto = new EmployeeDto();
            var departmentDto = new DepartmentDto();
            LateApprovalDto.LateAttendanceApprovalID = "LT-10";
            LateApprovalDto.ApprovalDate = Convert.ToDateTime(txtApproveDate.Text);
            LateApprovalDto.ArrivingTime = Convert.ToDateTime(txtArrivingTime.Text); LateRepository.AddLateAttendance(LateApprovalDto);
        }
When i insert data then create this exception"Object reference not set to an instance of an object." Error line 81. because btnSave cant find value Update or Save.But i cant understand that.Sorry my poor English.
Source Error:

Line 79:         void Save_Click(object sender, EventArgs e)
Line 80:         {
Line 81:             if (btnSave.Text == "Update")
Line 82:             {
Line 83:                 UpdateAttendance();

pls solute the problem
Posted
Updated 22-Oct-12 17:30pm
v3
Comments
I.explore.code 22-Oct-12 7:12am    
Put a break point on the button click event and debug it using "F10" key on your keyboard!
Ambesha 22-Oct-12 7:19am    
:)
govardhan4u 22-Oct-12 8:22am    
update aspx page code too.
senguptaamlan 22-Oct-12 9:00am    
Oh god...I'm unable to locate the question.....please, be specific while posting a question....here the guys are not working for Ur project or resolving your business logic.
joshrduncan2012 22-Oct-12 11:14am    
What is your question here? We can't infer as to what you are referring to with just the code that you have supplied?

Please see all the comments to the questions — they all are reasonable.

First of all, using the same button for those two different actions is a bad idea even if you never use them at the same time. The user should always see the visual clue on "what else is there". So, much better UI style would be having two separate buttons, but each should be enabled or disabled depending on current state; and this enable status should be updated each time the state is changed.

Now, look what you are doing! The use of immediate constants like "Save" and "Update" it totally unacceptable. Worse, you use such constant values in different parts of the code. How can you ever support it? This is nearly impossible. Imagine you want to modify one of the button names. Are you going to change it in several places and remember how they are related? And your check like if (btnSave.Text == "Update") will screw up things very well. This is not even bad coding — this is not programming at all. Rewrite the code completely. Such constants belong to resources; at least, make them explicit constants, but never ever hard-code them.

And finally, you did not show where the exception with the message "Object reference not set to an instance of an object" is thrown, but this is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: wither make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Case closed.

—SA
 
Share this answer
 
v2
Comments
Nelek 22-Oct-12 13:10pm    
Very good answer. +5
Sergey Alexandrovich Kryukov 22-Oct-12 13:24pm    
Thank you, Nelek.
--SA
Nelek 22-Oct-12 13:30pm    
You are welcome
Nilsomudra 22-Oct-12 23:31pm    
Thanks ur advice
Sergey Alexandrovich Kryukov 22-Oct-12 23:51pm    
You are welcome.
--SA
C#
void Save_Click(object sender, EventArgs e)
        {
            btnSave = this.Master.FindControl("btnSave") as Button;
            if (btnSave.Text == "Update")
            {
                UpdateLateAttendance();
            }
            else if (btnSave.Text == "Save")
            {
                SaveLateAttendance();
            }
             LoadGridView();
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900