none
result.Count throws Input string was not in a correct format! RRS feed

  • Question

  • Why does this give me the error below in the line below?  Sorry if this is simple!  I'm new to LINQ and have been looking for two days to fix this.  I see samples using this result.Count.

     

    Exception Details: System.FormatException: Input string was not in a correct format.
    Dim recordCount As Integer = result.Count
    _______________________________________

    Imports System.Linq

    Imports MyLinqToSqlProject

    Imports System.Collections.Generic

     

    Protected Sub SubCategoryNameLinkButton_Init(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim lb As LinkButton = sender

    Dim category_Id = lb.CommandArgument

    Dim ItemDc As New ItemDataContext

    Dim query As IQueryable(Of Item_Category) = ItemDc.Item_Category

    query = query.Where(Function(i As Item_Category) i.Item_Category_Id.Equals(category_Id))

    query = query.Where(Function(i As Item_Category) Not i.Parent_Category_Id.HasValue)

    Dim result As IEnumerable(Of Item_Category) = query.Select(Function(i As Item_Category) i)

    Dim recordCount As Integer = result.Count

    With lb

    If recordCount = 0 Then

    .Enabled = False

    Else

    .Enabled = True

    End If

    End With

    End Sub

    Monday, May 12, 2008 4:06 PM

Answers

  • You were on the right track.  Because I referenced label.commandArgument in the Init handler, the value was always a zero length string. 

     

    My logic was incorrect as well and it ended up working this simply.

     

    Protected Sub SubCategoryNameLinkButton_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim lb = sender

    Dim category_Id = lb.CommandArgument

    Dim ItemDc As New ItemDataContext

    Dim q = (From ic In ItemDc.Item_Category _

    Where ic.Parent_Category_Id.Equals(category_Id) _

    Select ic).Count()

    With lb

    If q = 0 Then

    .Enabled = False

    Else

    .Enabled = True

    End If

    End With

    End Sub

    Monday, May 12, 2008 6:24 PM

All replies

  • What is the type of 'category_Id'?

     

    Is this statement doing the right thing (in terms of comparing similar things):

      

         query = query.Where(Function(i As Item_Category) i.Item_Category_Id.Equals(category_Id))

     

    It will be hard to diagnose the problem without the context. You may need to try the query a little bit at a time and see which clause causes the error.

     

    Hope that helps,

     

    Samir

     

    Monday, May 12, 2008 5:06 PM
  • You were on the right track.  Because I referenced label.commandArgument in the Init handler, the value was always a zero length string. 

     

    My logic was incorrect as well and it ended up working this simply.

     

    Protected Sub SubCategoryNameLinkButton_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim lb = sender

    Dim category_Id = lb.CommandArgument

    Dim ItemDc As New ItemDataContext

    Dim q = (From ic In ItemDc.Item_Category _

    Where ic.Parent_Category_Id.Equals(category_Id) _

    Select ic).Count()

    With lb

    If q = 0 Then

    .Enabled = False

    Else

    .Enabled = True

    End If

    End With

    End Sub

    Monday, May 12, 2008 6:24 PM