EditItemTemplate Error

Discussion in 'ASP .Net Datagrid Control' started by Aaron Prohaska, Sep 12, 2003.

  1. Can anyone tell me why the following code is throwing this error:

    System.InvalidCastException: Specified cast is not valid

    Line 43: string itemID = e.Item.Cells[2].Text;
    Line 44: string manufacturer =
    ((TextBox)e.Item.Cells[3].Controls[0]).Text;
    Line 45: string model = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
    Line 46: string shortBlurb =
    ((TextBox)e.Item.Cells[5].Controls[0]).Text;

    aspx code

    <asp:DataGrid
    AutoGenerateColumns="False"
    BorderStyle="Solid"
    CellPadding="2"
    CellSpacing="0"
    Width="100%"
    DataKeyField="SpecialID"
    OnEditCommand="EditItemClick"
    OnUpdateCommand="UpdateItemClick"
    OnDeleteCommand="DeleteItemClick"
    OnCancelCommand="CancelItemClick"
    ID="viewSpecials"
    Runat="server">
    <HeaderStyle BackColor="#ffffff" Font-Bold="True"/>
    <ItemStyle BackColor="#ffffff"/>
    <AlternatingItemStyle BackColor="WhiteSmoke"/>
    <Columns>
    <asp:EditCommandColumn EditText="Edit" UpdateText="Save"
    CancelText="Cancel"/>
    <asp:ButtonColumn Text="Delete" CommandName="Delete"/>
    <asp:BoundColumn DataField="SpecialID" ReadOnly="True"
    Visible="False"/>
    <asp:TemplateColumn HeaderText="Manufacturer">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "Manufacturer") %>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="enterManufacturer" Text='<%#
    DataBinder.Eval(Container.DataItem, "Manufacturer") %>' Width="100"
    Runat="server"/>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Model">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "Model") %>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="Textbox1" Text='<%#
    DataBinder.Eval(Container.DataItem, "Model") %>' Width="100"
    Runat="server"/>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Blurb">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "ShortBlurb") %>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="Textbox8" Text='<%#
    DataBinder.Eval(Container.DataItem, "ShortBlurb") %>' Width="200"
    Runat="server"/>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Color">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "Color") %>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="Textbox2" Text='<%#
    DataBinder.Eval(Container.DataItem, "Color") %>' Width="100"
    Runat="server"/>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Image File">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "Image") %>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="Textbox3" Text='<%#
    DataBinder.Eval(Container.DataItem, "Image") %>' Width="100"
    Runat="server"/>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Part Type">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "PartType") %>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="Textbox4" Text='<%#
    DataBinder.Eval(Container.DataItem, "PartType") %>' Width="60"
    Runat="server"/>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Grams">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "Grams") %>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="Textbox5" Text='<%#
    DataBinder.Eval(Container.DataItem, "Grams") %>' Width="50"
    Runat="server"/>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="MSRP">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "MSRP") %>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="Textbox6" Text='<%#
    DataBinder.Eval(Container.DataItem, "MSRP") %>' Width="80"
    Runat="server"/>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Discount">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "Discount") %>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="Textbox7" Text='<%#
    DataBinder.Eval(Container.DataItem, "Discount") %>' Width="50"
    Runat="server"/>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="SalePrice" HeaderText="Sale Price"
    ReadOnly="True"/>
    <asp:BoundColumn DataField="Savings" HeaderText="Savings"
    ReadOnly="True"/>
    </Columns>
    </asp:DataGrid>

    Codebeind C#:

    string itemID = e.Item.Cells[2].Text;
    string manufacturer = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
    string model = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
    string shortBlurb = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
    string color = ((TextBox)e.Item.Cells[6].Controls[0]).Text;
    string image = ((TextBox)e.Item.Cells[7].Controls[0]).Text;
    string partType = ((TextBox)e.Item.Cells[8].Controls[0]).Text;
    string grams = ((TextBox)e.Item.Cells[9].Controls[0]).Text;
    string msrp = ((TextBox)e.Item.Cells[10].Controls[0]).Text;
    string discount = ((TextBox)e.Item.Cells[11].Controls[0]).Text;

    regards,

    Aaron
    Aaron Prohaska, Sep 12, 2003
    #1
    1. Advertising

  2. I seem to have found my answer. I'm now using the following to fix it.

    string manufacturer = ((TextBox)e.Item.FindControl("controlName")).Text;

    thanks anyways,

    Aaron

    Aaron Prohaska wrote:
    >
    > Can anyone tell me why the following code is throwing this error:
    >
    > System.InvalidCastException: Specified cast is not valid
    >
    > Line 43: string itemID = e.Item.Cells[2].Text;
    > Line 44: string manufacturer =
    > ((TextBox)e.Item.Cells[3].Controls[0]).Text;
    > Line 45: string model = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
    > Line 46: string shortBlurb =
    > ((TextBox)e.Item.Cells[5].Controls[0]).Text;
    >
    > aspx code
    >
    > <asp:DataGrid
    > AutoGenerateColumns="False"
    > BorderStyle="Solid"
    > CellPadding="2"
    > CellSpacing="0"
    > Width="100%"
    > DataKeyField="SpecialID"
    > OnEditCommand="EditItemClick"
    > OnUpdateCommand="UpdateItemClick"
    > OnDeleteCommand="DeleteItemClick"
    > OnCancelCommand="CancelItemClick"
    > ID="viewSpecials"
    > Runat="server">
    > <HeaderStyle BackColor="#ffffff" Font-Bold="True"/>
    > <ItemStyle BackColor="#ffffff"/>
    > <AlternatingItemStyle BackColor="WhiteSmoke"/>
    > <Columns>
    > <asp:EditCommandColumn EditText="Edit" UpdateText="Save"
    > CancelText="Cancel"/>
    > <asp:ButtonColumn Text="Delete" CommandName="Delete"/>
    > <asp:BoundColumn DataField="SpecialID" ReadOnly="True"
    > Visible="False"/>
    > <asp:TemplateColumn HeaderText="Manufacturer">
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "Manufacturer") %>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:TextBox ID="enterManufacturer" Text='<%#
    > DataBinder.Eval(Container.DataItem, "Manufacturer") %>' Width="100"
    > Runat="server"/>
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn HeaderText="Model">
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "Model") %>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:TextBox ID="Textbox1" Text='<%#
    > DataBinder.Eval(Container.DataItem, "Model") %>' Width="100"
    > Runat="server"/>
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn HeaderText="Blurb">
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "ShortBlurb") %>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:TextBox ID="Textbox8" Text='<%#
    > DataBinder.Eval(Container.DataItem, "ShortBlurb") %>' Width="200"
    > Runat="server"/>
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn HeaderText="Color">
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "Color") %>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:TextBox ID="Textbox2" Text='<%#
    > DataBinder.Eval(Container.DataItem, "Color") %>' Width="100"
    > Runat="server"/>
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn HeaderText="Image File">
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "Image") %>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:TextBox ID="Textbox3" Text='<%#
    > DataBinder.Eval(Container.DataItem, "Image") %>' Width="100"
    > Runat="server"/>
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn HeaderText="Part Type">
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "PartType") %>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:TextBox ID="Textbox4" Text='<%#
    > DataBinder.Eval(Container.DataItem, "PartType") %>' Width="60"
    > Runat="server"/>
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn HeaderText="Grams">
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "Grams") %>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:TextBox ID="Textbox5" Text='<%#
    > DataBinder.Eval(Container.DataItem, "Grams") %>' Width="50"
    > Runat="server"/>
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn HeaderText="MSRP">
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "MSRP") %>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:TextBox ID="Textbox6" Text='<%#
    > DataBinder.Eval(Container.DataItem, "MSRP") %>' Width="80"
    > Runat="server"/>
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn HeaderText="Discount">
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "Discount") %>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:TextBox ID="Textbox7" Text='<%#
    > DataBinder.Eval(Container.DataItem, "Discount") %>' Width="50"
    > Runat="server"/>
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    > <asp:BoundColumn DataField="SalePrice" HeaderText="Sale Price"
    > ReadOnly="True"/>
    > <asp:BoundColumn DataField="Savings" HeaderText="Savings"
    > ReadOnly="True"/>
    > </Columns>
    > </asp:DataGrid>
    >
    > Codebeind C#:
    >
    > string itemID = e.Item.Cells[2].Text;
    > string manufacturer = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
    > string model = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
    > string shortBlurb = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
    > string color = ((TextBox)e.Item.Cells[6].Controls[0]).Text;
    > string image = ((TextBox)e.Item.Cells[7].Controls[0]).Text;
    > string partType = ((TextBox)e.Item.Cells[8].Controls[0]).Text;
    > string grams = ((TextBox)e.Item.Cells[9].Controls[0]).Text;
    > string msrp = ((TextBox)e.Item.Cells[10].Controls[0]).Text;
    > string discount = ((TextBox)e.Item.Cells[11].Controls[0]).Text;
    >
    > regards,
    >
    > Aaron
    Aaron Prohaska, Sep 12, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Trevor Hartman

    datagrid edititemtemplate for dates

    Trevor Hartman, Jul 16, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    362
    Trevor Hartman
    Jul 16, 2003
  2. SStory

    Datalist EditItemTemplate

    SStory, Aug 19, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    486
    SStory
    Aug 19, 2003
  3. Dave
    Replies:
    1
    Views:
    520
  4. Kevin Quigley

    asp:hyperlink in EditItemTemplate

    Kevin Quigley, Dec 2, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    353
    Kevin Quigley
    Dec 2, 2003
  5. rsw8n

    DropDownList in DataGrid EditItemTemplate Return Error

    rsw8n, Nov 26, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    196
    rsw8n
    Nov 28, 2003
Loading...

Share This Page