buyform.aspx.vb

来自「Mastering VBNet Include Source Code」· VB 代码 · 共 166 行

VB
166
字号
Imports System.Data.SqlClient
Public Class BuyForm
    Inherits System.Web.UI.Page
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents Label3 As System.Web.UI.WebControls.Label
    Protected WithEvents RadioButtonList1 As System.Web.UI.WebControls.RadioButtonList
    Protected WithEvents Label4 As System.Web.UI.WebControls.Label
    Protected WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
    Protected WithEvents DACustomers As System.Data.SqlClient.SqlDataAdapter
    Protected WithEvents DsCustomers1 As ProductSearch.DSCustomers
    Protected WithEvents lblError As System.Web.UI.WebControls.Label
    Protected WithEvents txtAddress As System.Web.UI.WebControls.TextBox
    Protected WithEvents lblTotal As System.Web.UI.WebControls.Label
    Protected WithEvents bttnShip As System.Web.UI.WebControls.Button
    Protected WithEvents CustomerID As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection()
        Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand()
        Me.DACustomers = New System.Data.SqlClient.SqlDataAdapter()
        Me.DsCustomers1 = New ProductSearch.DSCustomers()
        CType(Me.DsCustomers1, System.ComponentModel.ISupportInitialize).BeginInit()
        '
        'SqlConnection1
        '
        Me.SqlConnection1.ConnectionString = "data source=(local);initial catalog=Northwind;persist security info=False;user id" & _
        "=sa;workstation id=PROTSERVER;packet size=4096"
        '
        'SqlSelectCommand1
        '
        Me.SqlSelectCommand1.CommandText = "SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region," & _
        " PostalCode, Country, Phone, Fax FROM dbo.Customers WHERE (CustomerID = @custID)" & _
        ""
        Me.SqlSelectCommand1.Connection = Me.SqlConnection1
        Me.SqlSelectCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@custID", System.Data.SqlDbType.NChar, 5, "CustomerID"))
        '
        'DACustomers
        '
        Me.DACustomers.SelectCommand = Me.SqlSelectCommand1
        Me.DACustomers.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "Customers", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"), New System.Data.Common.DataColumnMapping("CompanyName", "CompanyName"), New System.Data.Common.DataColumnMapping("ContactName", "ContactName"), New System.Data.Common.DataColumnMapping("ContactTitle", "ContactTitle"), New System.Data.Common.DataColumnMapping("Address", "Address"), New System.Data.Common.DataColumnMapping("City", "City"), New System.Data.Common.DataColumnMapping("Region", "Region"), New System.Data.Common.DataColumnMapping("PostalCode", "PostalCode"), New System.Data.Common.DataColumnMapping("Country", "Country"), New System.Data.Common.DataColumnMapping("Phone", "Phone"), New System.Data.Common.DataColumnMapping("Fax", "Fax")})})
        '
        'DsCustomers1
        '
        Me.DsCustomers1.DataSetName = "DSCustomers"
        Me.DsCustomers1.Locale = New System.Globalization.CultureInfo("en-US")
        Me.DsCustomers1.Namespace = "http://www.tempuri.org/DSCustomers.xsd"
        CType(Me.DsCustomers1, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        lblTotal.Text = "Your Order's Total is " & Request.QueryString("Total")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DACustomers.SelectCommand.Parameters("@CustID").Value = TextBox1.Text
        DACustomers.Fill(DsCustomers1, "Customers")
        If DsCustomers1.Customers.Rows.Count <> 1 Then
            lblError.Visible = True
        Else
            lblError.Visible = False
            Dim Address As String
            Address = DsCustomers1.Customers.Item(0).CompanyName & vbCrLf & _
                      DsCustomers1.Customers.Item(0).ContactName & vbCrLf & _
                      DsCustomers1.Customers.Item(0).Address & vbCrLf & _
                      DsCustomers1.Customers.Item(0).City & vbCrLf & _
                      DsCustomers1.Customers.Item(0).PostalCode & vbCrLf & _
                      DsCustomers1.Customers.Item(0).Country
            txtAddress.Text = Address
            bttnShip.Visible = True
            CustomerID.Value = TextBox1.Text
        End If
    End Sub

    Private Sub bttnShip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnShip.Click
        'Dim myConnection As New SqlConnection()
        'myConnection.ConnectionString = "data source=PowerToolkit;initial catalog=Northwind;integrated security=SSPI"
        Dim msg As String
        SqlConnection1.Open()

        Dim myCommand As New SqlCommand()
        myCommand.Connection = SqlConnection1

        Dim myTrans As SqlTransaction
        myTrans = SqlConnection1.BeginTransaction()
        myCommand.Transaction = myTrans

        Try
            myCommand.CommandText = "NewOrder"
            myCommand.CommandType = CommandType.StoredProcedure
            Dim p As New SqlParameter()
            p.ParameterName = "@CustID"
            p.Direction = ParameterDirection.Input
            p.SqlDbType = SqlDbType.Char
            p.Value = CustomerID.Value
            myCommand.Parameters.Add(p)
            p = New SqlParameter()
            p.ParameterName = "RETURN"
            p.Direction = ParameterDirection.ReturnValue
            p.SqlDbType = SqlDbType.Int
            myCommand.Parameters.Add(p)

            myCommand.ExecuteScalar()
            Dim orderID As Integer = CType(myCommand.Parameters("RETURN").Value, Integer)

            myCommand.CommandText = "NewOrderLine"
            myCommand.CommandType = CommandType.StoredProcedure
            myCommand.Parameters.Clear()

            Dim ck As Object
            Dim pID As Integer

            For Each ck In Session
                myCommand.Parameters.Clear()
                p = New SqlParameter()
                p.ParameterName = "@OrderID"
                p.Direction = ParameterDirection.Input
                p.SqlDbType = SqlDbType.Int
                p.Value = orderID
                myCommand.Parameters.Add(p)

                p = New SqlParameter()
                p.ParameterName = "@ProductID"
                p.Direction = ParameterDirection.Input
                p.SqlDbType = SqlDbType.Int
                p.Value = ck
                myCommand.Parameters.Add(p)
                p = New SqlParameter()
                p.ParameterName = "@Quantity"
                p.Direction = ParameterDirection.Input
                p.SqlDbType = SqlDbType.Int
                p.Value = Session(CStr(ck))
                myCommand.Parameters.Add(p)
                myCommand.ExecuteNonQuery()
            Next

            myTrans.Commit()
            msg = "<H1>Thank your for your order.</H1>"
        Catch exc As Exception
            myTrans.Rollback()
            msg = "<H1>Error in recording your order</H1>"
            msg = msg & "<BR>"
            msg = msg & "We were unable to accept your order due to the following error " & exc.Message
        Finally
            SqlConnection1.Close()
            Response.Redirect("ThanksPage.aspx?msg=" & Server.UrlEncode(msg))
        End Try
    End Sub
End Class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?