locked
loop sqlupdate with gridview RRS feed

  • Question

  • User1397998045 posted

    Afternoon asp.net

    Ive spend some time trying to figure out how to accomplish this task properly, The way it works now, it will update a single row, but not anymore, it fails on mult row updates staying the placeholds have been used however if i pass the UpdateParameters.Clear() it fails saying the  placeholders are undefined.

    Could someone give some insite on to how to correct the problem I'd ve very grateful.

        Protected Indicator As Object
        Protected Tflag As Object
        Protected ref As Object    
        
        Protected Sub RefreshData()
            ' Here submit the data to the DB
    
    
            SqlDataSource1.UpdateCommand = "UPDATE [header] SET [Tflag] = @Tflag, [Indicator] = @Indicator WHERE [Ref] = @Ref"
            SqlDataSource1.UpdateParameters.Add("Indicator", Indicator.ToString)
            SqlDataSource1.UpdateParameters.Add("Tflag", Tflag.ToString)
            SqlDataSource1.UpdateParameters.Add("ref", ref)
            SqlDataSource1.Update()
            SqlDataSource1.DataBind()
            'SqlDataSource1.UpdateParameters.Clear()
    
        End Sub
        Protected Sub GridView1_RowUpdating1(sender As Object, e As GridViewUpdateEventArgs)
    
            For Each row As GridViewRow In BulkEditGridView1.DirtyRows
    
    
                ref = BulkEditGridView1.Rows(e.RowIndex).FindControl("Ref")
    
                Indicator = CType(BulkEditGridView1.Rows(e.RowIndex).FindControl("Indicator"), CheckBox).Checked()
    
                Dim Tflagbol = CType(BulkEditGridView1.Rows(e.RowIndex).FindControl("Tflag"), CheckBox).Checked()
                If Tflagbol = True Then
                    Tflag = "-1"
                Else
                    Tflag = "   "
                End If
    
                RefreshData()
            Next
        End Sub
    
    
    



    Wednesday, April 24, 2013 3:07 PM

All replies

  • User-1716253493 posted

    To simplify the code, place update command and parameter directly to sqldatasource

            UpdateCommand="UPDATE [header] SET [Tflag] = @Tflag, [Indicator] = @Indicator WHERE [Ref] = @Ref">
            <UpdateParameters>
                <asp:Parameter Name="Tflag" />
                <asp:Parameter Name="Indicator " />
                <asp:Parameter Name="Ref" />
            </UpdateParameters>

    To update

            SqlDataSource1.UpdateParameters("Indicator").DefaultValue = Indicator.ToString
            SqlDataSource1.UpdateParameters("Tflag").DefaultValue = Tflag.ToString
            SqlDataSource1.UpdateParameters("ref").DefaultValue = ref
            SqlDataSource1.Update()

    To get control maybe like this

                Indicator = CType(row.FindControl("Indicator"), CheckBox).Checked()
                Dim Tflagbol = CType(row.FindControl("Tflag"), CheckBox).Checked()





    Wednesday, April 24, 2013 10:54 PM
  • User1397998045 posted

    That Doesn't work on this project a because the columns and check boxes in the gird view are passing a "null" value, which is why I'm having to manipulate the data before sending it into the sql source.

    Also everything is being stored in a CHAR value, because the guy who created this legacy program didn't understand databases

    Thursday, April 25, 2013 1:30 PM