Object reference not set to an instance of an object.

+1 Melissa Charn · September 12, 2014
Good day ,

I found this error while modifying one of the code 
I'm a beginner for programming and I hope to get some guidelines about what is this error message means ?

Attached with the codes below :


/images/forum/upload/2014-09-12/9840e0abf9bf40ce9566439ba6c4d5f0.jpg


Post a Reply

Replies

Oldest  Newest  Rating
0 Melissa Charn · September 17, 2014
Dear Eugene,

Thank you very much for your guidance on the code
I managed to solve the problem for the time being , will need to run this program for test and observation
If I have any problem , I hope you can guide me , I am still learning 
+2 Eugene Botma · September 15, 2014
Note that in my code i added the comment "or whatever class the toolingtype property is", because i didn't know if it was simply a string or its own class. In the class it is defined as a string, so you there is no need to use this.ToolingType = new ToolingType();. Instead, you can use this.ToolingType = "NEW";

So you need to add


this.ToolingType = "NEW";

//into

public TableTOF()
{

}


But note that whenever you create an object of TableTOF, the toolingtype value will always be "NEW" by default. If it needs to be something else, you will have to change it after initialization of the object.

so for example if it needs to be "MODIFICATION", you will need to do something like this:

TableTOF sometable = new TableTOF();
sometable.ToolingType = "MODIFICATION";
//and do whatever else needs to be done.
0 Melissa Charn · September 15, 2014
Dear George ,

thank you for the suggestion , I'm trying on the method given by Eugene.

======================================================================================================

Dear Eugene,

I added : 

tblTOF.ToolingType = new ToolingType();

into 

TableTOF tblTOF = new TableTOF();
tblTOF.ToolingType = new ToolingType();
if (!PopulateTOF(tblTOF)) return;

And I received a different error :

/images/forum/upload/2014-09-15/a4e56db39ea495631012c84d9e5dcc5c.jpg
=================================================================================
 
Server Error in '/forms' Application.

Compilation Error 

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'ToolingType' could not be found (are you missing a using directive or an assembly reference?)

Source Error:

Line 839:
Line 840:        TableTOF tblTOF = new TableTOF();
Line 841:        tblTOF.ToolingType = new ToolingType();
Line 842:        if (!PopulateTOF(tblTOF)) return;
Line 843:

Source File: f:\ForWeb\WebPage\RND_04B Copy\FORMS\Form5Modify.aspx.cs    Line: 841  

==================================================================================

Is it that I have to define the Tooling  Type ? 
It is already define in else where (another different sheet of coding named : TableTOF.cs)

Code are as below :


using System;
using System.Collections;

public class TableTOF : AuthBase
{

    public TableTOF()
    { 
    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }
    
    public override bool Equals(object obj)
    {
        return true;
    }

    private int _ID;
    public int ID
    {
        get { return _ID; }
        set { _ID = value; }
    }


    private string _Section;
    public string Section
    {
        get { return _Section; }
        set { _Section = value; }
    }

    private string _ToolingType;
    public string ToolingType
    {
        get { return _ToolingType; }
        set { _ToolingType = value; }
    }

    private string _IssueDate;
    public string IssueDate
    {
        get { return _IssueDate; }
        set { _IssueDate = value; }
    }

    private string _PartName;
    public string PartName
    {
        get { return _PartName; }
        set { _PartName = value; }
    }

    private string _PartNumber;
    public string PartNumber
    {
        get { return _PartNumber; }
        set { _PartNumber = value; }
    }

    private string _DevCode;
    public string DevCode
    {
        get { return _DevCode; }
        set { _DevCode = value; }
    }

    private string _ModelName;
    public string ModelName
    {
        get { return _ModelName; }
        set { _ModelName = value; }
    }

    private string _ModelNumber;
    public string ModelNumber
    {
        get { return _ModelNumber; }
        set { _ModelNumber = value; }
    }

    private string _Dim1;
    public string Dim1
    {
        get { return _Dim1; }
        set { _Dim1 = value; }
    }

    private string _Dim2;
    public string Dim2
    {
        get { return _Dim2; }
        set { _Dim2 = value; }
    }

    private string _Dim3;
    public string Dim3
    {
        get { return _Dim3; }
        set { _Dim3 = value; }
    }

    private string _SampleQuantity;
    public string SampleQuantity
    {
        get { return _SampleQuantity; }
        set { _SampleQuantity = value; }
    }

    private string _ToolingCost;
    public string ToolingCost
    {
        get { return _ToolingCost; }
        set { _ToolingCost = value; }
    }

    private string _MRNo;
    public string MRNo
    {
        get { return _MRNo; }
        set { _MRNo = value; }
    }

    private string _MRDate;
    public string MRDate
    {
        get { return _MRDate; }
        set { _MRDate = value; }
    }

    private string _Remarks;
    public string Remarks
    {
        get { return _Remarks; }
        set { _Remarks = value; }
    }

    private string _Author;
    public string Author
    {
        get { return _Author; }
        set { _Author = value; }
    }

    private string _UniqueTime;
    public string UniqueTime
    {
        get { return _UniqueTime; }
        set { _UniqueTime = value; }
    }

   
    private string _TOFFileName;
    public string TOFFileName
    {
        get { return _TOFFileName; }
        set { _TOFFileName = value; }
    }
   
    private string _UploadFFilename;
    public string UploadFFilename
    {
        get { return _UploadFFilename; }
        set { _UploadFFilename = value; }
    }
   
    private string _TOFFileName2;
    public string TOFFileName2
    {
        get { return _TOFFileName2; }
        set { _TOFFileName2 = value; }
    }

    private string _TOFFileName3;
    public string TOFFileName3
    {
        get { return _TOFFileName3; }
        set { _TOFFileName3 = value; }
    }

    private string _TOFFileName4;
    public string TOFFileName4
    {
        get { return _TOFFileName4; }
        set { _TOFFileName4 = value; }
    }

    private string _TOFFileName5;
    public string TOFFileName5
    {
        get { return _TOFFileName5; }
        set { _TOFFileName5 = value; }
    }

    private int _NonTooling;
    public int NonTooling
    {
        get { return _NonTooling; }
        set { _NonTooling = value; }
    }

private string _FileName;
    public string FileName
    {
        get { return _FileName; }
        set { _FileName = value; }
    }

    private string _FileName2;
    public string FileName2
    {
        get { return _FileName2; }
        set { _FileName2 = value; }
    }

    private string _FileName3;
    public string FileName3
    {
        get { return _FileName3; }
        set { _FileName3 = value; }
    }

    private string _FileName4;
    public string FileName4
    {
        get { return _FileName4; }
        set { _FileName4 = value; }
    }

    private string _FileName5;
    public string FileName5
    {
        get { return _FileName5; }
        set { _FileName5 = value; }
    }

private int _MutualExcl;
    public int MutualExcl
    {
        get { return _MutualExcl; }
        set { _MutualExcl = value; }
    }

}


Tooling Type is being define : 

private string _ToolingType;
    public string ToolingType
    {
        get { return _ToolingType; }
        set { _ToolingType = value; }
    }


Do I need to add : 

this.ToolingType = new ToolingType;

into 

public TableTOF()
    { 

    }
0 George Williams · September 12, 2014
Oh my, I cant believe I never I saw that..My fault, it was just off the top of my head
+2 Eugene Botma · September 12, 2014
@Geore

TableTOF vTable = new TableTOF(); 



does exactly the same as 

/Declare the object

TableTOF vTable;

//Set the Object elsewhere

vTable = new TableTOF();



That is not in fact the problem. the problem isn't that the TableTOF object is null, but rather the TableTOF.ToolingType property is null.

The fix is rather easy: set the TableTOF.ToolingType property before calling the PopulateTOF method.

For example, close to the end of the file (in the btnSaveTOF_click event handler), there is:


TableTOF tblTOF = new TableTOF();

if (!PopulateTOF(tblTOF)) return;


So instead, you could do


TableTOF tblTOF = new TableTOF();
tblTOP.ToolingType = new ToolinType(); //or whatever class the toolingtype property is
if (!PopulateTOF(tblTOF)) return;

And the error should be fixed.

The better solution (imo) is to set the ToolingType property in the TableTOF constructor. 

Thus in the TableTOF class, you will have something like:

public TableTOF()
{
this.ToolingType = new ToolingType; //or whatever class the toolingtype property is
}
0 George Williams · September 12, 2014
You got a Null reference exception error because your initial object reference:

TableTOF vTable = new TableTOF();

Is set to null(Don't ask how or I'll have to write  a new topic), so to solve this you.could do two things:

//Declare the object

TableTOF vTable;

//Set the Object elsewhere

vTable = new TableTOF();

If you still have an error then it's natural because this error is rather common, you'll have to go through a whole process of debugging...this is isn't gonna be a nice process if the above code doesn't work lol.
0 Melissa Charn · September 12, 2014
Dear George,

My apology :)

Thank you for your reply, the codes are as below, the part where I highlighted in red is the error from the message.

Codes starts here:
==============

using System;
using System.Data;
using System.IO;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

public partial class Form5Modify : System.Web.UI.Page
{
    private string NEW_TOOLING = "NEW";
    private string MODIFICATION_TOOLING = "MODIFICATION";
    private readonly string WEB_ADDR = "Form5List.aspx?page=1";
    private string FORM_NAME = "TOF";
private string FORM_ADDR = FormDBMaster.TOF_WEB;
    protected void Page_Load(object sender, EventArgs e)
    {
        string userName = CommonFunc.GetLoginUser(this.Page);
        DBTableUser userDB = new DBTableUser();
        if (!userDB.IsValidUser(userName))
            Response.Redirect("error.aspx");

        if (!IsPostBack)
        {
            DBTableApproval appDB = new DBTableApproval();
            TableUser tbUser = new TableUser();
            TableApproval tbApp = new TableApproval();

            int tbUserID = userDB.GetUserID(userName);
tbApp = appDB.GetUserApproval(tbUserID, 5);
            if (tbApp.Approver != 0 && userDB.GetUserName(tbApp.Approver) != null)
                sApprover.Items.Insert(0, userDB.GetUserName(tbApp.Approver));
            if (tbApp.Approver2 != 0 && userDB.GetUserName(tbApp.Approver2) != null)
                sApprover.Items.Insert(1, userDB.GetUserName(tbApp.Approver2));
            if (tbApp.Approver3 != 0 && userDB.GetUserName(tbApp.Approver3) != null)
                sApprover.Items.Insert(2, userDB.GetUserName(tbApp.Approver3));
            if (tbApp.Checker != 0 && userDB.GetUserName(tbApp.Checker) != null)
                sChecker.Items.Insert(0, userDB.GetUserName(tbApp.Checker));
            if (tbApp.Checker2 != 0 && userDB.GetUserName(tbApp.Checker2) != null)
                sChecker.Items.Insert(1, userDB.GetUserName(tbApp.Checker2));
            if (tbApp.Checker3 != 0 && userDB.GetUserName(tbApp.Checker3) != null)
                sChecker.Items.Insert(2, userDB.GetUserName(tbApp.Checker3));
btnSaveTOF.Attributes.Add("onclick", 
            "return confirm('Do you want to save it for later modification?');");
btnSubmit.Attributes.Add("onclick",
            "return confirm('Press Confirm To Proceed Modify Tooling Order Form?');");

            Session["ID"] = Request.QueryString["ID"];
            if (Session["ID"] == null) return;

            DBTableTOF db = new DBTableTOF();
            TableTOF vTable = new TableTOF();
            if (!db.GetTOF(Int32.Parse(Session["ID"].ToString()), ref vTable))
            return;

            tbPartName.Text = vTable.PartName;

            if (!PopulateTOF(vTable)) return;
            Session["OldFileName"] = vTable.TOFFileName;
}
        TextBox sc = new TextBox();
        sc.Text = "Modify";
        Session["_textBox"] = sc;
        Page.MaintainScrollPositionOnPostBack = true;
    }

    private bool PopulateTOF(TableTOF vTable)
    {       
        if (vTable.ToolingType.Equals("NEW"))
            rbNew.Checked = true;
        if (vTable.ToolingType.Equals("MODIFICATION"))
            rbModification.Checked = true;      


 //==================urgent Label======================
        if(vTable.Urgent.ToString().Equals("URGENT"))
            CBalert_urgent.Checked = true;
        else
            CBalert_urgent.Checked = false;
        //===================urgent label end====================== 

tbPartNumber.Text = vTable.PartNumber;
        tbDevCode.Text = vTable.DevCode;
        tbModelName.Text = vTable.ModelName;
        tbModelNumber.Text = vTable.ModelNumber;
        tbDim1.Text = vTable.Dim1;
        tbDim2.Text = vTable.Dim2;
        tbDim3.Text = vTable.Dim3;
        tbSampleQuantity.Text = vTable.SampleQuantity;
        tbCost.Text = vTable.ToolingCost;
        tbMRNo.Text = vTable.MRNo;
tbRemarks.Text = vTable.Remarks;
        if (vTable.NonTooling == 1)
            NTCheckbox.Checked = true;

ApplicationUtil ap = new ApplicationUtil();
        string dir = ap.GetWebSaveDir();
        if (vTable.TOFFileName != "")
        {
            fileName1.NavigateUrl = dir + vTable.TOFFileName;
            fileName1.Text = vTable.TOFFileName;
            fileName1.Visible = true;
            checkBox1.Visible = true;
            li1.Visible = true;
        }
        if (vTable.TOFFileName2 != "")
        {
            fileName2.NavigateUrl = dir + vTable.TOFFileName2;
            fileName2.Text = vTable.TOFFileName2;
            fileName2.Visible = true;
            checkBox2.Visible = true;
            li2.Visible = true;
        }
        if (vTable.TOFFileName3 != "")
        {
            fileName3.NavigateUrl = dir + vTable.TOFFileName3;
            fileName3.Text = vTable.TOFFileName3;
            fileName3.Visible = true;
            checkBox3.Visible = true;
            li3.Visible = true;
        }
        if (vTable.TOFFileName4 != "")
        {
            fileName4.NavigateUrl = dir + vTable.TOFFileName4;
            fileName4.Text = vTable.TOFFileName4;
            fileName4.Visible = true;
            checkBox4.Visible = true;
            li4.Visible = true;
        }
        if (vTable.TOFFileName5 != "")
        {
            fileName5.NavigateUrl = dir + vTable.TOFFileName5;
            fileName5.Text = vTable.TOFFileName5;
            fileName5.Visible = true;
            checkBox5.Visible = true;
            li5.Visible = true;
        }
        
        return true;
    }

    private bool GetData(TableTOF dtable, System.Web.UI.Page page)
    {
        KetsStringUtilCs util = new KetsStringUtilCs();
        KetsSecurityCs sec = new KetsSecurityCs(page);
        DBTableUser user = new DBTableUser();

        dtable.IssueDate = CommonFunc.GetDate();
        dtable.IssueTime = CommonFunc.GetTime();

        if (rbModification.Checked == true)
            dtable.ToolingType = MODIFICATION_TOOLING;
        if (rbNew.Checked == true)
            dtable.ToolingType = NEW_TOOLING;

        if (CBalert_urgent.Checked)
            dtable.Urgent = "URGENT";
        else
            dtable.Urgent = "";

        if (NTCheckbox.Checked)
            dtable.NonTooling = 1;
        else
            dtable.NonTooling = 0;

        dtable.PartName = util.Escape_SQL_Reserved_Words(tbPartName.Text.Trim());
        dtable.PartNumber = util.Escape_SQL_Reserved_Words(tbPartNumber.Text.Trim());
        dtable.DevCode = util.Escape_SQL_Reserved_Words(tbDevCode.Text.Trim());
        dtable.ModelName = util.Escape_SQL_Reserved_Words(tbModelName.Text.Trim());
        dtable.ModelNumber = util.Escape_SQL_Reserved_Words(tbModelNumber.Text.Trim());
        dtable.Dim1 = util.Escape_SQL_Reserved_Words(tbDim1.Text.Trim());
        dtable.Dim2 = util.Escape_SQL_Reserved_Words(tbDim2.Text.Trim());
        dtable.Dim3 = util.Escape_SQL_Reserved_Words(tbDim3.Text.Trim());
        dtable.SampleQuantity = util.Escape_SQL_Reserved_Words(tbSampleQuantity.Text.Trim());
        dtable.ToolingCost = util.Escape_SQL_Reserved_Words(tbCost.Text.Trim());
        dtable.MRNo = util.Escape_SQL_Reserved_Words(tbMRNo.Text.Trim());
dtable.Remarks = util.Escape_SQL_Reserved_Words(tbRemarks.Text.Trim());

string escUserName = CommonFunc.GetLoginUser(this.Page);
        dtable.Author = escUserName;

        dtable.TOFFileName = fileName1.Text;
        dtable.TOFFileName2 = fileName2.Text;
        dtable.TOFFileName3 = fileName3.Text;
        dtable.TOFFileName4 = fileName4.Text;
        dtable.TOFFileName5 = fileName5.Text;

CommonUtil cInstance = CommonUtil.GetInstance();
        string currTime = cInstance.GetClockTick;
        dtable.SubmitCode = currTime;
return true;
    }

    private string SaveAttach(string sUserName, int fileId)
    {
        if (fuAttach.PostedFile.ContentLength == 0) return string.Empty;
        string sIntFileName = null;

        sIntFileName = GetInternalFileName(fuAttach.PostedFile.FileName, sUserName, FORM_NAME, fileId);
        if (sIntFileName == null) return null;

        ApplicationUtil appUtil = new ApplicationUtil();
        string sBaseLocation = appUtil.GetFileSaveDir();

        try
        {
            fuAttach.PostedFile.SaveAs(sBaseLocation + sIntFileName);
        }
        catch (Exception exc)
        {
            string ngMsg = "Error In Saving File!";
            Page.RegisterStartupScript(@"NG-001",
               @"<script>alert('" + ngMsg + exc.ToString() + "');</script>");
        }
        return sIntFileName;
    }

    private string GetInternalFileName(string sFileName, string sUserName,
       string varTypeOfForm, int index)
    {

string varUploadDateTime = CommonFunc.GetDateTime();
string appendix = index.ToString().Insert(0, "-");
int iLocation = 0;
        iLocation = sFileName.LastIndexOf(".");

        return (varTypeOfForm + "_" + varUploadDateTime + "_" + sUserName + appendix + sFileName.Substring(iLocation));
    }



    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid) return;

        if (!swapChkApp())
            return;

        Session["ID"] = Request.QueryString["ID"];
        if (Session["ID"] == null) return;
       
        TableTOF oldTable = new TableTOF();
        DBTableTOF db = new DBTableTOF();
        if (!db.GetTOF(Int32.Parse(Session["ID"].ToString()), ref oldTable))
        return;

        TableTOF vTable = new TableTOF();
        if (!GetData(vTable, this.Page)) return;
        vTable.ID = oldTable.ID;
        vTable.RefNo = oldTable.RefNo;

if(!db.Update(vTable))
        {
            // Prompt Error
            string errMsg = "Error-001: Add Tooling Order Form Failed!";
            this.Page.RegisterStartupScript(@"Error-001",
               @"<script>alert('" + errMsg + "');</script>");
            return;
        }

        CommonUtil cInstance = CommonUtil.GetInstance();
        string currTime = cInstance.GetClockTick;
        vTable.SubmitCode = currTime;    
        if (!db.UpdateSubmitCode(vTable))
        {
            // Prompt Error
            string errMsg = "Error-005: Failed To Update Submission Code!";
            Page.RegisterStartupScript(@"Error-005",
               @"<script>alert('" + errMsg + "');</script>");
            return;
        }

CustomSQL cSQL = new CustomSQL();
        cSQL.IntCustomInsert("update table_tof_type set tof_type = '" + sType.SelectedValue + "' where tof_id = '" + vTable.ID+"'");

EmailUtil_TOF emailUtil = new EmailUtil_TOF();
        emailUtil.TOF_Add(vTable);
emailUtil.TOF_Modify(vTable);

string okMsg = "OK-001: Tooling Order Submitted: Submission Code: ";
        this.Page.RegisterStartupScript(@"OK-001",
           @"<script>alert('" + okMsg + vTable.SubmitCode + "Ref No:" + vTable.RefNo + "');</script>");

        Response.Redirect(WEB_ADDR);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Session["ID"] = Request.QueryString["ID"];
        if (Session["ID"] == null)
            return;
        DBTableTOF db = new DBTableTOF();
        TableTOF tblTOF = new TableTOF();
        if (!db.GetTOF(Int32.Parse(Session["ID"].ToString()), ref tblTOF)) return;
Response.Redirect(WEB_ADDR);
    }

    protected void upload_Click(object sender, EventArgs e)
    {
        Session["ID"] = Request.QueryString["ID"];
        if (Session["ID"] == null)
            return;
        string sIntFileName = "";
        DBTableTOF db = new DBTableTOF();
        TableTOF vTable = new TableTOF();
        if (!db.GetTOF(Int32.Parse(Session["ID"].ToString()), ref vTable))
            return;

        if (fileName1.Visible == false)
        {
            if (!fuAttach.PostedFile.FileName.Equals(""))
                sIntFileName = GetInternalFileName(fuAttach.PostedFile.FileName, CommonFunc.GetLoginUser(this.Page), FORM_NAME, 1);
            if (sIntFileName == "") return;
            if (vTable.ReviseCode == 2)
                sIntFileName = sIntFileName.Insert(sIntFileName.Length - 4, "-R");
            else if (vTable.ReviseCode > 2)
                sIntFileName = sIntFileName.Insert(sIntFileName.Length - 4, "-" + (vTable.ReviseCode - 1));
            fileName1.Text = sIntFileName;
            ApplicationUtil ap = new ApplicationUtil();
            string dir = ap.GetWebSaveDir();
            fileName1.NavigateUrl = dir + sIntFileName;
            fileName1.Visible = true;
checkBox1.Visible = true;
            li1.Visible = true;
            SaveAttach(CommonFunc.GetLoginUser(this.Page), 1);
        }
        else if (fileName2.Visible == false)
        {
            if (!fuAttach.PostedFile.FileName.Equals(""))
                sIntFileName = GetInternalFileName(fuAttach.PostedFile.FileName, CommonFunc.GetLoginUser(this.Page), FORM_NAME, 2);
            if (sIntFileName == "") return;
            if (vTable.ReviseCode == 2)
                sIntFileName = sIntFileName.Insert(sIntFileName.Length - 4, "-R");
            else if (vTable.ReviseCode > 2)
                sIntFileName = sIntFileName.Insert(sIntFileName.Length - 4, "-" + (vTable.ReviseCode - 1));
            fileName2.Text = sIntFileName;
            ApplicationUtil ap = new ApplicationUtil();
            string dir = ap.GetWebSaveDir();
            fileName2.NavigateUrl = dir + sIntFileName;
            fileName2.Visible = true;
checkBox2.Visible = true;
            li2.Visible = true;
            SaveAttach(CommonFunc.GetLoginUser(this.Page), 2);
        }
        else if (fileName3.Visible == false)
        {
            if (!fuAttach.PostedFile.FileName.Equals(""))
                sIntFileName = GetInternalFileName(fuAttach.PostedFile.FileName, CommonFunc.GetLoginUser(this.Page), FORM_NAME, 3);
            if (sIntFileName == "") return;
            if (vTable.ReviseCode == 2)
                sIntFileName = sIntFileName.Insert(sIntFileName.Length - 4, "-R");
            else if (vTable.ReviseCode > 2)
                sIntFileName = sIntFileName.Insert(sIntFileName.Length - 4, "-" + (vTable.ReviseCode - 1));
            fileName3.Text = sIntFileName;
            ApplicationUtil ap = new ApplicationUtil();
            string dir = ap.GetWebSaveDir();
            fileName3.NavigateUrl = dir + sIntFileName;
            fileName3.Visible = true;
checkBox3.Visible = true;
            li3.Visible = true;
            SaveAttach(CommonFunc.GetLoginUser(this.Page), 3);
        }
        else if (fileName4.Visible == false)
        {
            if (!fuAttach.PostedFile.FileName.Equals(""))
                sIntFileName = GetInternalFileName(fuAttach.PostedFile.FileName, CommonFunc.GetLoginUser(this.Page), FORM_NAME, 4);
            if (sIntFileName == "") return;
            if (vTable.ReviseCode == 2)
                sIntFileName = sIntFileName.Insert(sIntFileName.Length - 4, "-R");
            else if (vTable.ReviseCode > 2)
                sIntFileName = sIntFileName.Insert(sIntFileName.Length - 4, "-" + (vTable.ReviseCode - 1));
            fileName4.Text = sIntFileName;
            ApplicationUtil ap = new ApplicationUtil();
            string dir = ap.GetWebSaveDir();
            fileName4.NavigateUrl = dir + sIntFileName;
            fileName4.Visible = true;
checkBox4.Visible = true;
            li4.Visible = true;
            SaveAttach(CommonFunc.GetLoginUser(this.Page), 4);
        }
        else if (fileName5.Visible == false)
        {
            if (!fuAttach.PostedFile.FileName.Equals(""))
                sIntFileName = GetInternalFileName(fuAttach.PostedFile.FileName, CommonFunc.GetLoginUser(this.Page), FORM_NAME, 5);
            if (sIntFileName == "") return;
            if (vTable.ReviseCode == 2)
                sIntFileName = sIntFileName.Insert(sIntFileName.Length - 4, "-R");
            else if (vTable.ReviseCode > 2)
                sIntFileName = sIntFileName.Insert(sIntFileName.Length - 4, "-" + (vTable.ReviseCode-1));
            fileName5.Text = sIntFileName;
            ApplicationUtil ap = new ApplicationUtil();
            string dir = ap.GetWebSaveDir();
            fileName5.NavigateUrl = dir + sIntFileName;
            fileName5.Visible = true;
checkBox5.Visible = true;
            li5.Visible = true;
            SaveAttach(CommonFunc.GetLoginUser(this.Page), 5);
        }
    }
    protected void imgButton_Click(object sender, ImageClickEventArgs e)
    {
        if (checkBox1.Checked == true)
        {
            try
            {
                FileInfo TheFile = new FileInfo(MapPath(".") + "\\Files\\" + fileName1.Text);
                if (TheFile.Exists)
                {
                    File.Delete(MapPath(".") + "\\Files\\" + fileName1.Text);

                }
                else
                {
                    throw new FileNotFoundException();
                }
            }
            catch (FileNotFoundException exc)
            {
}
            catch (Exception exc)
            {
            }
            fileName1.Text = "";
            fileName1.NavigateUrl = "";
            fileName1.Visible = false;
            checkBox1.Checked = false;
            checkBox1.Visible = false;
            li1.Visible = false;
        }
        if (checkBox2.Checked == true)
        {
            try
            {
                FileInfo TheFile = new FileInfo(MapPath(".") + "\\Files\\" + fileName2.Text);
                if (TheFile.Exists)
                {
                    File.Delete(MapPath(".") + "\\Files\\" + fileName2.Text);

                }
                else
                {
                    throw new FileNotFoundException();
                }
            }
            catch (FileNotFoundException exc)
            {
}
            catch (Exception exc)
            {
            }
            fileName2.Text = "";
            fileName2.NavigateUrl = "";
            fileName2.Visible = false;
            checkBox2.Checked = false;
            checkBox2.Visible = false;
            li2.Visible = false;
        }
        if (checkBox3.Checked == true)
        {
            try
            {
                FileInfo TheFile = new FileInfo(MapPath(".") + "\\Files\\" + fileName3.Text);
                if (TheFile.Exists)
                {
                    File.Delete(MapPath(".") + "\\Files\\" + fileName3.Text);

                }
                else
                {
                    throw new FileNotFoundException();
                }
            }
            catch (FileNotFoundException exc)
            {
}
            catch (Exception exc)
            {
            }
            fileName3.Text = "";
            fileName3.NavigateUrl = "";
            fileName3.Visible = false;
            checkBox3.Checked = false;
            checkBox3.Visible = false;
            li3.Visible = false;
        }
        if (checkBox4.Checked == true)
        {
            try
            {
                FileInfo TheFile = new FileInfo(MapPath(".") + "\\Files\\" + fileName4.Text);
                if (TheFile.Exists)
                {
                    File.Delete(MapPath(".") + "\\Files\\" + fileName4.Text);

                }
                else
                {
                    throw new FileNotFoundException();
                }
            }
            catch (FileNotFoundException exc)
            {
}
            catch (Exception exc)
            {
            }
            fileName4.Text = "";
            fileName4.NavigateUrl = "";
            fileName4.Visible = false;
            checkBox4.Checked = false;
            checkBox4.Visible = false;
            li4.Visible = false;
        }
        if (checkBox5.Checked == true)
        {
            try
            {
                FileInfo TheFile = new FileInfo(MapPath(".") + "\\Files\\" + fileName5.Text);
                if (TheFile.Exists)
                {
                    File.Delete(MapPath(".") + "\\Files\\" + fileName5.Text);

                }
                else
                {
                    throw new FileNotFoundException();
                }
            }
            catch (FileNotFoundException exc)
            {
                string errMsg = "Error! Cannot Delete File: ";
                Page.RegisterStartupScript(@"Error-004",
                   @"<script>alert('" + errMsg + exc.ToString() + "');</script>");
                //return;
            }
            catch (Exception exc)
            {
                string errMsg = "Error! Cannot Delete File: ";
                Page.RegisterStartupScript(@"Error-004",
                   @"<script>alert('" + errMsg + exc.ToString() + "');</script>");
            }
            fileName5.Text = "";
            fileName5.NavigateUrl = "";
            fileName5.Visible = false;
            checkBox5.Checked = false;
            checkBox5.Visible = false;
            li5.Visible = false;
        }

    }

    protected void checkSelectAll(object sender, EventArgs e)
    {
        if (selectAll.Text.Equals("Select All"))
        {
            selectAll.Text = "Deselect All";
            checkBox1.Checked = true;
            checkBox2.Checked = true;
            checkBox3.Checked = true;
            checkBox4.Checked = true;
            checkBox5.Checked = true;
        }
        else
        {
            selectAll.Text = "Select All";
            checkBox1.Checked = false;
            checkBox2.Checked = false;
            checkBox3.Checked = false;
            checkBox4.Checked = false;
            checkBox5.Checked = false;
        }

    }
    protected void btnCancel_Click1(object sender, EventArgs e)
    {
        try
        {
            FileInfo TheFile = new FileInfo(MapPath(".") + "\\Files\\" + fileName1.Text);
            if (TheFile.Exists)
            {
                File.Delete(MapPath(".") + "\\Files\\" + fileName1.Text);
            }
            else
            {
                throw new FileNotFoundException();
            }
        }
        catch (FileNotFoundException exc)
        {
}
        catch (Exception exc)
        {
        }

        try
        {
            FileInfo TheFile = new FileInfo(MapPath(".") + "\\Files\\" + fileName2.Text);
            if (TheFile.Exists)
            {
                File.Delete(MapPath(".") + "\\Files\\" + fileName2.Text);
            }
            else
            {
                throw new FileNotFoundException();
            }
        }
        catch (FileNotFoundException exc)
        {
}
        catch (Exception exc)
        {
        }

        try
        {
            FileInfo TheFile = new FileInfo(MapPath(".") + "\\Files\\" + fileName3.Text);
            if (TheFile.Exists)
            {
                File.Delete(MapPath(".") + "\\Files\\" + fileName3.Text);
            }
            else
            {
                throw new FileNotFoundException();
            }
        }
        catch (FileNotFoundException exc)
        {
}
        catch (Exception exc)
        {
        }

        try
        {
            FileInfo TheFile = new FileInfo(MapPath(".") + "\\Files\\" + fileName4.Text);
            if (TheFile.Exists)
            {
                File.Delete(MapPath(".") + "\\Files\\" + fileName4.Text);
            }
            else
            {
                throw new FileNotFoundException();
            }
        }
        catch (FileNotFoundException exc)
        {
}
        catch (Exception exc)
        {
        }

        try
        {
            FileInfo TheFile = new FileInfo(MapPath(".") + "\\Files\\" + fileName5.Text);
            if (TheFile.Exists)
            {
                File.Delete(MapPath(".") + "\\Files\\" + fileName5.Text);
            }
            else
            {
                throw new FileNotFoundException();
            }
        }
        catch (FileNotFoundException exc)
        {
}
        catch (Exception exc)
        {
        }

        Response.Redirect("Form5List.aspx");
    }

    private bool swapChkApp()
    {
        TableApproval tblApp = new TableApproval();
        DBTableApproval dbTblApp = new DBTableApproval();
        DBTableUser dbTblUser = new DBTableUser();
        int UID = dbTblUser.GetUserID(CommonFunc.GetLoginUser(this.Page));
        tblApp = dbTblApp.GetUserApproval(UID, 1);
if (sApprover.SelectedIndex != 0)
        {
            int OID = tblApp.Approver;
            int UID1 = dbTblUser.GetUserID(sApprover.SelectedValue);
            if (dbTblUser.GetUser(UID1).Status == false)
            {
                string ngMsg = "Approve person on leave, choose another one";
                Page.RegisterStartupScript(@"NG-001",
                   @"<script>alert('" + ngMsg + "');</script>");
                return false;
            }
            tblApp.Approver = UID1;
            if (sApprover.SelectedIndex == 1)
                tblApp.Approver2 = OID;
            else if (sApprover.SelectedIndex == 2)
                tblApp.Approver3 = OID;

        }

        if (sChecker.SelectedIndex != 0)
        {
            int UID1 = dbTblUser.GetUserID(sChecker.SelectedValue);
            if (dbTblUser.GetUser(UID1).Status == false)
            {
                string ngMsg = "Check person on leave, choose another one";
                Page.RegisterStartupScript(@"NG-001",
                   @"<script>alert('" + ngMsg + "');</script>");
                return false;
            }
            int OID = tblApp.Checker;
            tblApp.Checker = UID1;
            if (sChecker.SelectedIndex == 1)
                tblApp.Checker2 = OID;
            else if (sChecker.SelectedIndex == 2)
                tblApp.Checker3 = OID;

        }

        dbTblApp.Update(tblApp);
        return true;
    }

protected void btnSaveTOF_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid) return;
        if(!swapChkApp())
            return;
          Session["ID"] = Request.QueryString["ID"];
         if (Session["ID"] == null)
         return;

        DBTableTOF db = new DBTableTOF();
        TableTOF oldTOF = new TableTOF();
        if (!db.GetTOF(Int32.Parse(Session["ID"].ToString()), ref oldTOF)) return;

        TableTOF tblTOF = new TableTOF();
        if (!PopulateTOF(tblTOF)) return;

        tblTOF.ID = oldTOF.ID;
        tblTOF.RefNo = oldTOF.RefNo;
        tblTOF.UniqueTime = oldTOF.UniqueTime;
       // tblDVR.CheckCode = "";
        
        if ( (tblTOF.FileName == null ))
        {
            tblTOF.FileName = fileName1.Text;
        }

        if ((tblTOF.FileName2 == null))
        {
            tblTOF.FileName2 = fileName2.Text;
        }

        if ((tblTOF.FileName3 == null))
        {
            tblTOF.FileName3 = fileName3.Text;
        }

        if ((tblTOF.FileName4 == null))
        {
            tblTOF.FileName4 = fileName4.Text;
        }

        if ((tblTOF.FileName5 == null))
        {
            tblTOF.FileName5 = fileName5.Text;
        }
        if (!db.Update(tblTOF))
        {
            // Prompt Error
            string errMsg = "Error-006: Modify TOF Failed!";
            Page.RegisterStartupScript(@"Error-006",
               @"<script>alert('" + errMsg + "');</script>");
            return;
        }
        tblTOF.Revise = oldTOF.Revise;
        if (!db.updateRevise(tblTOF)) return;
        //update child's Ancestor
        tblTOF.Ancestor = oldTOF.Ancestor;
        if (!db.updateAncestor(tblTOF)) return;
        //update parent entry's Child ID
        oldTOF.Child = tblTOF.Child;
        if (!db.updateChild(oldTOF)) return;
        //update child reviseCode
        tblTOF.ReviseCode = oldTOF.ReviseCode;
        if (!db.updateReviseCode(tblTOF)) return;

        tblTOF.MutualExcl = 0;
        if (!db.updateMutualExcl(tblTOF)) return;
        CommonUtil cInstance = CommonUtil.GetInstance();
        string currTime = cInstance.GetClockTick;
        tblTOF.SubmitCode = currTime;
        if (! db.UpdateSubmitCode(tblTOF))
        {
            // Prompt Error
            string errMsg = "Error-005: Failed To Update Submission Code!";
            Page.RegisterStartupScript(@"Error-005",
               @"<script>alert('" + errMsg + "');</script>");
            return;
        }

/* set check code to prevent this item from listed in checker list (workaround) */
tblTOF.CheckCode = currTime;
db.UpdateCheckCode(tblTOF);

/* set approve code to prevent this item from listed in approver list (workaround) */
tblTOF.ApproveCode = currTime;
db.UpdateApproveCode(tblTOF);
string okMsg = "OK-001: TOF saved: Submission Code: ";
        Page.RegisterStartupScript(@"OK-001",
           @"<script>alert('" + okMsg + tblTOF.SubmitCode + "Ref No:" + tblTOF.RefNo + "');</script>");
     }
    
}
0 George Williams · September 12, 2014
I dont mean to be this guy but, it states it in the error message, your referring to an object but there is no instance of that object..Now if you'd like me to help you solve your problem with code, im going to need to see more code from you :)
  • 1

C#

107,307 followers
About

C# is an object-oriented language designed for improving the development of web applications.

Links
Moderators