Use ado + to delete data

Source: Internet
Author: User

<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. SQL" %>

<Html>

<Script language = "VB" runat = "server">

Dim MyConnection As SQLConnection

Sub Page_Load (Src As Object, E As EventArgs)

MyConnection = New SQLConnection ("server = YOUR-SERVER; uid = joeuser; pwd = joeuser; database = pubs ")

If Not (IsPostBack)
BindGrid ()
End If
End Sub

Sub MyDataGrid_Delete (Sender As Object, E As DataGridCommandEventArgs)

Dim MyCommand As SQLCommand
Dim DeleteCmd As String = "DELETE from Authors where au_id = @ Id"

MyCommand = New SQLCommand (DeleteCmd, MyConnection)
MyCommand. Parameters. Add (New SQLParameter ("@ Id", SQLDataType. VarChar, 11 ))
MyCommand. Parameters ("@ Id"). Value = MyDataGrid. DataKeys (CInt (E. Item. ItemIndex ))

MyCommand. ActiveConnection. Open ()

Try
MyCommand. ExecuteNonQuery ()
Message. InnerHtml = "<B> Record Deleted </B> <br>" & DeleteCmd
Catch Exp As SQLException
Message. InnerHtml = "ERROR: cocould not delete record"
Message. Style ("color") = "red"
End Try

MyCommand. ActiveConnection. Close ()

BindGrid ()
End Sub

Sub BindGrid ()

Dim DS As DataSet
Dim MyCommand As SQLDataSetCommand
MyCommand = New SQLDataSetCommand ("select * from Authors", MyConnection)

DS = new DataSet ()
MyCommand. FillDataSet (DS, "Authors ")

MyDataGrid. DataSource = DS. Tables ("Authors"). DefaultView
MyDataGrid. DataBind ()
End Sub

</Script>

<Body style = "font: 10pt verdana">

<Form runat = "server">

<H3> <font face = "Verdana"> Deleting a Row of Data </font>

<Span id = "Message" MaintainState = "false" style = "font: arial 11pt;" runat = "server"/> <p>

<ASP: DataGrid id = "MyDataGrid" runat = "server"
Width = "800"
BackColor = "# ccccff"
BorderColor = "black"
ShowFooter = "false"
CellPadding = 3
CellSpacing = "0"
Font-Name = "Verdana"
Font-Size = "8pt"
HeaderStyle-BackColor = "# aaaadd"
DataKeyField = "au_id"
OnDeleteCommand = "MyDataGrid_Delete"
>

<Property name = "Columns">
<Asp: ButtonColumn Text = "Delete Author" CommandName = "Delete"/>
</Property>

</ASP: DataGrid>

</Form>

</Body>
</Html>


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.