Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
protected void SaveAttachments(Int32 actRuleID)
    {
        if (gvAttachments.Rows.Count > 0)
        {
            if ((Session["FileDetail"].ToString() != ""))
            {
                string[] Records = Session["FileDetail"].ToString().Split('#');
                byte[] fileContent;
                FileStream fs;
                BinaryReader br;
                for (int i = 0; i < Records.Length; i++)
                {

                    string[] arrRecords = Records[i].Split(',');

                    fs = new FileStream(arrRecords[2], FileMode.Open, FileAccess.Read);
                    br = new BinaryReader(fs);
                    fileContent = br.ReadBytes((int)fs.Length);
                    br.Close();
                    fs.Close();
                    Econnect.EEMS.ActRulesAttachments objAttachment = new Econnect.EEMS.ActRulesAttachments();
                    objAttachment.ActRuleID = actRuleID;
                    objAttachment.FileName = arrRecords[0].ToString();
                    objAttachment.FileFormat = arrRecords[3].ToString();
                    objAttachment.FileContent = fileContent;
                    context.ActRuleAttachments.Add(objAttachment);
                    context.SaveChanges();
                }
            }

        }
    }
Posted
Updated 23-May-14 3:29am
v3
Comments
soumyajayaraj 22-May-14 1:08am    
Which line is giving the error? Have you tried debugging?

1.This error occurs because you are trying to access the property/method of a null object.

2.You should Debug your code and inspect in your code that generate the exception the values of the used variables, and one of it should be null.

PS: Normally when you read something from Session you should test it if is null or not. if(Session["FileDetail"] != null && Session["FileDetail"].ToString() != "")){...}
 
Share this answer
 
v2
Comments
Neha Mukesh 22-May-14 1:14am    
i have debugged my code no error comes but still it is giving object refernece error??
Additional to the Solution 1, kindly check for the variables. Maybe they don't generate a value that's why you're getting an exception.

If you are using Visual Studio, use breakpoint so that you can see in what specific line you are having problems with.
 
Share this answer
 
Comments
Neha Mukesh 22-May-14 2:43am    
i got my answer i forgot ot use Dbcontext class

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