Note that there are some explanatory texts on larger screens.

plurals
  1. POasp.net how can i delete the current record with this method?
    primarykey
    data
    text
    <p><b>Default.aspx:</b> (i dynamically generated table rows on the page with data from a database table having 2 fields as you can see: <code>ID</code> and <code>studentname</code>)(btw i deleted from code below <code>&lt;table&gt;,&lt;tr&gt;,&lt;td&gt;</code> for the sake of the example clarity)</p> <pre class="lang-asp prettyprint-override"><code>&lt;asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="C:\db1.mdb" SelectCommand="SELECT * FROM [table1]"&gt; &lt;/asp:AccessDataSource&gt; &lt;asp:DataList ID="DataList1" runat="server" DataSourceID="AccessDataSource1" DataKeyField="ID" RepeatColumns="3" RepeatDirection="Horizontal"&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="Label1" runat="server" Text='&lt;%# "ID: " &amp; Eval("ID") %&gt;'&gt;&lt;/asp:Label&gt; &lt;asp:Label ID="Label2" runat="server" Text='&lt;%# Eval("studentname") %&gt;'&gt;&lt;/asp:Label&gt; &lt;asp:Button ID="Button1" runat="server" Text="Delete Row Button" OnClick="Button1_Click" /&gt; &lt;/ItemTemplate&gt; &lt;/asp:DataList&gt; </code></pre> <p>So if my database table has 2 records then a table with 2 rows is generated on page like this, and i created a delete button with each row:<br> ----------------<BR> | 1 | name1 | Delete Row Button<BR> ----------------<BR> | 2 | name2 | Delete Row Button<BR> ----------------<BR></p> <p><b>Default.aspx.vb:</b> (code-behind for the command button)</p> <pre class="lang-asp prettyprint-override"><code>Dim x As New Connection Protected Sub Button1_Click(ByVal a As Object, ByVal b As System.EventArgs) x.deleterowclass("1") End Sub </code></pre> <p><b>connection.vb:</b> (a class i created where i do the record deletion)</p> <pre class="lang-asp prettyprint-override"><code>Imports System.Data.OleDb Imports System.Data Imports Microsoft.VisualBasic Dim str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\db1.mdb" Dim con As New OleDbConnection(str) Public Sub deleterowclass(ByVal ID As Integer) con.Open() Dim adp As New OleDbCommand("Delete * from table1 where ID=" &amp; ID, con) adp.ExecuteNonQuery() con.Close() End Sub </code></pre> <p><br><h1>The problem:</h1> - The button code above deletes a record, from the access database, only which ID is "1" as i specified: <code>x.deleterowclass("1")</code> <BR> - since with each record in the database there is a new button created called <code>Button1</code> beside each row on the page, i'm not sure if it's a problem or an advantage to solve the problem since it does work.</p> <h1>My question:</h1> <p>How can I delete any record by clicking on the button that exists beside it (i mean beside the row on the page)? my thought is to work on the ID too and to pass it somewhere but unfortunately i'm not sure how i can accomplish it, i tried many parameters like this <code>OnClick="Button2_Click(&lt;%# Eval("ID") %&gt;)</code> but i failed with error message: The server tag is not well formed.</p> <p><BR><BR> <h2>Edit: Solution:</h2>Thank you so much atbebtg, here is the correspondent code in VB below in case someone may find it useful.</p> <p><b>The button code in Default.aspx (same as your answer but i thought to put it here too):</b></p> <pre class="lang-asp prettyprint-override"><code>&lt;asp:Button ID="Button1" runat="server" Text="Delete Row Button" OnClick="Button1_Click" CommandArgument='&lt;%# Eval("MyID") %&gt;'/&gt; </code></pre> <p><b>OnClick code in VB:</b></p> <pre class="lang-vb prettyprint-override"><code>Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim mybutton As Button = DirectCast(sender, Button) x.deleterowclass(Convert.ToInt32(mybutton.CommandArgument)) End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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