c#


Deleting a folder from database


i want to delete a folder from my database when button is clicked.
...
<td><%#Eval("FolderId").ToString() %></td>
...
I list files using the file ID.
Here is my btnDelete codes;
protected void btnDelete_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString);
con.Open();
SqlCommand del = new SqlCommand("delete * from Folders where FolderId=#id");
del.Parameters.AddWithValue("id", ???);
del.ExecuteNonQuery();
con.Close();
}
What should I write to the #id parameter value?
<asp:Repeater ID="rptC" runat="server">
<HeaderTemplate>
<center><table cellpadding="0" cellspacing="0" width="400">
<caption><b>Files</b></caption>
<thead>
<tr>
<th>File ID</th>
<th>Path</th>
<th>?</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tbody>
<tr>
<td><%#Eval("FolderId").ToString() %></td>
<td><%#Eval("FilePath").ToString() %></td>
<td><asp:Button ID="btnDelete" CssClass="btnH" ForeColor="#ffffff" BackColor="#ba04c2" Text="Delete" runat="server" OnClick="btnDelete_Click"/></td>
</tr>
</tbody>
</ItemTemplate>
<FooterTemplate>
</table> </center>
</FooterTemplate>
</asp:Repeater>
The proper way to implement that is using the Repeater ItemCommand event.
Markup
<asp:Repeater ID="repeater" runat="server" OnItemCommand="repeater_ItemCommand">
<ItemTemplate>
<%# Eval("Text") %>
<asp:LinkButton runat="server" ID="DeleteButton" Text="Text" CommandName="Delete" CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
Code Beside
protected void Page_Load(object sender, EventArgs e)
{
repeater.DataSource = new[] {
new {Id= 1, Text = "Text 1" },
new {Id= 2, Text = "Text 2" },
};
repeater.DataBind();
}
protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
Response.Write($"Do something with {e.CommandArgument}");
}
}
Source Code here
https://github.com/kblok/StackOverflowExamples/blob/master/AspNetDemoProject/AspNetDemoProject/Demos/RepeaterDemo.aspx
https://github.com/kblok/StackOverflowExamples/blob/master/AspNetDemoProject/AspNetDemoProject/Demos/RepeaterDemo.aspx.cs

Related Links

what is using SR=MS.Internal.PresentationCore.SR?
Design pattern for similar processing steps, but with slight variations
How can I declare a pointer to a struct for P/Invoke?
Check if FieldInfo type is generic class without type
Desktop application not able to connect to web services
datagridview all cell values pass to label
sql query for getting data between 2 dates which are explicitly given
Scale out PhantomJs istances on Azure in .Net
How to set help information for pscmdlet class
How to run a function or a piece of code at compile time in C#
Exclude a Controller from Default Route C# MVC
LINQ grouping and counting
Visual studio online continuous deployment to Azure SQL fail
EF Complex Query Join
RegEx ignoring part of string to extract out text
Newtonsoft JSON refusing to deserialize XNA rectangle

Categories

HOME
actionscript-3
javafx
stanford-nlp
mobile
app-store
hiveql
swap
osx-yosemite
theory
ldap-query
mbed
simd
powerpivot
google-street-view
autologin
xna
connectiq
procedural-generation
scrapy-spider
rancher
akavache
dynamic-memory-allocation
sqldependency
ios-autolayout
offset
motion
armadillo
linkurious
rbac
file-descriptor
servicebus
logan-square
sp
suitesparse
group
sqlproj
jigsaw
android-progressbar
videoview
magnet-uri
libgcrypt
web-standards
vspackage
xmla
cordova-3
parallel-port
opencobol
elixir-framework
fastcgi-mono-server
logo-lang
xml-validation
sygic
grape-api
listpicker
friendly-url
brew
database-restore
tasklist
derived-class
commonsware
gmagick
windows-xp-embedded
activescaffold
linq-to-mysql
newspeak
mdd
script-tag
ihtmldocument2
ffdshow
software-update

Resources

Encrypt Message