I have following code in asp.net core using multiple or in where statement. it always propmt error saying "An unhandled exception occurred while processing the request.
NullReferenceException: Object reference not set to an instance of an object.
lambda_method"
if (!String.IsNullOrEmpty(searchString))
{
mTickets = from mt in mTickets
where
(mt.TicketServiceFullNo.Contains(searchString)) || (mt.DeviceNo.Contains(searchString)) select mt;
}
return View(await mTickets.AsNoTracking().ToListAsync());
However, If i just use one or condition in where. it will work. I have no idea why. Please help. thanks
this will work
if (!String.IsNullOrEmpty(searchString))
{
mTickets = from mt in mTickets
where
(mt.TicketServiceFullNo.Contains(searchString)) select mt;
}
return View(await mTickets.AsNoTracking().ToListAsync());
It's almost certain that mt.DeviceNo is null.
You'll need to replace:
(mt.DeviceNo.Contains(searchString))
with
(mt.DeviceNo != null && mt.DeviceNo.Contains(searchString))
or
(mt.DeviceNo?.Contains(searchString))
if you're using C# 6.0
Related
Please some one provide me complete tutorial to insert only unique value in database.If any duplicate data is tried to insert it should show error message.
please provide me complete tutorials because i am beginner
Using LINQ
Your_Context_DB context = new Your_Context_DB();
public bool function(Your_entity entity)
{
if(context.Entity.Where(x => x.Id == entity.Id).Count() > 0) {
return true; //The value has been existed.
}
else
return false; //The value has not been existed.
}
In My MVC project, I am using session values like
var empId = Convert.ToInt32(Session["EmpId"].ToString());
I am getting Exception:
"An exception of type 'System.NullReferenceException' occurred in Project.Web.dll but was not handled in user code.
Additional information: Object reference not set to an instance of an object."
This error occurs when you call a method on the null object. In your case the value of Session["EmpId"] is NULL.
Which means you are calling NULL.ToString(), which is invaild hence it throws error.
You can avoid the error using null coaleascing operator or simply check null before performing any opearation on it.
Solution:
if(Session["EmpId"] == null)
//do something
else
var empId = Convert.ToInt32(Session["EmpId"].ToString());
Alternatively you can check my blog post on it
Before use first check is it null or not.
var empId = Session["EmapId"] != null ? Convert.ToInt32(Session["EmapId"]) : 0;
You have to check null as shown below :-
var empId = Convert.ToInt32((Session["EmpId"] ?? 0).ToString());
A more efficient way to accomplish your requirement :-
int temp = 0;
var empId = int.TryParse( Convert.ToString( Session["EmpId"] ),out temp );
I have the following entity property defined as the following metadata shows:
{"name":"website","dataType":"String",
"validators":[
{"name":"string"},
{"messageTemplate":"'%displayName%' is not valid",
"pattern":"^$|(^http|^https)://[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(/S*)?",
"name":"regExValidator"}]}
When I attempt to call entityAspect.validateProperty("website"), and the value of the website property is null, the call to the validateProperty() method throws the following exception:
"Unable to get property 'complexAspect' of undefined or null
reference"
I wouldn't expect this behavior since it's possible that the website entity property could be null. It looks like there may be a null reference handling bug in the validateProperty method:
In Breeze.debug.js:
proto.validateProperty = function (property, context) {
var value = this.getPropertyValue(property); // performs validations
if (value.complexAspect) { // THROWS EXCEPTION IF 'value' IS NULL
return validateTarget(value);
}
context = context || {};
context.entity = this.entity;
if (typeof(property) === 'string') {
context.property = this.entity.entityType.getProperty(property, true);
context.propertyName = property;
} else {
context.property = property;
context.propertyName = property.name;
}
return this._validateProperty(value, context);
};
Just curious if I'm doing something wrong, or if this is just a bug?
Thanks,
Richard
Edit: This was fixed in Breeze v1.3.0, available now.
This is a bug, and will be fixed in the next release, out later this week. ... and thx for finding and reporting it :)
I have method with code:
using (var cc = new MyDBContext())
{
var myList = (from user in cc.Users
where user.UserGroup.Name == "smth"
orderby user.ID ascending
select user);
if (startIndex != null)
return View(myList.Skip((int)startIndex).Take(50));
else
return View(myList);
}
In view I catch exception The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
Some people says that .ToList() must solve problem, but it throws an exception with myList.ToList() too. What is my problem?
P.S. in debug mode I have exception at #item.FullName in view, but if I move mouse on FullName property I can see correct value.
Sorry for my bad english.
Take the "return View()" statements outside of the "using" block completely. That will ensure you have retrieved the complete data sets before your DbContext object is disposed. Like this:
using (var cc = new MyDBContext())
{
var myList = (linq).ToList();
}
return View(myList);
I'm pretty sure the problem is that you are returning an IEnumerable to the View, which means the items haven't actually been retrieved yet. But when you return the object to your View, the DbContext is getting disposed before the view has a chance to retrieve the rows.
The problem was in lazy loaded sub property of User entity. I add to link statement Include("PropName") and it works good.
I am using the repository pattern in a asp.net mvc application (v3) and EntityFramework (v4).
I would like to add to the repository paging functionality. I have the following sample code:
public PagedResult<T> GetPaged(int offset = 0, int limit = PagedResult<T>.NoLimit, Expression<Func<T, bool>> predicate = null)
{
var res = BaseQ.Where(_predicate);//.Skip(offset);
if (predicate != null)
res = res.Where(predicate);
res = res.Skip(offset);
if (limit != PagedResult<T>.NoLimit)
{
res = res.Take(limit);
}
return new PagedResult<T>(res, res.Count(), offset, limit);
}
However, this will not work because Entity framework throws an exception that I should call OrderBy before Skip.
I am wondering how can I implement this in a generic way so that the order by expression will be plugged in from the outside. I would then pass it as a parameter.
Perhaps there is also another way of working around that problem.
[EDIT] I found out that this can be done by passing in the name of the property and creating an expression out of that, but I would like to actually just use it as I am using it in the OrderBy. So just pass in (c=>c.ID) for example.
Thanks in advance for any ideas
I have decided to go with passing in a string and creating the expression from it