ASP.NET   -  Input string was not in a correct format problem

Posted on 2005-05-05
Last Modified: 2006-11-18

I have a  function:

Public Shared Function GetProducts() As SqlDataReader

        Dim connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand("GetShoppingCartProducts", connection)
        command.CommandType = CommandType.StoredProcedure

        command.Parameters.Add("@CartID", SqlDbType.Char, 36)
        command.Parameters("@CartID").Value = shoppingCartID

            Return command.ExecuteReader(CommandBehavior.CloseConnection)

        Catch ex As Exception
            Throw ex

        End Try
    End Function

Which makes use of the variable (located in same class file):

  Private Shared ReadOnly Property shoppingCartID()
            Dim context As HttpContext = HttpContext.Current

            If context.Request.Cookies("Wind_CartID") Is Nothing Then
                Dim cartID As Guid = Guid.NewGuid()
                Dim cookie As New HttpCookie("Wind_CartID", cartID.ToString)
                Dim currentDate As DateTime = DateTime.Now
                Dim ts As New TimeSpan(10, 0, 0, 0)
                Dim expirationDate As DateTime = currentDate.Add(ts)
                cookie.Expires = expirationDate
                Return cartID.ToString()

                Return context.Request.Cookies("Wind_CartID").Value
            End If
        End Get
    End Property

And which calls the stored procedure (in an sql database):


            @CartID char(36)

      SELECT Product.ProductID, Product.Name, Product.Price, ShoppingCart.Quantity, product.Price*ShoppingCart.Quantity As Subtotal
      From ShoppingCart INNER Join Product
      ON ShoppingCart.ProductID = Product.ProductID
      WHERE ShoppingCart.CartID = @CartID

When I run the function I get:

Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:

Line 43:             grid.DataSource = ShoppingCart.GetProducts
Line 44:             grid.DataKeyField = "ProductID"
Line 45:             grid.DataBind()
Line 46:         End If
Line 47:     End Sub

Source File: C:\myCommerceSite\WindAddict\UserControls\ShoppingCart.ascx.vb    Line: 45

Any help would be greatly appreciated.
Question by:PaulCampbell
    LVL 1

    Accepted Solution

    Hi there,

    Check your stored proc return results.

    If your datagrid is using templates to render the output then I supsect you may have a NULL value being returned from the stored proc. The template dataitem cannot be NULL - hence the 'wrong format' section of the message.

    Check all of your stored proc return values and add an ISNULL(columnname, replacevalue) check in the stored proc to ensure that a value gets passed back.
    LVL 2

    Author Comment

    thanks a lot.

    didn't realize sql was case sensitive!

    LVL 1

    Expert Comment

    It can be case-sensitive or non-case-sensitive, it all depends on how the database is set up when it is created.

    Some applications that use SQL need the case sensitivity option. Generally speaking though it is a right pain in the a***.

    Glad to have helped.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    Suggested Solutions

    Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
    Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now