PDA

Click to See Complete Forum and Search --> : Help - DataGrid - Specified cast is not valid. *Resolved*


Tenebrosity
Mar 31st, 2005, 12:04 AM
I have been pulling my hair out over this and have yet to find a resolution. The code was working until I changed the bound until I started tweaking the DataGrid.

The ASP page code is:


<asp:datagrid id="DataGrid1" runat="server" GridLines="Vertical" DataKeyField="City" AllowPaging="True"
PageSize="6" BackColor="White" CellPadding="3" width="80%" BorderStyle="None" BorderWidth="1px"
BorderColor="#999999" AutoGenerateColumns="False">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server" Text="Edit" CommandName="Edit" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton runat="server" Text="Update" CommandName="Update"></asp:LinkButton>&nbsp;
<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="false"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server" Text="Delete" CommandName="Delete" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="City">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.City") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.City") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="State/Country">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.State/Country") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.State/Country") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Contact Email">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Contact Email") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Contact Email") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Active?" ReadOnly="True" HeaderText="Active?"></asp:BoundColumn>
</Columns>
<PagerStyle Font-Size="Smaller" HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"
Mode="NumericPages"></PagerStyle>
</asp:datagrid>


The DataGrid_Update Sub line of code that blows up:



Was working before my changes to the Datagrids look and feel:
Dim City As String = Server.UrlEncode(CType(E.Item.Cells(2).Controls(0), TextBox).Text)

Tried this option as well:
Dim city As TextBox = CType(E.Item.Cells(2).Controls(0), TextBox)
Dim strcity As String = Server.UrlEncode(city.Text)


What am i doing wrong?? :cry:

Tenebrosity
Mar 31st, 2005, 08:36 AM
I have also tried this method.


Dim tbCity As TextBox
tbCity = E.Item.Cells(3).Controls(0)
Dim strCity As String = tbCity.Text.Trim


Any Ideas? I am so frustrated right now.

Tenebrosity
Mar 31st, 2005, 03:42 PM
*bump*

EricDalnas
Mar 31st, 2005, 06:02 PM
try giving IDs to the controls like
<EditItemTemplate>
<asp:TextBox ID="City" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.City") %>'></asp:TextBox>
</EditItemTemplate>


and then in the code

tbCity = directcast(e.Item.FindControl("City"),TextBox)

Lemme know if that works.

Tenebrosity
Mar 31st, 2005, 11:16 PM
Thats got it! Thanks!

I was ready to toss my laptop across the room. hehehe