View Single Post
  #1 (permalink)  
Old April 24th, 2006, 11:36 AM
Silfverduk Silfverduk is offline
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