Avatar of sny23vpb
sny23vpb

asked on 

Gridview Cell Values durign RowUpdating Event

I have created a gridview that is dynamic and binds the grid dynamically based on a table selected in a dropdown. I have worked through most of the issues like dynamically creating the update, delete statements etc except for one problem I'm having in retrieving the cell value while in the rowupdating event.  I can retrieve it every where else; but not in this event.  I am not setting up an item template or assigning names so as to keep the process very dynamic where it can be run on one of 25 tables selected all with different fields where I only define the fields in the select, update, and delete statements with generate fields=true.

The code follows but  GridView1.Rows(rowIndex).Cells(i).Text  is always empty if your on the row being updated.  And this  e.NewValues(0).ToString()  is always an error so I am unable to get the value to plug into the dynamic update statement.

If you on a different row; no problem it will return the value.
Thanks for any help.

 <asp:GridView ID="GridView1" runat="server"  AutoGenerateDeleteButton="True"  AutoGenerateEditButton="True" DataKeyNames="id" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical">
            <FooterStyle BackColor="#CCCC99" />
            <RowStyle BackColor="#F7F7DE" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
         
        </asp:GridView>


Public Sub BindGridView()
        Try
            Dim connection As Data.SqlClient.SqlConnection
            Dim sql As String
            Dim begin As Integer = 0

            connection = New Data.SqlClient.SqlConnection("server=USG1\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=Test")
            connection.Open()

            Dim command2 As SqlCommand = New SqlCommand("Select * from Keys where TableName='" & "Country_Table" & "'")
            Dim reader As Data.SqlClient.SqlDataReader
            Dim da As New Data.SqlClient.SqlDataAdapter()
            Dim dt As New DataTable()
            Dim cb As Data.SqlClient.SqlCommandBuilder
            command2.Connection = connection
            da.SelectCommand = command2
            da.Fill(dt)
            cb = New Data.SqlClient.SqlCommandBuilder(da)
            reader = da.SelectCommand.ExecuteReader()
            sql = "Select Top 10"
            While reader.Read()
                If begin = 1 Then
                    sql = sql & "+"
                End If

                If Trim(reader(2).ToString()) = "int" Then
                    sql = sql & " " & Trim(reader(1).ToString()) & " "

                Else
                    sql = sql & " [" & Trim(reader(1).ToString()) & "] "

                End If
                Try
                Catch exc As Exception
                End Try
                begin = 1
            End While
            sql = sql & " as ID, *  from " & "Country_Table"

            reader.Close()

            Dim command As SqlCommand = New SqlCommand(sql, connection)
            command.Connection = connection


            Dim dataAdapter As SqlDataAdapter = New SqlDataAdapter(command)
            Dim dataSource As DataSet = New DataSet()
            dataAdapter.Fill(dataSource)
            GridView1.DataSource = dataSource.Tables(0)
            GridView1.DataBind()


        Catch ex As Exception
            Throw ex
        Finally

            If GridView1.Rows.Count = 0 Then
                MsgBox("No records to show!")
            End If
        End Try


RowUpdating
        Dim rowIndex As Integer = e.RowIndex
        Dim row As GridViewRow = GridView1.Rows(rowIndex)
            sql = sql & 'FieldA'
            sql = sql & "=" & GridView1.Rows(rowIndex).Cells(i).Text

.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
prairiedog
Avatar of sny23vpb
sny23vpb

ASKER

If there is no other way; I suppose I could define the fields dynamically and use the findcontrol to obtain the value during update mode; but I've not had any success dynamically defining the field names or labels.  Thanks
Avatar of prairiedog
prairiedog
Flag of United States of America image

1. Can you post your page_load event handler?
2. Can you post the complete RowUpdating event handler?
Avatar of sny23vpb
sny23vpb

ASKER

I actually haven't put it in a page load event since ultimately the work flow will require the selection of a table ; currently I have the grid bind in a button like below.

Thanks for reviewing; let me know if I can supply any other info.
------------------------------

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

               BindGridView()
 End Sub
--------------------------
The BindGridView process is listed out above;  It may appear a little more convoluted than normal simply because it must dynamically build the select statement based on any possible table selected.
--------------------------------------



    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim rowIndex As Integer = e.RowIndex
        Dim row As GridViewRow = GridView1.Rows(rowIndex)
        Dim ID As String = GridView1.Rows(rowIndex).Cells(1).Text
        Dim i As Integer = 2

        Dim totalfields As Integer
        totalfields = GridView1.Rows(rowIndex).Cells.Count - 2
        'start process
         Dim connection As Data.SqlClient.SqlConnection
        connection = New Data.SqlClient.SqlConnection("server=USG1\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=Test")
     
        connection.Open()
        Dim reader As Data.SqlClient.SqlDataReader
     
        Dim recordData As String = ""
        Dim sql As String = ""
        Dim x As Integer = 0
        Dim dk As String = ""
        Dim recordCount As Integer = 0

        Dim cmd As New Data.SqlClient.SqlCommand()
        Dim da As New Data.SqlClient.SqlDataAdapter()
        Dim dt As New DataTable()
        Dim cb As Data.SqlClient.SqlCommandBuilder

        cmd.Connection = connection
        cmd.CommandText = "SELECT Column_Name, Data_Type FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name='Country_Table' "
 

        da.SelectCommand = cmd

        da.Fill(dt)

        cb = New Data.SqlClient.SqlCommandBuilder(da)

        reader = da.SelectCommand.ExecuteReader()


        sql = "Update Country_Table Set "
        While reader.Read()

            recordData = reader(0).ToString()
            If x <> 0 Then
                sql = sql & ","
            End If
            sql = sql & recordData
            sql = sql & "=" & GridView1.Rows(rowIndex).Cells(i).Text



            x = 1
            i = i + 1
            Try
                '  Dim newvalue As String = e.NewValues(0).ToString()
                '  Dim oldvalue As String = e.OldValues(0).ToString()



            Catch exc As Exception

            End Try


        End While
        sql = sql & " where " & dk
        MsgBox(sql)


    End Sub
ASKER CERTIFIED SOLUTION
Avatar of prairiedog
prairiedog
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of sny23vpb
sny23vpb

ASKER

The EE.jpg file shows a picture of the watch window as the process is passing both the GridView1_RowUpdating event and for the heck of it GridView1_RowEditing events after the update button is clicked ; the first watch is the row being edited ; the second watch is a row not being edited ; the values should be identical since I didn't make any change on that field and the field is the same for all recs in the recordset yet the rec being updated is empty string at that point in time.

Is there another event I could define that would fire when the auto-generated  update button is clicked. I've tried rowupdating and rowediting.

Ideally I need to capture the before and after change field values so I can log it but for now if I could just get the value to write the update statement; that would be a great start.

Thanks for the help.

EE.JPG
Avatar of prairiedog
prairiedog
Flag of United States of America image

Why rowindex=0? Are you updating the first datarow?
Avatar of sny23vpb
sny23vpb

ASKER

Yes; at that particular time; I was working on the first row with rowindex=0; but I've tried all the data rows and I've tried fields where I've made a change prior to pressing update and fields where I have made no change.  Let me know if you have any other ideas; if this doesn't work; I'm not sure what option I have besides drawing out a table and using update links the old way with posts.
Avatar of prairiedog
prairiedog
Flag of United States of America image

I am out of ideas. Sorry.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo