Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use delete within a GridView in Asp.Net
    text
    copied!<p>I need a way to grab the values of columns of the row whose "Delete" button was hit in a data-bound GridView. The values in the data-bound GridView are those from a linked table, and upon hitting "Delete" on one of the rows, I would like to delete the entry from the database.</p> <p>This is my gridview</p> <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="Session[userId'],courseId" //&lt; ? for 'userId' style="z-index: 1; left: 222px; top: 139px; position: absolute; height: 299px; width: 458px"&gt; &lt;Columns&gt; &lt;asp:CommandField ShowDeleteButton="True" /&gt; &lt;asp:BoundField DataField="department" HeaderText="Department" SortExpression="department" /&gt; &lt;asp:BoundField DataField="courseCode" HeaderText="Course" SortExpression="courseCode" /&gt; &lt;asp:BoundField DataField="courseName" HeaderText="Name" SortExpression="courseName" /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; </code></pre> <p>This is my current, work in progress of my SQLDataSource. I will need the above "courseCode" and "courseName" values to select (from a different) table an unique "courseId" which I use with a session variable Session["userId"] to delete the entry from my table, thus updating my GridView.</p> <p>It's important to say here that my select statement is only taking in one variable (it is using Session["userId"]), while my delete statement doesn't only use Session["userId"], it also uses two other variables. </p> <pre><code>&lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="&lt;%$ ConnectionStrings:ConnectionString %&gt;" SelectCommand="GetPreviouslyTakenCourses" SelectCommandType="StoredProcedure" DeleteCommand="DELETE FROM UserHasCourse WHERE UserHasCourse.userId=@userId AND (SELECT Id FROM Courses WHERE courseCode=@courseCode AND courseName=@courseName)"&gt; &lt;SelectParameters&gt; &lt;asp:SessionParameter DefaultValue="1" Name="userId" SessionField="userId" Size="10" Type="Int32" /&gt; &lt;/SelectParameters&gt; &lt;DeleteParameters&gt; &lt;asp:SessionParameter Name="userId" SessionField="userId" /&gt; &lt;asp:Parameter Name="courseCode" /&gt; &lt;asp:Parameter Name="courseName" /&gt; &lt;/DeleteParameters&gt; &lt;/asp:SqlDataSource&gt; </code></pre> <p>I know I am missing a few things, if there is any additional information needed I can update my question.</p> <p><strong>Update 1:</strong> Need to move DataKeyNames into the GridView. An additional question is now how to pass a Session variable into DataKeyNames</p> <p><strong>Update 2:</strong> ControlID was removed. ControlParameter changed to Parameter within SQLDataSource (Is this correct?)</p> <p><strong>Update 3:</strong> Am I able to grab the userId value (which is equal to Session["userId"]) within my select query, and make it hidden, thus able to use it in my delete statement?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload