Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 19th, 2005, 04:52 AM
Authorized User
 
Join Date: Apr 2005
Location: , , .
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default Input string was not in a correct format

I get the following error on compilation, I've googled away without any luck, if anyone could please shed some light that would be great.

What I am trying to do is that if a value from DDL 1 is selected then DDL 2 gets populated with values. For example if DDL 1 is 1024, then DDL 2 returns 2*512 AND 4*256.

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 50: Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)
Line 51: DropDownList2.DataTextField = "MemConfig"
Line 52: DropDownList2.DataSource = MEMConfig("memAmount")
Line 53: DropDownList2.DataBind()
Line 54: End Sub

Stack Trace:

[FormatException: Input string was not in a correct format.]
Microsoft.VisualBasic.CompilerServices.Doubl eType.Parse(String Value, NumberFormatInfo NumberFormat) +184
Microsoft.VisualBasic.CompilerServices.Integ erType.FromString(String Value) +97

[InvalidCastException: Cast from string "memAmount" to type 'Integer' is not valid.]
Microsoft.VisualBasic.CompilerServices.Integ erType.FromString(String Value) +211
ASP.memory_aspx.DropDownList1_SelectedIndexC hanged(Object sender, EventArgs e) in \\boston.co.uk\storage\users\Dhwiren\memory.aspx:5 2
System.Web.UI.WebControls.ListControl.OnSele ctedIndexChanged(EventArgs e) +83
System.Web.UI.WebControls.DropDownList.Syste m.Web.UI.IPostBackDataHandler.RaisePostDataChanged Event() +13
System.Web.UI.Page.RaiseChangedEvents() +115
System.Web.UI.Page.ProcessRequestMain() +1099

MY CODE IS AS FOLLOWS
Code:
Function Memamount() As System.Data.DataSet
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=\\boston.co.uk\"& _
"storage\users\Dhwiren\memory.mdb"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

        Dim queryString As String = "SELECT [MemAmount].[Memory Amount] FROM [MemAmount]"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
        dataAdapter.SelectCommand = dbCommand
        Dim dataSet As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dataSet)

        Return dataSet
    End Function

    Sub Page_Load(Sender As Object, E As EventArgs)
        If Not Page.IsPostBack Then
        DropDownList1.DataTextField = "Memory Amount"
        DropDownList1.DataSource = Memamount()
        DropDownList1.DataBind()
        End If
    End Sub

Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)
        DropDownList2.DataTextField = "MemConfig"
        DropDownList2.DataSource = MEMConfig("memAmount")
        DropDownList2.DataBind()
End Sub


    Function MEMConfig(ByVal memAmount As Integer) As System.Data.DataSet
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=\\boston.co.uk\"& _
"storage\users\Dhwiren\memory.mdb"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

        Dim queryString As String = "SELECT [MemConfig].[MemConfig] FROM [MemConfig] WHERE ([MemConfig].[MemAmount] = "& _
"@MemAmount)"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dbParam_memAmount As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
        dbParam_memAmount.ParameterName = "@MemAmount"
        dbParam_memAmount.Value = memAmount
        dbParam_memAmount.DbType = System.Data.DbType.Int32
        dbCommand.Parameters.Add(dbParam_memAmount)

        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
        dataAdapter.SelectCommand = dbCommand
        Dim dataSet As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dataSet)

        Return dataSet
    End Function
Reply With Quote
  #2 (permalink)  
Old April 19th, 2005, 08:18 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

DropDownList2.DataSource = MEMConfig("memAmount")
...
Function MEMConfig(ByVal memAmount As Integer) As System.Data.DataSet

It would seem that you are trying to pass the string "memAmount" to a function that's expecting an Integer.

-Peter
Reply With Quote
  #3 (permalink)  
Old April 19th, 2005, 08:26 AM
Authorized User
 
Join Date: Apr 2005
Location: , , .
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is there any way in which I could get it to work?
Take the argument from the 1st DDL?
I've set the values as integer in the database, basically I have 2 DDL's and I would like to populate the second DDL by the 1st DDL is there an easier way? But it has to take the values from a Database.

Thanks for getting back Peter

Reply With Quote
  #4 (permalink)  
Old April 19th, 2005, 10:09 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Perhaps this is what you need:

DropDownList2.DataSource = MEMConfig(CType(DropDownList1.SelectedValue, Integer))

-Peter
Reply With Quote
  #5 (permalink)  
Old April 19th, 2005, 10:21 AM
Authorized User
 
Join Date: Apr 2005
Location: , , .
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter, thank you so much, you have helped me resolve my problem which I have been working on for the past 2 days.
Thank you for your time, it was very much appreciated.

D

Reply With Quote
  #6 (permalink)  
Old April 19th, 2005, 10:32 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You're welcome. Glad I could help.
Reply With Quote
  #7 (permalink)  
Old May 10th, 2005, 01:13 PM
Friend of Wrox
 
Join Date: Mar 2005
Location: , , .
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default

peter could u help me with my problem as well ? i get same error but do not know how to fix it! I have this asp.net script that supposed to update players table records.But when i run it i get this error. i only tried to change name feild and i get the this error . I also tried to change the brith date feiled and i got same error! My db is in access and i posted its field type below. Do u think there is some thing wrong with the update sql statement?

I be happy if an expert help me fix this broken code.Thanks

PLAYERS TABLE data type:

PLAERNO => NUMBER
NAME => TEXT
INITIALS => TEXT
BIRTH_DATE=> DATE/TIME
************ => TEXT
JOINED => NUMBER
STREET => TEXT
HOUSENO => TEXT
POSTCODE => TEXT
TOWN => TEXT
PHONENO => TEXT
LEAGUENO => TEXT


Server Error in '/asp' 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:


Line 139: myCommand.Parameters.Add(parameterPLAYERNO)
Line 140:
Line 141: myCommand.ExecuteNonQuery() 'Execute the UPDATE query
Line 142:
Line 143: objConn.Close()


Source File: C:\edit.aspx Line: 141


code:

Code:
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
    Sub Page_Load(sender as Object, e as EventArgs)
        If Not Page.IsPostBack
            BindData()
        End If
    End Sub


    Sub BindData()
        '1. Create a connection
        Const strConnString as String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                                "Data Source=C:\db.mdb"
        Dim objConn as New OleDbConnection(strConnString)
        objConn.Open()    'You must open the db connection before populating the DataReader


        '2. Create a command object for the query
        Const strSQL as String = "SELECT * FROM players"
        Dim objCmd as New OleDbCommand(strSQL, objConn)


        '3. Create/Populate the DataReader
        Dim objDR as OleDbDataReader
        objDR = objCmd.ExecuteReader()    


        dgProducts.DataSource = objDR
        dgProducts.DataBind()    
    End Sub


    Sub dgProducts_Edit(sender As Object, e As DataGridCommandEventArgs)
        dgProducts.EditItemIndex = e.Item.ItemIndex
        BindData()
    End Sub

    Sub dgProducts_Cancel(sender As Object, e As DataGridCommandEventArgs)
        dgProducts.EditItemIndex = -1
        BindData()
    End Sub

    Sub dgProducts_Update(sender As Object, e As DataGridCommandEventArgs)
        'Read in the values of the updated row
        Dim PLAYERNO as Integer = e.Item.Cells(1).Text
        Dim NAME as String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
        Dim INITIALS as String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
        Dim BIRTH_DATE as String = CType(e.Item.Cells(4).Controls(0), TextBox).Text

        Dim ************ as String = CType(e.Item.Cells(5).Controls(0), TextBox).Text
        Dim JOINED as String = CType(e.Item.Cells(6).Controls(0), TextBox).Text
        Dim STREET as String = CType(e.Item.Cells(7).Controls(0), TextBox).Text
        Dim HOUSENO as String = CType(e.Item.Cells(8).Controls(0), TextBox).Text
        Dim POSTCODE as String = CType(e.Item.Cells(9).Controls(0), TextBox).Text
        Dim TOWN as String = CType(e.Item.Cells(10).Controls(0), TextBox).Text
        Dim PHONENO as string = CType(e.Item.Cells(11).Controls(0), TextBox).Text
        Dim LEAGUENO as string = CType(e.Item.Cells(12).Controls(0), TextBox).Text


        'Construct the SQL statement using Parameters
        Dim strSQL as String = "UPDATE [PLAYERS] SET [Name] = @Name, " & _
                               "[INITIALS] = @INITIALS, [BIRTH_DATE] = @BIRTH_DATE " & _
                               "[************] = @************, [JOINED] = @JOINED " & _
                               "[STREET] = @STREET, [HOUSENO] = @HOUSENO " & _
                               "[POSTCODE] = @POSTCODE, [TOWN] = @TOWN " & _
                               "[PHONENO] = @PHONENO, [LEAGUENO] = @LEAGUENO " & _
                               "WHERE [PLAYERNO] = @PLAYERNO"

        ' Create Instance of Connection and Command Object
        '1. Create a connection
        Const strConnString as String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                                "Data Source=C:\db.mdb"
        Dim objConn as New OleDbConnection(strConnString)
        objConn.Open()

        Dim myCommand as OleDbCommand = new OleDbCommand(strSQL, objConn)
        myCommand.CommandType = CommandType.Text

        ' Add Parameters to the SQL query
        Dim parameterName as OleDbParameter = new OleDbParameter("@Name", OleDbType.VarWChar, 75)
        parameterName.Value = Name
        myCommand.Parameters.Add(parameterName)




               Dim parameterINITIALS as OleDbParameter = new OleDbParameter("@INITIALS", OleDbType.VarWChar, 75)
        parameterINITIALS.Value = INITIALS
        myCommand.Parameters.Add(parameterINITIALS)



               Dim parameterBIRTH_DATE as OleDbParameter = new OleDbParameter("@BIRTH_DATE", OleDbType.VarWChar, 75)
        parameterBIRTH_DATE.Value = BIRTH_DATE
        myCommand.Parameters.Add(parameterBIRTH_DATE)




                Dim parameterSEX as OleDbParameter = new OleDbParameter("@************", OleDbType.VarWChar, 75)
        parameterSEX.Value = ************
        myCommand.Parameters.Add(parameterSEX)


                Dim parameterJOINED as OleDbParameter = new OleDbParameter("@JOINED", OleDbType.VarWChar, 75)
        parameterJOINED.Value = JOINED
        myCommand.Parameters.Add(parameterJOINED)


                Dim parameterSTREET as OleDbParameter = new OleDbParameter("@STREET", OleDbType.VarWChar, 75)
        parameterSTREET.Value = STREET
        myCommand.Parameters.Add(parameterSTREET)

                Dim parameterHOUSENO as OleDbParameter = new OleDbParameter("@HOUSENO", OleDbType.VarWChar, 75)
        parameterHOUSENO.Value = HOUSENO
        myCommand.Parameters.Add(parameterHOUSENO)

                Dim parameterPOSTCODE as OleDbParameter = new OleDbParameter("@POSTCODE", OleDbType.VarWChar, 75)
        parameterPOSTCODE.Value = POSTCODE
        myCommand.Parameters.Add(parameterPOSTCODE)


                Dim parameterTOWN as OleDbParameter = new OleDbParameter("@TOWN", OleDbType.VarWChar, 75)
        parameterTOWN.Value = TOWN
        myCommand.Parameters.Add(parameterTOWN)

        Dim parameterPHONENO as OleDbParameter = new OleDbParameter("@PHONENO", OleDbType.Currency)
        parameterPHONENO.Value = PHONENO
        myCommand.Parameters.Add(parameterPHONENO)

        Dim parameterLEAGUENO as OleDbParameter = new OleDbParameter("@LEAGUENO", OleDbType.VarWChar)
        parameterLEAGUENO.Value = LEAGUENO
        myCommand.Parameters.Add(parameterLEAGUENO)

        Dim parameterPLAYERNO as OleDbParameter = new OleDbParameter("@PLAYERNO", OleDbType.Integer)
        parameterPLAYERNO.Value = PLAYERNO
        myCommand.Parameters.Add(parameterPLAYERNO)

        myCommand.ExecuteNonQuery()   'Execute the UPDATE query

        objConn.Close()


        'Finally, set the EditItemIndex to -1 and rebind the DataGrid
        dgProducts.EditItemIndex = -1
        BindData()        
    End Sub

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <asp:DataGrid id="dgProducts" runat="server"
            AutoGenerateColumns="False" CellPadding="4"
            HeaderStyle-BackColor="Black"
            HeaderStyle-ForeColor="White"
            HeaderStyle-HorizontalAlign="Center"
            HeaderStyle-Font-Bold="True"

            EditItemStyle-BackColor="#eeeeee"

            OnEditCommand="dgProducts_Edit"
            OnUpdateCommand="dgProducts_Update"
            OnCancelCommand="dgProducts_Cancel">

            <Columns>
                <asp:EditCommandColumn EditText="Edit Info" ButtonType="PushButton"
                        UpdateText="Update" CancelText="Cancel" />
                <asp:BoundColumn HeaderText="PLAYERNO" DataField="PLAYERNO"
                        ReadOnly="True" />                
                <asp:BoundColumn HeaderText="NAME" DataField="NAME"
                        ItemStyle-HorizontalAlign="Right"
                        DataFormatString="{0:$#,###.##}" />
                <asp:BoundColumn HeaderText="INITIALS" DataField="INITIALS" />
                <asp:BoundColumn HeaderText="BIRTH_DATE" DataField="BIRTH_DATE" />   
                <asp:BoundColumn HeaderText="************" DataField="************" />  
                <asp:BoundColumn HeaderText="JOINED" DataField="JOINED" />  
                <asp:BoundColumn HeaderText="STREET" DataField="STREET" />  
                <asp:BoundColumn HeaderText="HOUSENO" DataField="HOUSENO" />  
                <asp:BoundColumn HeaderText="POSTCODE" DataField="POSTCODE" />  
                <asp:BoundColumn HeaderText="TOWN" DataField="TOWN" />  
                <asp:BoundColumn HeaderText="PHONENO" DataField="PHONENO" />  
                <asp:BoundColumn HeaderText="LEAGUENO" DataField="LEAGUENO" />                       
            </Columns>        
        </asp:DataGrid>

    </form>
</body>
</html>
Reply With Quote
  #8 (permalink)  
Old May 10th, 2005, 03:21 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

method,

Look at the other thread you started addressing this problem:
http://p2p.wrox.com/topic.asp?TOPIC_ID=30235

-Peter
Reply With Quote
  #9 (permalink)  
Old April 29th, 2010, 05:56 AM
Authorized User
 
Join Date: Jan 2010
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to elizas
Default Formating the string before displaying in ASP.NET

In Some cases while displaying a large number it will be nice if we can format the number to a more readable format
Like : Reputation Point : 537456
Can be more readable if we can write it as Reputation Point : 537,456
ASP.NET provide features like String.Format(format,arg0) to format arguments into different forms.
For above solution you can implement

Response.Write(String.Format("{0:#,###}", 123456789));
Which will print 123,456,789

{0:#,###} → Known as the format string where “{ ,}”are compulsory to mentation.
The first part before ':' represent the argument number & it will be an integer.
The second part after ':' represent the format that you want your argument to be converted.
__________________
Cheers,
Eliza

Mindfire: India's Only Company to be both Apple Premier & Microsoft Gold certified.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Input string was not in a correct format. Silfverduk VB.NET 2002/2003 Basics 1 May 20th, 2006 04:58 AM
input string was not in a correct format kunal.net VS.NET 2002/2003 1 October 11th, 2005 12:18 AM
Input string was not in a correct format Dwizz VB.NET 2002/2003 Basics 2 April 4th, 2005 11:03 AM
"Input string was not in a correct format." keane Pro VB.NET 2002/2003 1 December 18th, 2004 08:45 PM



All times are GMT -4. The time now is 09:41 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.