从字符串“”转换输入“整数”无效
当我尝试运行以下代码时,我得到从字符串“”到类型“整数”的转换无效。错误。
Dim maj = (From c In connect.Courses _
Where c.COTRequired = CBool("True") _
Select c.CourseID, c.CourseName, c.CreditHours).Except _
(From en In connect.Enrollments _
Join s In connect.Sections On en.SectionID Equals s.SectionID _
Join cs In connect.Courses On s.CourseID Equals cs.CourseID _
Join st In connect.Students On en.StudentID Equals st.StudentID _
Order By cs.CourseName _
Where st.StudentID = CInt(SID) _
Select cs.CourseID, cs.CourseName, cs.CreditHours)
Dim maj2 = (From m2 In maj _
Select m2.CreditHours).Sum().ToString
这是错误详细信息。我知道需要进行某种类型的转换,但无法准确确定是哪一种类型。
System.InvalidCastException 未由用户代码处理 Message="从字符串“”到类型“整数”的转换无效。” 源=“系统.Data.Linq” 堆栈跟踪: 在 System.Data.Linq.SqlClient.QueryConverter.VisitInvocau(InitationExpression 调用) 在 System.Data.Linq.SqlClient.QueryConverter.VisitInner(表达式节点) 在 System.Data.Linq.SqlClient.QueryConverter.Visit(表达式节点) 在 System.Data.Linq.SqlClient.QueryConverter.VisitExpression(表达式 exp) 在 System.Data.Linq.SqlClient.QueryConverter.VisitBinary(BinaryExpression b) 在 System.Data.Linq.SqlClient.QueryConverter.VisitInner(表达式节点) 在 System.Data.Linq.SqlClient.QueryConverter.Visit(表达式节点) 在 System.Data.Linq.SqlClient.QueryConverter.VisitExpression(表达式 exp) 在 System.Data.Linq.SqlClient.QueryConverter.VisitWhere(表达式序列,LambdaExpression 谓词) 在 System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) 在 System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) 在 System.Data.Linq.SqlClient.QueryConverter.VisitInner(表达式节点) 在 System.Data.Linq.SqlClient.QueryConverter.Visit(表达式节点) 在 System.Data.Linq.SqlClient.QueryConverter.VisitSelect(表达式序列,LambdaExpression 选择器) 在 System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) 在 System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) 在 System.Data.Linq.SqlClient.QueryConverter.VisitInner(表达式节点) 在 System.Data.Linq.SqlClient.QueryConverter.Visit(表达式节点) 在 System.Data.Linq.SqlClient.QueryConverter.VisitExcept(表达式源1,表达式源2) 在 System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) 在 System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) 在 System.Data.Linq.SqlClient.QueryConverter.VisitInner(表达式节点) 在 System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(表达式节点) 在System.Data.Linq.SqlClient.SqlProvider.BuildQuery(表达式查询,SqlNodeAnnotations注释) 在 System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(表达式查询) 在 System.Data.Linq.DataQuery1.System.Collections.Generic.IEnumerable
1..ctor(IEnumerable1 集合) 在 System.Linq.Enumerable.ToList[TSource](IEnumerable
1 源) 在 System.Data.Linq.Provider.BindingList.Create[T](DataContext 上下文,IEnumerable1 序列) 在 System.Data.Linq.DataQuery
1.GetNewBindingList() 在 System.Data.Linq.DataQuery`1.System.ComponentModel.IListSource.GetList() 在 System.Windows.Forms.ListBindingHelper.GetList(对象列表) 在 System.Windows.Forms.ListBindingHelper.GetList(对象数据源,字符串数据成员) 在 System.Windows.Forms.BindingSource.ResetList() 在 System.Windows.Forms.BindingSource.set_DataSource(对象值) 在 C:\Users\Charles.McBeth\Documents\School\ProgramManagement\Final Project\Final Project\Final Project\Form1.vb 中的 WindowsApplication1.Form1.ComboBox1_SelectedIndexChanged(Object sender, EventArgs e):第 68 行 在 System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e) 在 System.Windows.Forms.ComboBox.set_SelectedIndex(Int32 值) 在 System.Windows.Forms.ComboBox.RefreshItems() 在 System.Windows.Forms.ComboBox.OnDataSourceChanged(EventArgs e) 在 System.Windows.Forms.ListControl.SetDataConnection(对象 newDataSource,BindingMemberInfo newDisplayMember,布尔力) 在 System.Windows.Forms.ListControl.set_DataSource(对象值) 内部异常:System.FormatException Message="输入字符串的格式不正确。" 来源=“Microsoft.VisualBasic” 堆栈跟踪: 在 Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(字符串值,NumberFormatInfo NumberFormat) 在 Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(字符串值) 内部异常:
When I try to run the following code I get a Conversion from string "" to type 'Integer' is not valid. error.
Dim maj = (From c In connect.Courses _
Where c.COTRequired = CBool("True") _
Select c.CourseID, c.CourseName, c.CreditHours).Except _
(From en In connect.Enrollments _
Join s In connect.Sections On en.SectionID Equals s.SectionID _
Join cs In connect.Courses On s.CourseID Equals cs.CourseID _
Join st In connect.Students On en.StudentID Equals st.StudentID _
Order By cs.CourseName _
Where st.StudentID = CInt(SID) _
Select cs.CourseID, cs.CourseName, cs.CreditHours)
Dim maj2 = (From m2 In maj _
Select m2.CreditHours).Sum().ToString
Here is the error detail. I know there is some type of conversion that needs to take place, but am having trouble pinning down exactly which one.
System.InvalidCastException was unhandled by user code
Message="Conversion from string "" to type 'Integer' is not valid."
Source="System.Data.Linq"
StackTrace:
at System.Data.Linq.SqlClient.QueryConverter.VisitInvocation(InvocationExpression invoke)
at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
at System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
at System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp)
at System.Data.Linq.SqlClient.QueryConverter.VisitBinary(BinaryExpression b)
at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
at System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
at System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp)
at System.Data.Linq.SqlClient.QueryConverter.VisitWhere(Expression sequence, LambdaExpression predicate)
at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
at System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
at System.Data.Linq.SqlClient.QueryConverter.VisitSelect(Expression sequence, LambdaExpression selector)
at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
at System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
at System.Data.Linq.SqlClient.QueryConverter.VisitExcept(Expression source1, Expression source2)
at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
at System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.DataQuery1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
1..ctor(IEnumerable
at System.Collections.Generic.List1 collection)
1 source)
at System.Linq.Enumerable.ToList[TSource](IEnumerable
at System.Data.Linq.Provider.BindingList.Create[T](DataContext context, IEnumerable1 sequence)
1.GetNewBindingList()
at System.Data.Linq.DataQuery
at System.Data.Linq.DataQuery`1.System.ComponentModel.IListSource.GetList()
at System.Windows.Forms.ListBindingHelper.GetList(Object list)
at System.Windows.Forms.ListBindingHelper.GetList(Object dataSource, String dataMember)
at System.Windows.Forms.BindingSource.ResetList()
at System.Windows.Forms.BindingSource.set_DataSource(Object value)
at WindowsApplication1.Form1.ComboBox1_SelectedIndexChanged(Object sender, EventArgs e) in C:\Users\Charles.McBeth\Documents\School\ProgramManagement\Final Project\Final Project\Final Project\Form1.vb:line 68
at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ComboBox.set_SelectedIndex(Int32 value)
at System.Windows.Forms.ComboBox.RefreshItems()
at System.Windows.Forms.ComboBox.OnDataSourceChanged(EventArgs e)
at System.Windows.Forms.ListControl.SetDataConnection(Object newDataSource, BindingMemberInfo newDisplayMember, Boolean force)
at System.Windows.Forms.ListControl.set_DataSource(Object value)
InnerException: System.FormatException
Message="Input string was not in a correct format."
Source="Microsoft.VisualBasic"
StackTrace:
at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value)
InnerException:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑是这样的:
SID是什么,有没有可能是空字符串?如果代码是一个空字符串,您希望它做什么?
I suspect it's this bit:
What is SID, and is there a possibility that it's an empty string? What do you want the code to do if it is an empty string?
我能看到的唯一显式转换为整数的是
CInt(SID)
-SID
字段是什么类型?如果它包含字母数字值,那可能就是你的罪魁祸首。The only explicit conversion to integer I can see is
CInt(SID)
- what type is theSID
field? If it contains alphanumeric values, that might be your culprit.我发现 LINQ to SQL 的局限性无法忍受,因此我开发了自己的数据基础层。然而,我发现当我需要使用 LINQ to SQL 时,它有助于将所有计算从代码中提取出来。
在你的情况下,我会尝试:
请注意,VB.NET 有常量 True 和 False (无引号),它们是实际的布尔值。
I find LINQ to SQL's limitations intolerable, so I developed my own data foundation layer. However I have found that when I needed to use LINQ to SQL it helped to hoist all calculations out of the code.
In your case I would try:
Note that VB.NET has the constants True and False (no quotes) that are actual boolean values.