Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2005-05-17
Medium Priority
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

raterus earned 680 total points
ID: 14018014
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
Bob Learned earned 680 total points
ID: 14018095
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

b1xml2 earned 640 total points
ID: 14018469
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

578 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