Use the delete function in Asp.net 2.0

Source: Internet
Author: User

In 4guysfromrolla, I saw an article about how to use the delete function in Asp.net 2.0. Article Http://aspnet.4guysfromrolla.com/articles/062007-1.aspx ),
I think it is very good. Now I will take some notes, using the example of northwind.

1) Delete the project in dropdownlist
<Asp: dropdownlist id = "productlist" runat = "server" performanceid = "productsperformancefordropdownlist"
Datatextfield = "productname" datavaluefield = "productid">
</ASP: dropdownlist>

<Asp: sqldatasource id = "productsperformancefordropdownlist" runat = "server"
Connectionstring = "<% $ connectionstrings: northwindconnectionstring %>" deletecommand = "delete from [products] Where [productid] = @ productid"
Selectcommand = "select [productid], [productname] from [products] Order by [productname]">
<Deleteparameters>
<Asp: controlparameter controlid = "productlist" name = "productid" propertyname = "selectedvalue"
Type = "int32"/>
</Deleteparameters>
</ASP: sqldatasource>
<Asp: button id = "btndeleteproduct" runat = "server" text = "delete selected product"/>

In the delete event, write the followingCode
Protected sub btndeleteproduct_click (byval sender as object, byval e as system. eventargs) handles btndeleteproduct. Click
'Programmatically call the delete Method
Productsperformancefordropdownlist. Delete ()

Dim productjustdeleted as string = productlist. selecteditem. Text
Clientscript. registerstartupscript (Me. getType (), "message", String. format ("alert ('the product {0} has just been deleted... '); ", productjustdeleted. replace ("'", "\'"), true)

'Rebind the data to the dropdownlist so that the just-deleted product no longer appears
Productlist. databind ()
End sub

2 delete in the gridview
This is a common usage

deletecommand =" delete from [products] Where [productid] = @ productid "
selectcommand =" select [productid], [productname], [unitprice], [Discontinued] from [products] Order by [productname] ">



datakeynames = "productid"
3. deletion is not allowed based on certain conditions. For example, if you want to specify a product with a unit price of <500, you are not allowed to delete it when you click Delete. You need to use the
deleting event
protected sub productsdatasource_deleting (byval sender as object, byval e as system. web. UI. webcontrols. sqldatasourcecommandeventargs) handles productsdatasource. deleting
'termine if the product being deleted has a unit price >$ 50
dim productid as integer = convert. toint32 (E. command. parameters ("@ productid "). value)

'termine the unit price of the product that the user wants to delete...
'there are always ways you can accomplish this-use a sqldatasource control, write code to query the database...
'let's Use Data Access Code
dim myconnection as new sqlconnection (configurationmanager. connectionstrings ("northwindconnectionstring "). connectionstring)
const strsql as string = "select unitprice from products where productid = @ productid"
dim mycommand as new sqlcommand (strsql, myconnection)
mycommand. parameters. addwithvalue ("@ productid", productid)

myconnection. open ()
dim price as object = mycommand. executescalar
myconnection. Close ()

'prohibit the Delete if price is not a database null and it is greater than $50
if not convert. isdbnull (price) andalso convert. todecimal (price)> 50 then
E. cancel = true 'cancel the delete
cannotdeletemessage. visible = true 'show a message explaining why
end if
end sub
note that dim productid as integer = convert. toint32 (E. command. parameters ("@ productid "). value)
and E. cancel = true 'cancel the delete

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.