shopbike.aspx.vb
来自「这是用ASP.NET和SQLserver编写的购物网站的程序」· VB 代码 · 共 243 行
VB
243 行
Imports System.Data.SqlClient
Imports System.Data
Public Class shopbike
Inherits System.Web.UI.Page
Dim cstring As String = ConfigurationSettings.AppSettings("ConnectionString")
Protected WithEvents Button3 As System.Web.UI.WebControls.Button
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Dim conn As SqlConnection = New SqlConnection(cstring) '定义数据库连接
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Button4 As System.Web.UI.WebControls.Button
Protected WithEvents Label3 As System.Web.UI.WebControls.Label
Protected WithEvents Hyperlink11 As System.Web.UI.WebControls.HyperLink
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Dim conn1 As SqlConnection = New SqlConnection(cstring) '定义数据库连接
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents ImageButton1 As System.Web.UI.WebControls.ImageButton
Protected WithEvents HyperLink6 As System.Web.UI.WebControls.HyperLink
Protected WithEvents HyperLink5 As System.Web.UI.WebControls.HyperLink
Protected WithEvents HyperLink4 As System.Web.UI.WebControls.HyperLink
Protected WithEvents HyperLink3 As System.Web.UI.WebControls.HyperLink
Protected WithEvents HyperLink1 As System.Web.UI.WebControls.HyperLink
Protected WithEvents HyperLink2 As System.Web.UI.WebControls.HyperLink
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Dim a As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Not IsPostBack Then
Try
If Session("userpower") <> 1 Then
Response.End()
End If
Catch ex As Exception
Response.Write("您还未登陆,请登陆后再购物!<a href='login.aspx'>登陆</a>")
Response.End()
End Try
Session("userid") = Session("username")
conn.Open()
Dim cmd As New SqlCommand
Session("itemid") = Request.QueryString("title_id")
If Session("itemid") <> "" Then
cmd.CommandText = "select itemname,listprice from item where itemid='" & Session("itemid") & "'"
cmd.Connection = conn
Dim myreader As SqlDataReader = cmd.ExecuteReader()
myreader.Read()
Dim itemname As String = myreader("itemname")
Dim listprice As String = myreader("listprice")
myreader.Close()
Dim s As String
cmd.CommandText = "select itemid from shopping where itemid='" & Session("itemid") & "' and userid='" & Session("userid") & "'"
cmd.Connection = conn
s = cmd.ExecuteScalar()
If s <> "" Then
Dim strscript As String = "<script language = javascript>alert('您已经购买了该商品,请更改商品数量')</script>"
RegisterClientScriptBlock("系统消息", strscript)
Else
cmd.CommandText = "insert into shopping values('" & Session("userid") & "','" & Session("itemid") & "','" & itemname & "'," & listprice & ",1)"
cmd.Connection = conn
cmd.ExecuteNonQuery()
End If
End If
Dim CommandText As String = "select itemid 商品号,itemname 商品名,untiprice 单价,qty 数量 from shopping where userid='" & Session("userid") & "'"
' myreader = cmd.ExecuteReader()
cmd.CommandText = CommandText
cmd.Connection = conn
Dim myreader1 As SqlDataReader = cmd.ExecuteReader()
Dim b As Boolean = myreader1.Read
myreader1.Close()
If b = True Then
Dim s1 As String = CommandText
Dim adp As New SqlClient.SqlDataAdapter(s1, conn)
'myreader.Close()
Dim ds As New DataSet
adp.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
Else
Dim strscript As String = "<script language = javascript>alert('您没有任何购物')</script>"
RegisterClientScriptBlock("系统消息", strscript)
End If
End If
conn.Close()
Label2.Text = "欢迎" & Session("username")
Label2.Visible = True
Button3.Visible = True
End Sub
Private Sub ImageButton1_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
Response.Redirect("search.aspx")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Redirect("search.aspx", True)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim cmd As New SqlCommand
conn.Open()
cmd.CommandText = "delete from shopping where userid='" & Session("userid") & "'"
cmd.Connection = conn
cmd.ExecuteNonQuery()
Session("userpower") = -1
Session("username") = ""
Label2.Visible = False
Button3.Visible = False
conn.Close()
Response.Redirect("webform1.aspx")
End Sub
Private Sub DataGrid1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid1.SelectedIndexChanged
End Sub
Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cmd As New SqlCommand
conn.Open()
conn1.Open()
cmd.CommandText = "select itemid,itemname, qty from shopping where userid='" & Session("userid") & "'"
cmd.Connection = conn
Dim reader As SqlDataReader = cmd.ExecuteReader()
Dim cmd1 As New SqlCommand
Dim a As Boolean = True
While reader.Read()
cmd1.CommandText = "select qty from item where itemid='" & reader("itemid") & "'"
cmd1.Connection = conn1
Dim b As Int16 = cmd1.ExecuteScalar()
If (b < reader("qty")) Then
Dim strscript As String = "<script language = javascript>alert('" & reader("itemname") & "库存不足,只有" & b & "')</script>"
RegisterClientScriptBlock("系统消息", strscript)
a = False
Exit While
End If
End While
If a = True Then
Response.Redirect("line.aspx", True)
End If
conn1.Close()
conn.Close()
End Sub
Private Sub DataGrid1_modify(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
End Sub
Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.CancelCommand
End Sub
Private Sub DataGrid1_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.DataBinding
End Sub
Private Sub DataGrid1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Disposed
End Sub
Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
If e.CommandName <> "Delete" Then
Session("id") = DataGrid1.DataKeys(e.Item.ItemIndex)
Dim cmd As New SqlCommand
conn.Open()
cmd.CommandText = "select itemname from item where itemid='" & Session("id") & "'"
cmd.Connection = conn
a = cmd.ExecuteScalar
Label3.Text = "请您输入新数量以修改" & a & "的购买数量"
Label3.Visible = True
Button4.Visible = True
TextBox1.Visible = True
conn.Close()
Else
conn.Open()
Dim id As String = DataGrid1.DataKeys(e.Item.ItemIndex)
Dim cmd As New SqlCommand
cmd.CommandText = "delete from shopping where itemid='" & id & "' and userid='" & Session("userid") & "'"
cmd.Connection = conn
cmd.ExecuteNonQuery()
Dim s1 As String = "select itemid 商品号,itemname 商品名,untiprice 单价,qty 数量 from shopping where userid='" & Session("userid") & "'"
Dim adp As New SqlClient.SqlDataAdapter(s1, conn)
Dim ds As New DataSet
adp.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
conn.Close()
End If
End Sub
Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim cmd As New SqlCommand
Dim strscript As String
conn.Open()
If TextBox1.Text = "" Then
strscript = "<script language = javascript>alert('数量不能为空')</script>"
RegisterClientScriptBlock("系统消息", strscript)
Else
cmd.CommandText = "update shopping set qty='" & TextBox1.Text & "' where itemid='" & Session("id") & "'"
cmd.Connection = conn
cmd.ExecuteNonQuery()
Label3.Visible = False
Button4.Visible = False
TextBox1.Visible = False
Dim s1 As String = "select itemid 商品号,itemname 商品名,untiprice 单价,qty 数量 from shopping where userid='" & Session("userid") & "'"
Dim adp As New SqlClient.SqlDataAdapter(s1, conn)
Dim ds As New DataSet
adp.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End If
conn.Close()
End Sub
End Class
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?