locked
DELETE RECORD IN GRIDVIEW USING TWO PARAMETERS RRS feed

  • Question

  • User-1495932509 posted

    Hello,

    Please i have problem of deleting record using gridview control and datasource, please ineed your help

    Bellow is my HTML Document and Code

     <asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="False" DataSourceID="firstsemesterdatacourse"
    Width="801px">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:CheckBox ID="cbRows" runat="server"/>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="coursecode"
    HeaderText="Course Code" SortExpression="coursecode" />
    <asp:BoundField DataField="title"
    HeaderText="Course Title" SortExpression="title" />
    <asp:BoundField DataField="unit" HeaderText="Unit"
    SortExpression="unit" />
    <asp:BoundField DataField="status" HeaderText="Status"
    SortExpression="status" />
    <asp:BoundField DataField="semester"
    HeaderText="Semester" SortExpression="semester" />
    </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="firstsemesterdatacourse" runat="server" ConnectionString="<%$ ConnectionStrings:SSPConnect %>"

    SelectCommand="SELECT [coursecode], [title], [unit], [status], [semester] FROM [tblrecordbook] WHERE (([matricno] = @matricno) AND ([semester] = @semester) AND ([semlevel] = @semlevel))"
    DeleteCommand="DELETE FROM [tblrecordbook] WHERE [coursecode] = @coursecode AND [matricno] = @matricno"
    >

    <SelectParameters>
    <asp:SessionParameter Name="matricno"
    SessionField="matno" Type="String" />
    <asp:SessionParameter DefaultValue="First"
    Name="semester" SessionField="semester" Type="String" />
    <asp:SessionParameter DefaultValue="100" Name="semlevel"
    SessionField="level" Type="String" />
    </SelectParameters>
    <DeleteParameters>
    <asp:Parameter Name="coursecode" />
    <asp:SessionParameter Name="matricno"
    SessionField="matno" Type="String" />
    </DeleteParameters>
    </asp:SqlDataSource>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Button ID="btndelete1" runat="server" Text="Drop Course" />
    </td>
    
    
    COD BEHIND
    
    
    ' Looping through all the rows in the GridView

    For Each row As GridViewRow In GridView1.Rows

    Dim checkbox As CheckBox = CType(row.FindControl("cbRows"), CheckBox)

    'Check if the checkbox is checked.

    'value in the HtmlInputCheckBox's Value property is set as the //value of the delete command's parameter.

    If checkbox.Checked Then

    ' Retreive the Employee ID

    Dim coursecode As String = Convert.ToString(GridView1.DataKeys(row.RowIndex).Value)

    ' Pass the value of the selected Employye ID to the Delete //command.

    firstsemesterdatacourse.DeleteParameters("coursecode").DefaultValue = coursecode.ToString()
    firstsemesterdatacourse.DeleteParameters("matricno").DefaultValue = Session("matno")

    firstsemesterdatacourse.Delete()

    End If

    Next row
    POINT OF CORRECTION
      DeleteCommand="DELETE FROM [tblrecordbook]  WHERE [coursecode] = @coursecode AND [matricno] = @matricno" 
    PLEASE HELP ME WITH THAT
    Tuesday, October 11, 2011 4:24 AM

Answers

  • User3866881 posted

    Hello:)

    This error means that you've lacked of setting DataKeyNames for the GridView, So the solution can be easy like this:

    <asp:GridView ID="GridView1" runat="server"  DataKeyNames="Your Primary Key Here!"
                    AutoGenerateColumns="False" DataSourceID="firstsemesterdatacourse"
                    Width="801px">

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 12, 2011 9:19 PM

All replies

  • User-1812063630 posted

    Hi aliyuusman4u,

    Please try the following code

     For Each row As GridViewRow In GridView1.Rows
    
                Dim checkbox As CheckBox = CType(row.FindControl("cbRows"), CheckBox)
    
                'Check if the checkbox is checked.
    
                'value in the HtmlInputCheckBox's Value property is set as the //value of the delete command's parameter.
    
                If checkbox.Checked Then
    
                    Dim coursecode As String = Convert.ToString(GridView1.DataKeys(row.RowIndex).Value)
    
    	        firstsemesterdatacourse.DeleteCommand="DELETE FROM [tblrecordbook]  WHERE [coursecode] = @coursecode AND [matricno] = @matricno" 
    	        firstsemesterdatacourse.DeleteParameters.Clear()
                    firstsemesterdatacourse.DeleteParameters.Add("coursecode", coursecode.ToString())
                    firstsemesterdatacourse.DeleteParameters.Add("matricno", Session("matno").ToString())
                    firstsemesterdatacourse.Delete()
    
                End If
    
            Next row

    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.

    Tuesday, October 11, 2011 7:14 AM
  • User-1495932509 posted

    HELLO SIR I STILL HAVE THIS ERROR, AND I TRY ALL MY BEST BUT YET NOT DONE. BELLOW IS THE ERROR I GOT

    Server Error in '/' Application.


    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    Source Error: 

    Line 224:            If checkbox.Checked Then
    Line 225:
    Line 226: Dim coursecode As String = Convert.ToString(GridView1.DataKeys(row.RowIndex).Value) Line 227:
    Line 228:                firstsemesterdatacourse.DeleteCommand = "DELETE FROM [tblrecordbook]  WHERE [coursecode] = @coursecode AND [matricno] = @matricno"
    Tuesday, October 11, 2011 8:30 AM
  • User3866881 posted

    Hello:)

    This error means that you've lacked of setting DataKeyNames for the GridView, So the solution can be easy like this:

    <asp:GridView ID="GridView1" runat="server"  DataKeyNames="Your Primary Key Here!"
                    AutoGenerateColumns="False" DataSourceID="firstsemesterdatacourse"
                    Width="801px">

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 12, 2011 9:19 PM
  • User-1495932509 posted

    Dear Brother,

    your Support is nice, the problem is solved

    thanks

    Saturday, October 15, 2011 6:19 AM