Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8642383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:45:44+00:00 2026-06-12T11:45:44+00:00

I developed the following code for editing a GridView (following a tutorial written in

  • 0

I developed the following code for editing a GridView (following a tutorial written in C#), It goes into edit mode, but my edits do not take effect, here is my code:

aspx.vb code:

Imports System.Data
Imports System.Data.SqlClient
Imports System.Globalization

Partial Class MemberPages_editOutage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not IsPostBack Then
            BindGrid()
        End If
    End Sub

    Private Sub BindGrid()
        Dim dt As New DataTable()
        Dim connection As New SqlConnection("server='\SQLEXPRESS'; trusted_connection='true'; Database='OutagesMgt_db'")
        Try
            connection.Open()
            Dim sqlStatement As String = "SELECT OutageDetailId, LocationName, Description, DetailDescription, CreateDate, StatusId FROM OutageDetail WHERE StatusId='1' ORDER BY CreateDate DESC"
            Dim cmd As New SqlCommand(sqlStatement, connection)
            Dim sqlDa As New SqlDataAdapter(cmd)

            sqlDa.Fill(dt)
            If dt.Rows.Count > 0 Then
                MyDataGrid.DataSource = dt
                MyDataGrid.DataBind()
            End If
        Catch ex As System.Data.SqlClient.SqlException
            Dim msg As String = "Fetch Error:"
            msg += ex.Message
            Throw New Exception(msg)
        Finally
            connection.Close()
        End Try
    End Sub
    'edit command
    Protected Sub MyDataGrid_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles MyDataGrid.RowEditing
        'turn to edit mode
        MyDataGrid.EditIndex = e.NewEditIndex
        'Rebind the GridView to show the data in edit mode
        BindGrid()
    End Sub
    'cancel command
    Protected Sub MyDataGrid_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles MyDataGrid.RowCancelingEdit
        ' switch back to edit default mode
        MyDataGrid.EditIndex = -1
        'Rebind the GridView to show the data in edit mode
        BindGrid()
    End Sub
    'Update Function
    Private Sub UpdateRecord(ByVal SOutageDetailId As String, ByVal SDescription As String, ByVal SDetailDescription As String, ByVal SCreateDate As String, ByVal SstatusId As String)
        Dim connection As New SqlConnection("server='\SQLEXPRESS'; trusted_connection='true'; Database='OutagesMgt_db'")
        Dim sqlStatement As String = String.Empty
        sqlStatement = "UPDATE OutageDetail SET @OutageDetailId = @OutageDetailId, LocationName = @LocationName, " & _
                        "Description = @Description, DetailDescription= @DetailDescription, " & _
                        "CreateDate = @CreateDate, StatusId = @StatusId WHERE OutageDetailId = @OutageDetailId"
        connection.Open()

        Dim cmd As New SqlCommand(sqlStatement, connection)
        cmd.Parameters.Add(New SqlParameter("@OutageDetailId", SOutageDetailId))
        cmd.Parameters.Add(New SqlParameter("@LocationName", SDescription))
        cmd.Parameters.Add(New SqlParameter("@Description", SDescription))
        cmd.Parameters.Add(New SqlParameter("@DetailDescription", SDetailDescription))
        cmd.Parameters.Add(New SqlParameter("@CreateDate", SCreateDate))
        cmd.Parameters.Add(New SqlParameter("@StatusId", SstatusId))
        cmd.CommandType = CommandType.Text
        cmd.ExecuteNonQuery()


        ' MyDataGrid.EditIndex = -1
        connection.Close()

        BindGrid()
    End Sub
    'update command
    Protected Sub MyDataGrid_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles MyDataGrid.RowUpdating
        'Accessing Edited values from the GridView
        Dim SOutageDetailId As String = MyDataGrid.Rows(e.RowIndex).Cells(0).Text
        Dim SDescription As String = MyDataGrid.Rows(e.RowIndex).Cells(1).Text
        Dim SDetailDescription As String = MyDataGrid.Rows(e.RowIndex).Cells(2).Text
        Dim SCreateDate As String = MyDataGrid.Rows(e.RowIndex).Cells(3).Text
        Dim SstatusId As String = MyDataGrid.Rows(e.RowIndex).Cells(4).Text

        'Call the function to update the GridView
        UpdateRecord(SOutageDetailId, SDescription, SDetailDescription, SCreateDate, SstatusId)

        MyDataGrid.EditIndex = -1

        'Rebind Gridview to reflect changes made
        BindGrid()
    End Sub
End Class

aspx code:

<asp:GridView id="MyDataGrid" runat="server"
                    Width="750px"
                    CssClass="gridViewEdit"
                    BackColor="White"
                    BorderColor="Black"
                    CellPadding="3"
                    Font-Name="Verdana"
                    Font-Size="8pt"
                    HeaderStyle-BackColor="#FFFFFF"
                    OnEditCommand="MyDataGrid_RowEditing"
                    OnCancelCommand="MyDataGrid_RowCancelingEdit"
                    OnUpdateCommand="MyDataGrid_RowUpdating"
                    DataKeyField="OutageDetailId" 
                    Font-Names="Verdana">
                  <Columns>
                     <asp:CommandField ShowEditButton="True" EditText="Edit" CancelText="Cancel" UpdateText="Update" />
                 </Columns>
                <HeaderStyle BackColor="White"></HeaderStyle>
            </asp:GridView>

Could someone shed some light on what I am missing please.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:45:45+00:00Added an answer on June 12, 2026 at 11:45 am

    The moment you hit the Edit, you go to get the ID of the line that must be update, and you get it from this line

    Dim SOutageDetailId As String = MyDataGrid.Rows(e.RowIndex).Cells(0).Text
    

    but on page load you have set

        If Not IsPostBack Then
            BindGrid()
        End If
    

    so on the post back, the grid up to the point you try to get the id from the cell, is empty.

    Two ways, ether give again the data on post back, and make DataBind right after the update, or get the Index of the Grid View to make the Update, and not take the Cell.

    For example, I will change your code to:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)        
            BindGrid()
    End Sub
    
    Private Sub BindGrid()
        Dim dt As New DataTable()
        Dim connection As New SqlConnection("server='\SQLEXPRESS'; trusted_connection='true'; Database='OutagesMgt_db'")
        Try
            connection.Open()
            Dim sqlStatement As String = "SELECT OutageDetailId, LocationName, Description, DetailDescription, CreateDate, StatusId FROM OutageDetail WHERE StatusId='1' ORDER BY CreateDate DESC"
            Dim cmd As New SqlCommand(sqlStatement, connection)
            Dim sqlDa As New SqlDataAdapter(cmd)
    
            sqlDa.Fill(dt)
            If dt.Rows.Count > 0 Then
                MyDataGrid.DataSource = dt
               If Not IsPostBack Then
                   MyDataGrid.DataBind()
               End If                
            End If
        Catch ex As System.Data.SqlClient.SqlException
            Dim msg As String = "Fetch Error:"
            msg += ex.Message
            Throw New Exception(msg)
        Finally
            connection.Close()
        End Try
    End Sub
    

    [*] Assuming that you do not have other bugs on sql…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed the following C code to mask data before sending back to
I developed an RSS feed following a tutorial and I think the .xml file
I developed a C++ program which utilises an .exe file through the following code:
I'm a total javascript noobie. I developed the code bellow following and modifing some
I am using the following code to login to asp website but it doesnt
I have developed the following code: CREATE PROCEDURE [dbo].[Test01] AS BEGIN SELECT * FROM
Update: I left the following javascript code in to show how the problem developed
Ok so I developed the following code to use Google Maps with Marker Clusterer.
The following code snippet is from The Official GNOME 2 Developer's Guide : GMemChunk
At the following URL: https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsICacheVisitor is the following code chunk: boolean visitDevice(in string deviceID,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.