开发者

Conversion from string "" to type 'Integer' is not valid

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.Vi开发者_JAVA技巧sitInner(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() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at System.Data.Linq.Provider.BindingList.Create[T](DataContext context, IEnumerable1 sequence) at System.Data.Linq.DataQuery1.GetNewBindingList() 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:


I suspect it's this bit:

st.StudentID = CInt(SID)

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?


The only explicit conversion to integer I can see is CInt(SID) - what type is the SID field? If it contains alphanumeric values, that might be your culprit.


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:

Dim intSID = CInt(SID)
Dim maj = ...
        Where c.COTRequired = True _
        ...
        Where st.StudentID = intSID _
        ...

Note that VB.NET has the constants True and False (no quotes) that are actual boolean values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜