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
| FAQ | Members List | 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 24th, 2006, 11:36 AM
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Input string was not in a correct format.

Completely stumped. Any help would be greatly appreciated.

I'm getting the following error:


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 288: txtEditFirstName = CType(e.Item.FindControl("txtEditFirstname"), TextBox)
Line 289: txtEditLastName = CType(e.Item.FindControl("txtEditLastname"), TextBox)
Line 290: intEditDeptID = CType(e.Item.Cells(5).FindControl("ddlEditDeptID") , DropDownList).SelectedValue
Line 291: chkEditUserAdmin = CType(e.Item.FindControl("chkEditAdmin"), CheckBox)
Line 292:


Source File: D:\WEBRoot\80.1\PMPrototype\admin\admin.aspx.vb Line: 290

Stack Trace:


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

[InvalidCastException: Cast from string "" to type 'Integer' is not valid.]
Microsoft.VisualBasic.CompilerServices.Integ erType.FromString(String Value) +212
PMPrototype.pgAdmin.dgUsers_UpdateCommand(Ob ject sender, DataGridCommandEventArgs e) in D:\WEBRoot\80.1\PMPrototype\admin\admin.aspx.vb:29 0
System.Web.UI.WebControls.DataGrid.OnUpdateC ommand(DataGridCommandEventArgs e) +109
System.Web.UI.WebControls.DataGrid.OnBubbleE vent(Object source, EventArgs e) +507
System.Web.UI.Control.RaiseBubbleEvent(Objec t source, EventArgs args) +26
System.Web.UI.WebControls.DataGridItem.OnBub bleEvent(Object source, EventArgs e) +101
System.Web.UI.Control.RaiseBubbleEvent(Objec t source, EventArgs args) +26
System.Web.UI.WebControls.LinkButton.OnComma nd(CommandEventArgs e) +121
System.Web.UI.WebControls.LinkButton.System. Web.UI.IPostBackEventHandler.RaisePostBackEvent(St ring eventArgument) +116
System.Web.UI.Page.RaisePostBackEvent(IPostB ackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameVa lueCollection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1292

I do a view source on the code, and see that the value of the ddlEditDeptID cannot be "" because all of the list items (<option>) are numbers.


Here's the Code:

Code:
#Region " User Datagrid "

    Private Sub dgUsers_ItemDatabound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgUsers.ItemDataBound

        Select Case e.Item.ItemType

            Case ListItemType.EditItem, ListItemType.EditItem

                Dim ddlEditDept As DropDownList
                Dim lblDGDeptID As Label
                lblDGDeptID = CType(e.Item.Cells(4).FindControl("lblEditDeptID"), Label)
                ddlEditDept = CType(e.Item.Cells(5).FindControl("ddlEditDeptID"), System.Web.UI.WebControls.DropDownList)
                ddlEditDept = clsRBForm.MakeDDLDept(ddlEditDept, Integer.Parse(lblDGDeptID.Text))

                e.Item.Cells(5).Controls.Clear()
                e.Item.Cells(5).Controls.Add(ddlEditDept)

        End Select

    End Sub

    Private Sub dgUsers_EditCommand(ByVal sender As System.Object, ByVal e As DataGridCommandEventArgs) Handles dgUsers.EditCommand

        dgUsers.EditItemIndex = e.Item.ItemIndex
        BindDatagrid(1)

    End Sub

    Private Sub dgUsers_CancelCommand(ByVal sender As System.Object, ByVal e As DataGridCommandEventArgs) Handles dgUsers.CancelCommand

        dgUsers.EditItemIndex = -1
        BindDatagrid(1)

    End Sub

    Private Sub dgUsers_UpdateCommand(ByVal sender As System.Object, ByVal e As DataGridCommandEventArgs) Handles dgUsers.UpdateCommand

        Dim txtEditUserName As TextBox
        Dim txtEditFirstName As TextBox
        Dim txtEditLastName As TextBox
        Dim ddlEditUserDept As DropDownList
        Dim intEditDeptID As Integer
        Dim chkEditUserAdmin As CheckBox
        Dim objUser As clsUser

        If IsValid() Then
            txtEditUserName = CType(e.Item.FindControl("txtEditUsername"), TextBox)
            txtEditFirstName = CType(e.Item.FindControl("txtEditFirstname"), TextBox)
            txtEditLastName = CType(e.Item.FindControl("txtEditLastname"), TextBox)
            intEditDeptID = CType(e.Item.Cells(5).FindControl("ddlEditDeptID"), DropDownList).SelectedValue
            chkEditUserAdmin = CType(e.Item.FindControl("chkEditAdmin"), CheckBox)

            objUser = New clsUser(CInt(dgUsers.DataKeys(e.Item.ItemIndex)))

            objUser.Username = txtEditUserName.Text
            objUser.Firstname = txtEditFirstName.Text
            objUser.Lastname = txtEditLastName.Text
            objUser.DeptID = intEditDeptID
            objUser.Admin = chkEditUserAdmin.Checked

            objUser.Save()

            dgUsers.EditItemIndex = -1
            BindDatagrid(1)

            SetMessage("User updated.")
        End If

    End Sub

    Private Sub btnAddUser_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles btnAddUser.Click

        Response.Redirect(clsRBForm.AdminUserPage)

    End Sub

#End Region


Here's the HTML:

Code:
<asp:DataGrid id="dgUsers" Runat="server" cssclass="datagrid" autogeneratecolumns="False" datakeyfield="UserID">
    <Columns>
        <asp:BoundColumn ItemStyle-CssClass="datagriditem" HeaderStyle-cssclass="datagridheader" ReadOnly="True"
            DataField="UserID" HeaderText="ID"></asp:BoundColumn>
        <asp:TemplateColumn ItemStyle-CssClass="datagriditem" HeaderStyle-cssclass="datagridheader" HeaderText="Username">
            <Itemtemplate>
                <%#Container.DataItem("Username")%>
            </Itemtemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtEditUsername" Runat=server text='<%#Container.DataItem("Username")%>'>
                </asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvEditUsername" ControlToValidate="txtEditUsername" InitialValue="" ErrorMessage="Username is required."></asp:RequiredFieldValidator>
            </EditItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn ItemStyle-CssClass="datagriditem" HeaderStyle-cssclass="datagridheader" HeaderText="Firstname">
            <Itemtemplate>
                <%#Container.DataItem("Firstname")%>
            </Itemtemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtEditFirstname" runat=server Text='<%#Container.DataItem("Firstname")%>'>
                </asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvEditFirstname" ControlToValidate="txtFirstUsername" InitialValue="" ErrorMessage="First name is required."></asp:RequiredFieldValidator>
            </EditItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn ItemStyle-CssClass="datagriditem" HeaderStyle-cssclass="datagridheader" HeaderText="Lastname">
            <Itemtemplate>
                <%#Container.DataItem("Lastname")%>
            </Itemtemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtEditLastname" Runat=server Text='<%#Container.DataItem("Lastname")%>'>
                </asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvEditLastname" ControlToValidate="txtEditLastname" InitialValue="" ErrorMessage="Last name is required."></asp:RequiredFieldValidator>
            </EditItemTemplate>
        </asp:TemplateColumn>
        <asp:templatecolumn Visible="False">
            <ItemTemplate>
                <asp:label runat=server ID="lblViewDeptID" text='<%#Container.DataItem("DeptID")%>'>
                </asp:label>
            </ItemTemplate>
            <edititemtemplate>
                <asp:label runat=server ID="lblEditDeptID" text='<%#Container.DataItem("DeptID")%>'>
                </asp:label>
            </edititemtemplate>
        </asp:templatecolumn>
        <asp:TemplateColumn ItemStyle-CssClass="datagriditem" HeaderStyle-cssclass="datagridheader" HeaderText="Department">
            <ItemTemplate>
                <%# Container.DataItem("DeptName")%>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList runat="server" id="ddlEditDeptID"></asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="Admin" ItemStyle-CssClass="datagriditem" HeaderStyle-cssclass="datagridheader"
            runat="server">
            <ItemTemplate>
                <asp:Checkbox enabled=False runat=server id="chkUserAdminView" Checked='<%#Container.DataItem("Admin")%>'/>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Checkbox runat=server id="chkUserAdminEdit" Checked='<%#Container.DataItem("Admin")%>' />
            </EditItemTemplate>
        </asp:TemplateColumn>
        <asp:EditCommandColumn runat="server" ItemStyle-CssClass="datagriditem" HeaderStyle-cssclass="datagridheader"
            headertext="Options" CancelText="Cancel" EditText="Edit" UpdateText="Update"></asp:EditCommandColumn>
    </Columns>
</asp:DataGrid>
Reply With Quote
  #2 (permalink)  
Old May 20th, 2006, 04:58 AM
Registered User
 
Join Date: May 2006
Location: Mumbai, Maharashtra, India.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Check the Data Type you have used in the Table for for DeptId. If it is other than you have used in the code the exception is thrown.

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 Dwizz VB.NET 2002/2003 Basics 8 April 29th, 2010 05:56 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 06:51 AM.


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