Input string not in correct format.

Last post 10-26-2009 10:57 AM by Rajneesh Verma. 8 replies.

Sort Posts:

  • Input string not in correct format.

    10-26-2009, 6:59 AM
    • Member
      688 point Member
    • ajw
    • Member since 08-29-2006, 4:16 PM
    • UK
    • Posts 532



    Input string was not in a correct format.

    Server Error in '/Build' Application.

    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:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [FormatException: Input string was not in a correct format.]
       System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +2755599
       System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +112
       System.String.System.IConvertible.ToInt32(IFormatProvider provider) +43
       System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) +293
       System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges) +264
       System.Web.UI.WebControls.Parameter.get_ParameterValue() +66
       System.Web.UI.WebControls.ParameterCollection.GetValues(HttpContext context


    Here is my code:


    Imports System.Data
    Imports System.Data.SqlClient
    Partial Class Admin_Default
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            ' DropDownList1.Items.Insert(0, "choose me!")
            BindData()
        End Sub
        Sub BindData()

            '1. Create a connection

                  Dim myConnection As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("hallam-cmsConnectionString").ConnectionString)

            '2. Create the command object, passing in the SQL string
            Const strSQL As String = "SELECT PageID, PageName FROM Pages"
            Dim myCommand As New SqlCommand(strSQL, myConnection)

            myConnection.Open()
            '3. Create the DataReader
            Dim objDR As SqlDataReader
            objDR = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

            'Databind the DataReader to the listbox Web control
            DropDownList1.DataSource = objDR
            DropDownList1.DataBind()

            'Add a new listitem to the beginning of the listitemcollection
            DropDownList1.Items.Insert(0, New ListItem("-- Choose a Page --"))

        End Sub


        Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
            Dim dv As DataView
            Dim PageType As Integer
            dv = CType(SqlDataSource3.Select(DataSourceSelectArguments.Empty), DataView)
            PageType = CType(dv.Table.Rows(0)(0), Integer)
            If (PageType = 2) Then
                Image1.ImageUrl = "~/images/dev-diag.gif"
            Else
                Image1.ImageUrl = "~/images/dev-diag-sidebar.gif"
            End If
        End Sub
    End Class



    Any ideas, thanks


    Andy

  • Re: Input string not in correct format.

    10-26-2009, 7:04 AM
    Answer
    • Member
      688 point Member
    • ajw
    • Member since 08-29-2006, 4:16 PM
    • UK
    • Posts 532

    Here is the culprit:


    'DropDownList1.Items.Insert(0, New ListItem("-- Choose a Page --"))

    What's wrong with it though?


    Andy

  • Re: Input string not in correct format.

    10-26-2009, 7:21 AM
    • Member
      439 point Member
    • net.hitesh
    • Member since 09-03-2009, 5:33 AM
    • Jaipur
    • Posts 126

    We can't debug it bcoz we dnt have database but this is very common problem

    we get this error when our query is wrong somewwhere or our input don't match to query

    Hitesh Sharma
    .net Developer
  • Re: Input string not in correct format.

    10-26-2009, 7:26 AM
    Answer
    • Participant
      1,917 point Participant
    • nimish_soft
    • Member since 10-22-2009, 1:23 PM
    • India
    • Posts 376

    why dont u simpley write:

    DropDownList1.Items.Insert(0, "Some Text")

    - Nimish Garg
    Software Developer
    IndiaMART InterMESH Limited, Noida

    Blog: http://nimishgarg.blogspot.com/

    Plz do click "Mark as Answer" on the post that helped you. This will also give you point and help readers to know which post solved your issue and make their search easy.
  • Re: Input string not in correct format.

    10-26-2009, 7:30 AM
    Answer
    • Member
      439 point Member
    • net.hitesh
    • Member since 09-03-2009, 5:33 AM
    • Jaipur
    • Posts 126

    use DropDownList1.Items.Insert(0, "-- Choose a Page --")


    Hitesh Sharma
    .net Developer
  • Re: Input string not in correct format.

    10-26-2009, 7:32 AM
    • Contributor
      3,082 point Contributor
    • paindaasp
    • Member since 12-19-2007, 12:47 PM
    • Exit 3
    • Posts 530

    Could it be because you are not assigning the DataTextField and DataValueFields for your DropDownList?  When I run your code I don't get your error, but the DropDownList doesn't contain the data I would expect.

     

            DropDownList1.DataTextField = "PageName"
            DropDownList1.DataValueField = "PageID"
    


     

  • Re: Input string not in correct format.

    10-26-2009, 9:22 AM
    Answer
    • Member
      48 point Member
    • monoliths
    • Member since 10-23-2009, 2:30 PM
    • TX
    • Posts 18

    Try something along these lines:


    Sub AddListItem(ByRef objDDL as DropDownList, sText as string, sValue as string)

                dim lItem as new ListItem(sText, sValue)

                objDDL.items.add(lItem)

    End Sub

               

    Steven
    ASP/VB.net novice
  • Re: Input string not in correct format.

    10-26-2009, 10:33 AM
    Answer
    • Member
      688 point Member
    • ajw
    • Member since 08-29-2006, 4:16 PM
    • UK
    • Posts 532

    The problem is:

    The dropdownlist, if I add another item with   not the right type then it throws a wobbly.

    sorted with

    <asp:DropDownList ID="DropDownList1" runat="server"  
                        DataTextField="PageName" DataValueField="PageID"
                        DataSourceID="SqlDataSource1" AppendDataBoundItems="true" AutoPostBack="True">
           <asp:ListItem Selected="True" Value ="">Choose a Page</asp:ListItem>
        </asp:DropDownList>

    Protected Sub SqlDataSource2_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource2.Selecting
            If Not Page.IsPostBack Then
                e.Cancel = True
            End If
        End Sub

    Problem is that now if I choose the 'choose a page' item after the sqldatasource2 has started binding then I get an 'object not set to instance of an object' because another piece of code:

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
            Dim dv As DataView
            Dim PageType As Integer
            dv = CType(SqlDataSource3.Select(DataSourceSelectArguments.Empty), DataView)
            PageType = CType(dv.Table.Rows(0)(0), Integer)
            If (PageType = 2) Then
                Image1.ImageUrl = "~/images/dev-diag.gif"
            Else
                Image1.ImageUrl = "~/images/dev-diag-sidebar.gif"
            End If
        End Sub

    Is looking for a value for that item... So I'm thinking to alter tha stored procedure for the sqldatasource3 so it only fires if not null??... is that possible in SQL?

    Andy

  • Re: Input string not in correct format.

    10-26-2009, 10:57 AM
    Answer
    • Contributor
      3,278 point Contributor
    • Rajneesh Verma
    • Member since 07-15-2008, 6:22 AM
    • Lucknow, U.P. INDIA
    • Posts 745

    Hi,

    Andy

    ajw:
    '2. Create the command object, passing in the SQL string
            Const strSQL As String = "SELECT PageID, PageName FROM Pages"
            Dim myCommand As New SqlCommand(strSQL, myConnection)

            myConnection.Open()
            '3. Create the DataReader
            Dim objDR As SqlDataReader
            objDR = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

            'Databind the DataReader to the listbox Web control
            DropDownList1.DataSource = objDR
            DropDownList1.DataBind()

            'Add a new listitem to the beginning of the listitemcollection
            DropDownList1.Items.Insert(0, New ListItem("-- Choose a Page --"))


    If we use like following then???

    ----------------------------------

     con.Open();
                SqlCommand com = new SqlCommand("select * from Product", con);
                Int32 i = 1;
                ddlProduct.Items.Clear();
                ddlProduct.Items.Add("--Select--");
                ddlProduct.Items[0].Value = "0000";
                dr = com.ExecuteReader();
                while (dr.Read())
                {
                    ddlProduct.Items.Add(dr[1].ToString());
                    ddlProduct.Items[i].Value = dr[0].ToString();
                    i = i + 1;
                }
                dr.Close();
                con.Close();

    con.Open()

    Dim com As New SqlCommand("SELECT PageID, PageName FROM Pages",myConnection)

    myConnection.Open()

    Dim i As Int32 = 1

    DropDownList1.Items.Clear()

    DropDownList1.Items.Add("-- Choose a Page --")

    DropDownList1.Items(0).Value = "0000"

    Dim objDR As SqlDataReader

    objDR = com.ExecuteReader(CommandBehavior.CloseConnection)

    While objDR.Read()

        DropDownList1.Items.Add(objDR(1).ToString())

        DropDownList1.Items(i).Value = objDR(0).ToString()

        i = i + 1

    End While

    objDR.Close()


    Think about that both Data Text and Value fields are here.


    Thanks..


    RajneesH Verma
    Sr. Software Developer
    09742846246 (Karnataka)
    09839040266 (Uttar Pradesh)
    Technology Specialist at www.innovateonindia.com

    Please remember to click "Mark as Answer" on the post that helps you and close that thread which fulfill your requirement.
Page 1 of 1 (9 items)