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

Posted on 2005-05-17
Last Modified: 2010-04-07

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

        '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


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

    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.


Paul G

Question by:pgilfeather
    LVL 33

    Accepted Solution

    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.


    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.
    LVL 96

    Assisted Solution

    by:Bob Learned
    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


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

    LVL 23

    Assisted Solution

    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">


    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)


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video discusses moving either the default database or any database to a new volume.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now