Object reference is not set into instance of an object - c#

I am retrieving data from database table.
By giving input as 1 in
textbox1(voucherno)-->damagedetail =
web.getdamageddetail(Convert.ToInt32(voucherno.Text));
I want get branchno as output in textbox2(branchno)
branchno.Text = damagedetail.branchno.ToString();
but am getting error
Object reference is not set into instance of an object.
Check my second line.
Is second line coding correct?

damagedetail.branchno is null.
Try this:
if (damagedetail.branchno != null)
branchno.Text = damagedetail.branchno;
else
branchno.Text = "abcd";

Either your object property is not filled thus when you retrive the value "branchno" from the object the value is equal to null, or your not filling the property in your "getdamageddetail" method

Related

C#, Session object is updated after change in List object [duplicate]

I have this really weird problem and I'm sure I'm missing something obvious here. I have these two lines:
HttpContext.Current.Session[listModelType + "ListModel"] = listModel;
listModel.ProductRows = new Collection<ProductRow>(listModel.ProductRows.Where(r => r.ParentRowId == 0).ToList());
After the second line is executed my session object is updated as well (according to "Watch" in Visual Studio)
What am I missing here?
I have tried
int i = 0;
HttpContext.Current.Session["i"] = i;
i++;
and HttpContext.Current.Session["i"] remains 0.
See value types and reference types.
The int is a value type so will be stored "as-is" on the moment of assignment; your listModel is a reference type so you store a reference to the object in your session, not the value of the object.
You'll have to create a new instance of listModel if you want the one in your session untouched.
In your first example you are storing a reference to the object (The lists memory location). So if the list is updated it will reflect in the session. This is a reference type.
In the second example you are using a value type:
int i = 0;
HttpContext.Current.Session["i"] = i;
i++;
You declare i and set it to 0 (Value type)
You store the value 0 in the session. (Not the memory location of i)
You increment i but the session still has the value 0
In the first example your session variable is pointing to a reference so it gets updated because the two references are pointing to the same value.
The second session variable is pointing to a primitive (value) type so they have separate copies of value.
In the first example your session variable is pointing to a reference so it gets updated because the two references are pointing to the same value.
so before assigning to the session you convert it into Json and then assign
HttpContext.Current.Session[listModelType + "ListModel"] = JsonConvert.SerializeObject(listModel);
Note: JsonConvert is from name space Newtonsoft.Json namespace in c#
In second line if value changes in listModel object that does't reflect session.
but when you want to retreive the value from session you should convert to object form Json
if (HttpContext.Current.Session[listModelType + "ListModel"] != null)
{
listModel = JsonConvert.DeserializeObject<*CLASS name of lsitmodel*>((string)HttpContext.Current.Session[listModelType + "ListModel"]);
}
The correct way should be:
int i = 0;
i++;
HttpContext.Current.Session["i"] = i;
the HttpContext.Current.Session["i"] remains 1.
In the first example your session variable is pointing to a reference so it gets updated because the two references are pointing to the same value.
so before assigning to the session you convert it into Json and then assign
HttpContext.Current.Session[listModelType + "ListModel"] = JsonConvert.SerializeObject(listModel);
Note: JsonConvert is from name space Newtonsoft.Json namespace in c#
In second line if value changes in listModel object that does't reflect session. but when you want to retreive the value from session you should convert to object form Json
if (HttpContext.Current.Session[listModelType + "ListModel"] != null) {
listModel = JsonConvert.DeserializeObject<*CLASS name of lsitmodel*>((string)HttpContext.Current.Session[listModelType + "ListModel"]);
}

Retrieve a value from SQLParameter in C#

I am trying to retrieve a value from a List. The list is already filled but I keep getting an error saying Object reference not set to an instance of an object when I try to execute this code:
I created a global variable
private static readonly string _isDevItemParamName = "#DevItem";
In my method I call:
var devItem = sqlParams.Where(p => p.ParameterName == _isDevItemParamName).First();
This is where the error seems to occur when I do breakpoints.
Check sqlParams is null or contains any parameters and also use firstordefault instead of first because if the parameter is not available it should not through any exception.

Object reference not set to an instance of an object while reading file properties from SP2010

I have sharepoint document library which has custom field called "DocumentType" this is not mandatory field. When I am trying to read this property using the below code, when value is there in this field its working fine but where its value empty giving the error "Object reference not set to an instance of an object." If value is not there I need to pass empty string for further logic, how can I handle this?
SPFile spFile=Web.GetFile(Context.Request.Url.ToString());
string spDocumentType=string.Empty;
if (spFile.Properties["DocumentType"].ToString() == "INV") *In this line exception throwing where value is empty in this field in the doc library.
{
spDocumentType = spFile.Properties["DocumentType"].ToString();
}
do like this:
if(spFile.Properties["DocumentType"] !=null)
{
spDocumentType = spFile.Properties["DocumentType"].ToString() == "INV" ? spFile.Properties["DocumentType"].ToString() : "";
}
else
{
spDocumentType ="";
}
Change this piece of code:
spFile.Properties["DocumentType"].ToString()
To this:
Convert.ToString(spFile.Properties["DocumentType"])
While ToString() throws the exception you're getting when the value is null, the Convert.ToString() method tests for null and returns an empty string.

DataTable; Object reference not set to an instance of an object

I have the following line of code,
sitetb.Text = sitesformdataset.Tables["Sites"].Rows[_selectedindex].Field<string>("SiteName").ToString();
This returns the
System.NullReferenceException; Object reference not set to an instance
of an object.
I know why it is returning this exception and thats because there is nothing in that cell.
How do I correctly handle this?
try
if(sitesformdataset!=null&&sitesformdataset.Tables["Sites"]!=null)
{
sitetb.Text = sitesformdataset.Tables["Sites"].Rows[_selectedindex].Field<string>("SiteName").ToString();
}
You don't need to call .ToString()
.Field<string>("SiteName") convert to string , you don't need to call tostring again.
sitetb.Text = sitesformdataset.Tables["Sites"].Rows[_selectedindex].Field<string>("SiteName");
if your cell value null then you can't call ToString method of null object, that will fail.
If you have already checked for _selectedindex != -1 and being in the correct range the correct the code like this:
sitetb.Text = (sitesformdataset.Tables["Sites"].Rows[_selectedindex].Field<string>("SiteName")?? "").ToString();
it is the short writing of this:
var cellContetnt = sitesformdataset.Tables["Sites"].Rows[_selectedindex].Field<string>("SiteName");
if(cellCount == null)
sitetb.Text = "";
else
sitetb.Text = cellContent.ToString();

Session object changes when object is updated in C#

I have this really weird problem and I'm sure I'm missing something obvious here. I have these two lines:
HttpContext.Current.Session[listModelType + "ListModel"] = listModel;
listModel.ProductRows = new Collection<ProductRow>(listModel.ProductRows.Where(r => r.ParentRowId == 0).ToList());
After the second line is executed my session object is updated as well (according to "Watch" in Visual Studio)
What am I missing here?
I have tried
int i = 0;
HttpContext.Current.Session["i"] = i;
i++;
and HttpContext.Current.Session["i"] remains 0.
See value types and reference types.
The int is a value type so will be stored "as-is" on the moment of assignment; your listModel is a reference type so you store a reference to the object in your session, not the value of the object.
You'll have to create a new instance of listModel if you want the one in your session untouched.
In your first example you are storing a reference to the object (The lists memory location). So if the list is updated it will reflect in the session. This is a reference type.
In the second example you are using a value type:
int i = 0;
HttpContext.Current.Session["i"] = i;
i++;
You declare i and set it to 0 (Value type)
You store the value 0 in the session. (Not the memory location of i)
You increment i but the session still has the value 0
In the first example your session variable is pointing to a reference so it gets updated because the two references are pointing to the same value.
The second session variable is pointing to a primitive (value) type so they have separate copies of value.
In the first example your session variable is pointing to a reference so it gets updated because the two references are pointing to the same value.
so before assigning to the session you convert it into Json and then assign
HttpContext.Current.Session[listModelType + "ListModel"] = JsonConvert.SerializeObject(listModel);
Note: JsonConvert is from name space Newtonsoft.Json namespace in c#
In second line if value changes in listModel object that does't reflect session.
but when you want to retreive the value from session you should convert to object form Json
if (HttpContext.Current.Session[listModelType + "ListModel"] != null)
{
listModel = JsonConvert.DeserializeObject<*CLASS name of lsitmodel*>((string)HttpContext.Current.Session[listModelType + "ListModel"]);
}
The correct way should be:
int i = 0;
i++;
HttpContext.Current.Session["i"] = i;
the HttpContext.Current.Session["i"] remains 1.
In the first example your session variable is pointing to a reference so it gets updated because the two references are pointing to the same value.
so before assigning to the session you convert it into Json and then assign
HttpContext.Current.Session[listModelType + "ListModel"] = JsonConvert.SerializeObject(listModel);
Note: JsonConvert is from name space Newtonsoft.Json namespace in c#
In second line if value changes in listModel object that does't reflect session. but when you want to retreive the value from session you should convert to object form Json
if (HttpContext.Current.Session[listModelType + "ListModel"] != null) {
listModel = JsonConvert.DeserializeObject<*CLASS name of lsitmodel*>((string)HttpContext.Current.Session[listModelType + "ListModel"]);
}

Categories