Asp.Net GridView Drag and Drop online example update other fields help!

kouts1
kouts1 used Ask the Experts™
on
Hi,

Hi, I am using this online solution for Grideview Drag and Drop asp.net web forms (https://www.aspsnippets.com/Articles/Reorder-GridView-Rows-Drag-and-Drop-ordering-of-GridView-Rows-using-jQuery-in-ASPNet.aspx).  Everything works fine but now I need help updating the database table with additional fields.  For example:  I need to update the table of who dragged and dropped the row.  I added the "UpdateBy" field to my update statement , it works, but it updates every row with the value not the one row that was dropped.  What am i missing? Please need help asap.

  

  Private Sub BindGrid()
        Dim query As String = "SELECT [id], [DisplayOrder], [STUDY], [PATIENT],[WAIT_TIME],[USER_ID],[UpdateBy] FROM [Radio_queue4] " +
"order by DisplayOrder asc"
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand(query)
                Using sda As New SqlDataAdapter()
                    cmd.CommandType = CommandType.Text
                    cmd.Connection = con
                    sda.SelectCommand = cmd
                    Using dt As New DataTable()
                        sda.Fill(dt)
                        grid.DataSource = dt
                        grid.DataBind()
                    End Using
                End Using
            End Using
        End Using
    End Sub

 Protected Sub UpdatePreference(sender As Object, e As EventArgs)
        Dim ids As Integer() = (From p In Request.Form("id1").Split(",")
                                Select Integer.Parse(p)).ToArray()
        Dim DisplayOrder As Integer = 1

        For Each id As Integer In ids
            Me.UpdatePreference(id, DisplayOrder)
            DisplayOrder += 1
        Next

        Response.Redirect(Request.Url.AbsoluteUri)
    End Sub



Private Sub UpdatePreference(id As Integer, DisplayOrder As Integer)
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As New System.Data.SqlClient.SqlConnection(constr)
            Using cmd As New SqlCommand("UPDATE Radio_queue4 SET DisplayOrder = @DisplayOrder, UpdateBy = @UpdateBy WHERE id = @id")
                Using sda As New SqlDataAdapter()
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.AddWithValue("@id", id)
                    cmd.Parameters.AddWithValue("@DisplayOrder", DisplayOrder)
                    cmd.Parameters.AddWithValue("@UpdateBy", "TestUser")
                    cmd.Connection = con
                    con.Open()
                    cmd.ExecuteNonQuery()
                    con.Close()
                End Using
            End Using
        End Using
    End Sub

 Private Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
        If Not IsPostBack Then
            Me.BindGrid()
        
        End If
    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi kouts1,

This is the culprit. You should only add this parameter for the row which was being updated. Currently, you are executing
                    cmd.Parameters.AddWithValue("@UpdateBy", "TestUser")

Open in new window

You will have to use a hidden field OR a command parameter to send the row ID which was updated by the user to the server side and after that your code should be something like this.

If rowId = id Then
                    cmd.Parameters.AddWithValue("@UpdateBy", "TestUser")
End If

Open in new window


Regards,
Chinmay.

Author

Commented:
Thank you very much!
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Glad I could help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial