Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jul 21, 2010 10:35 PM by sansan
Participant
951 Points
573 Posts
Jul 21, 2010 10:26 PM|LINK
Hi everyone, I having a problem with linq. The following line
Emp objEmp = tblEmps.SingleOrDefault(p => p.empno ==Int32.Parse(this.txtEmpno.Text));
generates the following error message: System.InvalidCastException was caught Message=Specified cast is not valid. Source=System.Data.Linq StackTrace: at System.Data.Linq.SqlClient.SqlProvider.Execute(Exp ression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll( Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) at System.Data.Linq.SqlClient.SqlProvider.System.Data .Linq.Provider.IProvider.Execute(Expression query) at System.Data.Linq.Table`1.System.Linq.IQueryProvide r.Execute[TResult](Expression expression) at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source, Expression`1 predicate) at SampleCS.CRUD.btnSearch_Click(Object sender, EventArgs e) in p:\JIMMY PROGRAMMING IN 2010\Linq_Samples\CRUD.aspx.cs:line 48 InnerException: ------------------ Please let me know how to fix this problem. Thanks. Cecilia
All-Star
53942 Points
8147 Posts
Jul 21, 2010 10:35 PM|LINK
Check if the empno is an integer to compare
and make sure you check for null values in the TextEmpno.Text
if the text is blank, it will fail in casting to integer
a quick solution for that will be using Convert.ToString on both sides which again depends on your scenario
as
Convert.ToString(p.empno)==txtEmpno.Text
correct me if I am wrong
MayLam
Participant
951 Points
573 Posts
LINQ - Specified cast is not valid
Jul 21, 2010 10:26 PM|LINK
Hi everyone,
I having a problem with linq. The following line
generates the following error message:
<div></div>System.InvalidCastException was caught
Message=Specified cast is not valid.
Source=System.Data.Linq
StackTrace:
at System.Data.Linq.SqlClient.SqlProvider.Execute(Exp ression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll( Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
at System.Data.Linq.SqlClient.SqlProvider.System.Data .Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.Table`1.System.Linq.IQueryProvide r.Execute[TResult](Expression expression)
at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
at SampleCS.CRUD.btnSearch_Click(Object sender, EventArgs e) in p:\JIMMY PROGRAMMING IN 2010\Linq_Samples\CRUD.aspx.cs:line 48
InnerException:
------------------
Please let me know how to fix this problem. Thanks.
Cecilia
sansan
All-Star
53942 Points
8147 Posts
Re: LINQ - Specified cast is not valid
Jul 21, 2010 10:35 PM|LINK
Check if the empno is an integer to compare
and make sure you check for null values in the TextEmpno.Text
if the text is blank, it will fail in casting to integer
a quick solution for that will be using Convert.ToString on both sides which again depends on your scenario
as
Convert.ToString(p.empno)==txtEmpno.Text
correct me if I am wrong