"Input string was not in a correct format"

Discussion in 'ASP .Net' started by phmyhn, Jun 24, 2003.

  1. phmyhn

    phmyhn Guest

    I have two web pages, one is viewlarger.aspx, another one is
    shoppingcart.aspx. On the viewlarger.aspx, when clicking "add to cart"
    image button, the sub appends the id (passed from another page from a
    hyperlink column) into a session. the code is as following:
    Private Sub ibtAddtoCart_Click(ByVal sender As System.Object,
    ByVal e As System.Web.UI.ImageClickEventArgs) Handles
    ibtAddtoCart.Click
    idStr = Session("ids")
    pid = Request.QueryString("id")
    Dim sb As New StringBuilder(idStr)
    idStr = sb.Append(" ").Append(pid).ToString
    Session("ids") = idStr
    'Label1.Text = "idStr:" + CType(Session("ids"), String)
    Response.Redirect("shoppingcart.aspx")
    End Sub
    I use a label to test if the session gets the id, it shows me that
    this sub works well, each time I click the "add to cart" button, the
    id is appended to the session.
    then on shoppingcart.aspx, in onload, I pick up the id string, call a
    function to get the items from the Access database. The function first
    pick up all records and fill them into the datatable, then I delete
    those datarows which don't match the ids that I pick up from the
    session. I bind the datatable to the datagrid. When compile, I got
    this error message "Input string was not in a correct format" which
    complain the line in the function. Here is the function:
    Public Class ShoppingCart
    Public Function getCart(ByVal ids As String) As DataTable

    Dim conStr As String =
    System.Configuration.ConfigurationSettings.AppSettings("conString")
    Dim oleCon As New OleDbConnection(conStr)
    Dim cmdStr As String = "Select item_ID, item_name, price,
    P_S_T, G_S_T from Item"
    Dim oleAdapter As New OleDbDataAdapter(cmdStr, oleCon)

    Dim dsItems As New DataSet()
    oleAdapter.Fill(dsItems, "items")

    Dim idArr As String() = Split(ids)
    Dim dr As DataRow
    Dim dt As DataTable = dsItems.Tables("items")
    For Each dr In dt.Rows
    Dim id_to_check As Integer = dr.Item("item_ID")
    Dim id As String
    Dim found As Boolean = False
    For Each id In idArr
    If (id_to_check = Integer.Parse(id)) Then
    ' this line is the red line when compiling'
    found = True
    End If
    Next
    If Not found Then
    dr.Delete()
    End If
    Next
    Return dt
    End Function

    in onload, code is like:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    Dim cart As New Store.ShoppingCart()
    Dim str As String = CType(Session("ids"), String)
    'Dim str As String = "1 3 4" 'if I use this string
    instead of using the string from session, no error at all.
    Dim dt As DataTable = cart.getCart(str)

    If Not IsPostBack Then
    dgCart.DataSource = dt.DefaultView
    dgCart.DataBind()
    End If

    the item_ID in item table in Access, is defined as "Number".
    I'm confused with this error message. I have tested the session result
    on the viewlarger.aspx, and with a string " 1 3 4". Both are ok. Why
    couldn't I use the string got from the session?
    If you know the answer to my problem, please help me!
    phmyhn, Jun 24, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TJS
    Replies:
    0
    Views:
    1,359
  2. sbox
    Replies:
    2
    Views:
    15,361
    Ben Miller [MSFT]
    Feb 15, 2004
  3. Replies:
    0
    Views:
    623
  4. Maria
    Replies:
    4
    Views:
    8,494
    Maria
    Apr 4, 2004
  5. chongo
    Replies:
    2
    Views:
    8,177
    Edward
    Jul 8, 2004
Loading...

Share This Page