• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

Problems with code while performing updatecommand from datagrid with strongly typed dataset


I have the following datagrid

VideoDetailID   Edit Column   VideoTitle   VideoDuration   VideoURL   GlobalAccessID
       1                 Edit            MyTitle          2.46              C:/......           25
       2                 Edit            MyTitle2        3.56              C:/......           32

I have used a strongly typed dataset and am trying to implement the update command.

The following is my code for the updatecommand

Private Sub dgTypedDataGrid_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgTypedDataGrid.UpdateCommand
        BindToGrid()

        'Dim the values you are working with
        Dim VideoTitle As String
        Dim VideoDuration As Decimal
        Dim VideoUrl As String
        Dim GlobalAccessID As String

        Dim dt As DataTable
        Dim dr As DataRow

        Dim txt As TextBox

        'Retrieve the changed data
        txt = CType(e.Item.Controls(2).Controls(0), TextBox)
        VideoTitle = CType(txt.Text, String)

        txt = CType(e.Item.Controls(3).Controls(0), TextBox)
        VideoDuration = CType(txt.Text, Decimal)

        txt = CType(e.Item.Controls(4).Controls(0), TextBox)
        VideoUrl = CType(txt.Text, String)

        txt = CType(e.Item.Controls(5).Controls(0), TextBox)
        GlobalAccessID = CType(txt.Text, Integer)

        'Retrieve stored data
        Dim VideoCache As DataSet = CType(Cache.Get("VideoCache"), DataSet)
        dt = VideoCache.Tables("tblVideoDetail")
        dr = dt.Rows.Find(CStr(e.Item.Cells(2).Text))

        If Not (dr Is Nothing) Then
            With dr
                .Item("VideoTitle") = VideoTitle
                .Item("VideoDuration") = VideoTitle
                .Item("VideoUrl") = VideoTitle
                .Item("GlobalAccessID") = VideoTitle
            End With
        End If

        daVideoDetails.Update(dt)

        'Restore the grid to read only
        dgTypedDataGrid.EditItemIndex = -1
        BindToGrid()

    End Sub


The above code keeps stopping at  dr = dt.Rows.Find(CStr(e.Item.Cells(2).Text)) and saying 'Input string not in correct format'

To be honest I dont know what  "dr = dt.Rows.Find(CStr(e.Item.Cells(2).Text))"  is supposed to be doing since all my columns in the datagrid are editable and cells(2) seems to me to be hard coded.

I hope all this makes sense.

Cheers

Paul G








0
pgilfeather
Asked:
pgilfeather
3 Solutions
 
raterusCommented:
First, let me just mention something...

VideoTitle = CType(txt.Text, String)

txt.Text is already a String!  Why are you recasting it as a string?  This is doing absolutely nothing but wasting time.

Better...

VideoTitle = txt.Text

k, sorry I'm a code critic!

--

It looks like you are trying to find the original row in the datatable that corresponds to the updated row in the DataGrid.  I'd suggest using the DataKeys/DataKeyField properties of the DataGrid to do this.  The Rows.Find method is only going to search the primary key value in the DataTable, and from the looks of your code, you aren't searching by that.

Something like this hopefully will work better,

 dr = dt.Rows.Find(myDataGrid.DataKeys(e.Item.ItemIndex))

You'll also need to set the DataKeyField of the DataGrid = your primary key field in your datatable.
0
 
Bob LearnedCommented:
Also, if you have a strongly-typed dataset, you have not taken advantage of its power by using generic types:

Dim dt As DataTable
Dim dr As DataRow

Example:

Dim dt As SpecificDataSet.SpecificTableType
Dim dr As SpecificDataSet.SpecificRowType

Bob
0
 
b1xml2Commented:
The RowCollection Find Method (Rows.Find) relies on the the definition of the primary key and the proper type used.
So if your strongly typed dataset did not have the Video Title set as the Primary Key, the Find method will not work.

I'd say your primary key is the Column1 (int type) instead, so if you have set the DataKeyField attribute like so:

<asp:DataGrid Id="DataGrid1" runat="server" DataKeyField="Column1">

</asp:DataGrid>

then you can obtain a reference to to the row accordingly:

'whatever the strongly typed dataset is called
Dim key as Integer = DirectCast(DataGrid1.DataKeys(e.Item.ItemIndex),Integer)
Dim videos As VideoDataSet = CType(Cache.Get("VideoCache"), VideoDataSet )
Dim video As VideoDataSet.VideoRow = videos.FindByColumn1(key)


in untyped datasets.
Dim key as Integer = DirectCast(DataGrid1.DataKeys(e.Item.ItemIndex),Integer)
Dim videos As DataSet =  CType(Cache.Get("VideoCache"), DataSet)
Dim video As DataRow = videos.Rows.Find(key)



0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now