Internet Engineer
Internet Engineer

Reputation: 2534

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

I get the following error when trying to create a default row in the asp.net dropdown: Conversion from string "x" to type 'Integer' is not valid.

**

Dim DefaultSalesAgent = (From o In db.PayoutRegisters
                         Join s In db.SalesAgents On s.SalesAgentId Equals o.SalesAgentID
                         Where o.PayoutRegisterID = PayoutRegisterID
                         Select o.PayoutRegisterID, x = s.CSRName + " (" + o.PaidThruDate.ToString + ")").ToList




    If DefaultSalesAgent.Count > 0 Then
        CmbSalesAgents.Items.Insert(0, New ListItem(DefaultSalesAgent.Item("x").ToString, PayoutRegisterID))
    Else
        CmbSalesAgents.Items.Insert(0, New ListItem("Select Sales Agent Payout Register", 0))
    End If

Upvotes: 0

Views: 567

Answers (2)

RandomUs1r
RandomUs1r

Reputation: 4188

If you specifically need to insert the row based off a string value, you can replace

DefaultSalesAgent.Item("x")

With

DefaultSalesAgent.Item(DefaultSalesAgent.FindIndex(x => x.StartsWith("x")));

You can also use contains instead of startswith depending on your needs. Please note this is only if you specifically need to look for strings as it's a lot more expensive than accessing an index in the list.

Upvotes: 0

Peter
Peter

Reputation: 493

Since DefaultSalesAgent is a list object then you should access its items through integer index not a string:

DefaultSalesAgent.Item(integer_index)

Upvotes: 1

Related Questions