Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi i'm getting the above error with following line in my below code.
"Object refrence not set to the instance of the Object."

DepartmentID.Text = Asset.mASS_mDEP_NUPKId_Department.ToString();
if (DepartmentID.Text != "0")
{
  LQMasterComDepartmentDataContext CDbde = new LQMasterComDepartmentDataContext();
  TB_MasterCompanyDepartment AssDepartment = CDbde.TB_MasterCompanyDepartments.FirstOrDefault
  (C => C.mCDP_NUPKId == mobjGenlib.ConvertLong(DepartmentID.Text));
  DepartmentName.Text = AssDepartment.mCDP_VCDepartmentName;
}


I'm getting error in below line
"
DepartmentName.Text = AssDepartment.mCDP_VCDepartmentName;
"


can any one please help me to figure it out?

Thanks in advance.
Posted
Comments
Raje_ 3-Mar-14 2:22am    
Really....!!! AssDepartment? :)
[no name] 3-Mar-14 2:24am    
yes yes......Not in AssDepartment in DepartmentName.....
[no name] 3-Mar-14 2:24am    
DepartmentName = (TextBox)(Department.FindControl("DepartmentName")); this is how i declared which is wrong...the correct way is DepartmentName = (TextBox)(Department.FindControl("Department"));

Interesting variable name :laugh:

Anyway, the reason you're getting a NullReferenceException probably lies in the previous line:
C#
TB_MasterCompanyDepartment AssDepartment = CDbde.TB_MasterCompanyDepartments.FirstOrDefault(C => C.mCDP_NUPKId == mobjGenlib.ConvertLong(DepartmentID.Text));


Either TB_MasterCompanyDepartments is an empty collection, or the value of DepartmentID.Text does not match any mCDP_NUPKId value.

Step through your code with a debugger and it's pretty easy to spot what the problem is here. And either way it's entirely valid for AssDepartment to be null, so test it and handle it accordingly.
 
Share this answer
 
Comments
[no name] 3-Mar-14 0:38am    
I Checked using debugging and TB_MasterCompanyDepartments is not an empty set ....
I have mCDP_NUPKId as 5 and it desired mCDP_VCDepartmentName = Air Import.

while the code reaches to DepartmentName.Text = AssDepartment.mCDP_VCDepartmentName; this line it throws an error
Maarten Kools 3-Mar-14 0:54am    
Yes, the code will reach that line just fine. Stepping through with the debugger and checking all variables on that line (should be easy, there are only two, DepartmentName and AssDepartment) will tell you exactly what is null (unless, of course, mCDP_VCDepartmentName is a property which does all kinds of things that throws a NullReferenceException). Now, the most probably suspect for a variable being null is AssDepartment, and that means your FirstOrDefault expression returns a null value. If it doesn't, then DepartmentName is null.
pradeep surya 3-Mar-14 6:58am    
AssDepartment is null....
The thing which you are querying using Linq is not present i guess..
Hence getting the null AssDepartment object.
Hence while accessing the null object(AssDepartment) for getting the mCDP_VCDepartmentName value throws an null exception.
First, is the entity 'DeparmentName something other than 'null here ?

Put a break-point on the line: DepartmentName.Text = AssDepartment.mCDP_VCDepartmentName;

When you reach that break-point, inspect the value of 'DepartmentName, and its 'Text property.

If 'DepartName is okay, then open the command-window ... Control+W,A ... and type: ? AssDepartment <return> :

Examine the output in the command-window: is 'AssDepartment what you think it should be; does the property 'mCDP_VCDepartmentName "look right" ?
 
Share this answer
 
Comments
[no name] 3-Mar-14 2:00am    
It saying as "The system cannot find the file specified"
[no name] 3-Mar-14 2:04am    
DepartmentName.Text = Department.mCDP_VCDepartmentName;

While inspecting it shows as
Department.mCDP_VCDepartmentName(Air Import)
DepartmentName.Text ( DepartmentName.Text|null)
Hi I found the solution for the issues

Actually in the definition of usercontrol i have given as

C#
DepartmentID = (TextBox)(Department.FindControl("DepartmentID"));
DepartmentName = (TextBox)(Department.FindControl("DepartmentName"));

Which is wrong it should be

C#
DepartmentID = (TextBox)(Department.FindControl("DepartmentID"));
DepartmentName = (TextBox)(Department.FindControl("Department"));


Now it works fine :)

Thanks for all who showed interest on my Question.
 
Share this answer
 

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