Share via

Gridview binding data not deleting properly

BeUnique 2,332 Reputation points
2021-12-30T12:25:15.203+00:00

I am using Gridview and binding some fields from compdata.

But, when i am trying to delete, the data will not be deleting the specific row.

what is the problem..?

pls. find the sample source code as below.

161419-source-code.txt

Developer technologies | ASP.NET Core | Other

Answer accepted by question author

AgaveJoe 31,361 Reputation points
2021-12-30T14:01:57.643+00:00

You've defined an @ID parameter but there is no code that configures the @ID parameter. Most beginning level GridView/SQlDataSource CRUD tutorials take advantage of DataKeyNames to set a primary key parameter. Please go through the following tutorial to learn GridView/SqldataSouce fundamentals.

An Overview of Inserting, Updating, and Deleting Data (C#)

<form id="form1" runat="server">  
        <div>  
            <asp:GridView ID="GridView1"   
                runat="server"   
                AllowPaging="True"   
                AllowSorting="True"   
                AutoGenerateColumns="False"   
                DataKeyNames="ID"   
                DataSourceID="SqlDataSource1">  
                <Columns>  
                    <asp:CommandField ShowSelectButton="True" />  
                    <asp:BoundField DataField="EmpNo" HeaderText="EmpNo" SortExpression="EmpNo" />  
                    <asp:BoundField DataField="EmpDesc" HeaderText="EmpDesc" SortExpression="EmpDesc" />  
                    <asp:BoundField DataField="EPFNo" HeaderText="EPFNo" SortExpression="EPFNo" />  
                    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />  
                    <asp:TemplateField ShowHeader="False">  
                        <ItemTemplate>  
                            <asp:LinkButton ID="LinkButton1"   
                                runat="server"   
                                CausesValidation="False"   
                                CommandName="Delete"   
                                Text="Delete"   
                                OnClientClick="return isDelete();"></asp:LinkButton>  
                        </ItemTemplate>  
                    </asp:TemplateField>  
                </Columns>  
            </asp:GridView>  
            <asp:SqlDataSource ID="SqlDataSource1"   
                runat="server"   
                ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"   
                SelectCommand="SELECT [EmpNo], [EmpDesc], [EPFNo], [ID] FROM [empdata]"   
                DeleteCommand="DELETE FROM empdata WHERE (ID = @ID)">  
                <DeleteParameters>  
                    <asp:ControlParameter ControlID="GridView1" DefaultValue="0" Name="ID" PropertyName="SelectedValue" />  
                </DeleteParameters>  
            </asp:SqlDataSource>  
        </div>  
    </form>  

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.