Monday, March 26, 2012

Newbie datagrid woes

Data grid

Hi Guys, I am fairly new to .net but have found a lot of the stuff I am attempting is working out as expected. I have however hit a snag with an updateable datagrid. I have spent the last two days searching forums and google for an answer but nothing I do seems to work.
My db is SQL 2000 with 3 columns movieID (Int, Pri) movieTitle(Varchar) movieCategory(int,4 used as a relation to categories table)

Any advice offered will be gratefully received as I am totally at my wits end

The error occurs on datagrid update and I get the following error

Specified cast is not valid.
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.InvalidCastException: Specified cast is not valid.

Source Error:

Line 104: Dim CategoryID As String = CType(e.Item.FindControl("movieCategory"), DropDownList).SelectedItem.Value
Line 105:
Line 106: Dim MovieTitle As String = CType(e.Item.Cells(1).Controls(0), TextBox).Text
Line 108:

I am using vs.net 2003 my code is below

Update function


Public Sub OnUpdate(ByVal source As Object, ByVal e As DataGridCommandEventArgs)
Dim CategoryID As String = CType(e.Item.FindControl("movieCategory"), DropDownList).SelectedItem.Value

Dim MovieTitle As String = CType(e.Item.Cells(1).Controls(0), TextBox).Text

' form the update statement
Dim cmd As String = "Update tblMovies set MovieTitle = " & MovieTitle & "MovieCategory = " & CategoryID & "where movieID = " & DataGrid1.DataKeys(e.Item.ItemIndex)

' connect to database
Dim connectionString As String = Global.dbConnect

Dim connection As New SqlConnection(connectionString)
connection.Open()

' call the update and rebind the datagrid
Dim command As New SqlCommand
command.CommandText = cmd
command.Connection = connection
command.ExecuteNonQuery()

DataGrid1.EditItemIndex = -1
BindGrid()
End Sub


Datagrid tag

<asp:DataGrid
id="DataGrid1"
runat="server"
AutoGenerateColumns="False"
CellPadding="5"
HeaderStyle-BackColor="PapayaWhip"
BorderWidth="5px"
BorderColor="#000099"
AlternatingItemStyle-BackColor="LightGrey"
HeaderStyle-Font-Bold="True"
EditItemStyle-BackColor="Yellow"
EditItemStyle-ForeColor="Black"
DataKeyField ="movieID"
OnEditCommand="OnEdit"
OnCancelCommand="OnCancel"
OnUpdateCommand="OnUpdate">
<Columns
<asp:BoundColumn DataField="MovieID" HeaderText="ID" ReadOnly="True" />
<asp:TemplateColumn HeaderText="Movie">
<ItemTemplate>
<asp:Label
Text='<%# Convert.ToString(DataBinder.Eval(Container.DataItem,"MovieTitle")) %>'
Runat="server" ID="lblProduct"/>
</ItemTemplate
<EditItemTemplate>
<asp:TextBox ID="MovieTitle" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"MovieTitle") %>'/>
</EditItemTemplate
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Category">
<ItemTemplate>
<asp:Label
Text='<%# Convert.ToString(DataBinder.Eval(Container.DataItem,"MovieCategory")) %>'
Runat="server" ID="Label1"/>
</ItemTemplate
<EditItemTemplate>
<asp:DropDownList
Runat="server"
ID="movieCategory"
DataSource='<%# GetValues("Category") %>'
DataTextField ="MovieCategory"
DataValueField ="id"
Width ="200" />
</EditItemTemplate
</asp:TemplateColumn
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Save" /
</Columns>
</asp:DataGrid>
You cannot directly cast a objects like that into a string object - try something like this:

Dim CategoryID As String
Dim MovieCategoryDDL as dropdownlist
MovieCategoryDDL = e.item.findcontrol("moviecategory")
CategoryID = MovieCategoryDDL.selecteditem.value

Dim MovieTitle As String
Dim myTextBox as textbox
myTextBox = e.item.findcontrol("MovieTitle")
MovieTitle = myTextBox.text

0 comments:

Post a Comment