Imports System.Data
Imports System.Data.SqlClient
Public Class Product
Inherits System.Web.UI.Page
Dim Conn As SqlConnection
Dim da As SqlDataAdapter
Dim ds As New DataSet
Dim dt As DataTable
Dim strConn As String = "data source= 192.168.70.11;Persist Security Info=False;User ID=sqladmin;Initial Catalog=dbAllindex;"
Dim strKeyWord As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
strKeyWord = Me.txtKeyWord.Text
'BindProduct()
InitialCategory()
End Sub
Private Sub InitialCategory()
Dim sqlShow As String
Dim ds1 As New DataSet
Dim da1 As SqlDataAdapter
Dim dt1 As DataTable
sqlShow = "SELECT CateID,CateName FROM tbCategory where tbCategory.CateID not like 'C0000'"
da1 = New SqlDataAdapter(sqlShow, strConn)
da1.Fill(ds1, "tbCategory")
dt1 = ds1.Tables("tbCategory")
ListBox1.DataSource = dt1
ListBox1.DataTextField = "CateName"
ListBox1.DataValueField = "CateID"
ListBox1.DataBind()
End Sub
Private Sub BindProduct()
Dim objConn As SqlConnection
Dim strConnString As String
strConnString = "data source= 192.168.70.11;Persist Security Info=False;User ID=sqladmin;Initial Catalog=dbAllindex;"
objConn = New SqlConnection(strConnString)
objConn.Open()
Dim strSQL As String
'strSQL = "select * from tbProduct order by ProID"
strSQL = "SELECT * FROM tbProduct WHERE (ProName like '%" & strKeyWord & "%' OR ProID like '%" & strKeyWord & "%')"
strSQL += "Order by ProID"
da = New SqlDataAdapter(strSQL, strConn)
da.Fill(ds, "tbProduct")
dt = ds.Tables("tbProduct")
myGridView.DataSource = dt
myGridView.DataBind()
End Sub
Protected Sub myGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles myGridView.RowCommand
If e.CommandName = "Add2Cart" Then
Dim strProductID As String = e.CommandArgument
Dim dt As DataTable
Dim dr As DataRow
If IsNothing(Session("myCart")) Then
dt = New DataTable()
dt.Columns.Add("ProductID")
dt.Columns.Add("Qty")
Session("myCart") = dt
End If
dt = DirectCast(Session("myCart"), DataTable)
Dim foundRows() As DataRow
foundRows = dt.Select("ProductID = '" & strProductID & "'")
If foundRows.Length = 0 Then
dr = dt.NewRow()
dr("ProductID") = strProductID
dr("Qty") = "1"
dt.Rows.Add(dr)
Else
Dim updateRow() As DataRow
updateRow = dt.Select("ProductID = '" & strProductID & "'")
updateRow(0)("Qty") = updateRow(0)("Qty") + 1
End If
Session("myCart") = dt
Response.Redirect("ViewCart.aspx")
End If
End Sub
Protected Sub myGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles myGridView.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
''*** Picture ***'
'Dim imgPicture As Image = DirectCast(e.Row.FindControl("imgPicture"), Image)
'If Not IsNothing(imgPicture) Then
' imgPicture.ImageUrl = "img/" & e.Row.DataItem("Picture")
'End If
'*** ProductID ***'
Dim lblProductID As Label = DirectCast(e.Row.FindControl("lblProductID"), Label)
If Not IsNothing(lblProductID) Then
lblProductID.Text = e.Row.DataItem("ProID")
End If
'*** ProductName ***'
Dim lblProductName As Label = DirectCast(e.Row.FindControl("lblProductName"), Label)
If Not IsNothing(lblProductName) Then
lblProductName.Text = e.Row.DataItem("ProName")
End If
'*** Price ***'
Dim lblPrice As Label = DirectCast(e.Row.FindControl("lblPrice"), Label)
lblPrice.Text = DataBinder.Eval(e.Row.DataItem, "ProPrice").ToString()
'If (lblPrice.Text IsNot DBNull.Value) Then
' lblPrice.Text = DataBinder.Eval(e.Row.DataItem, "ProPrice")
' If (lblPrice.Text Is DBNull.Value) Then
' lblPrice.Text = (CType)"DataBinder.Eval(e.Row.DataItem, "ProPrice")
' Else
' lblPrice.Text = "NULL"
' End If
'*** Remark ***'
Dim lblRemark As Label = DirectCast(e.Row.FindControl("lblRemark"), Label)
lblRemark.Text = DataBinder.Eval(e.Row.DataItem, "Remark").ToString()
'*** Amount ***'
Dim lblAmount As Label = DirectCast(e.Row.FindControl("lblAmount"), Label)
If Not IsNothing(lblAmount) Then
lblAmount.Text = e.Row.DataItem("Amnt")
End If
'*** Balance ***'
Dim lblBalance As Label = DirectCast(e.Row.FindControl("lblBalance"), Label)
If Not IsNothing(lblBalance) Then
lblBalance.Text = e.Row.DataItem("Balance")
End If
'*** AddToCart ***'
Dim lnkAddToCart As LinkButton = DirectCast(e.Row.FindControl("lnkAddToCart"), LinkButton)
If Not IsNothing(lnkAddToCart) Then
lnkAddToCart.Text = "Add"
lnkAddToCart.CommandName = "Add2Cart"
lnkAddToCart.CommandArgument = e.Row.DataItem("ProID")
End If
End If
End Sub
Protected Sub myGridView_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles myGridView.PageIndexChanging
myGridView.PageIndex = e.NewPageIndex
BindProduct()
End Sub
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
BindProduct()
End Sub
Protected Sub BindCate()
Dim da2 As New SqlDataAdapter("SELECT * FROM tbProduct WHERE (tbProduct.CateID ='" & ListBox1.SelectedItem.Value & " ' ) Order by ProID", strConn)
Dim ds2 As New DataSet()
da2.Fill(ds2, "tbProduct")
myGridView.DataSource = ds2.Tables("tbProduct")
myGridView.DataBind()
End Sub
Protected Sub txtKeyWord_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtKeyWord.TextChanged
BindProduct()
End Sub
Protected Sub ShowData(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged
BindCate()
End Sub
End Class
Tag : ASP.NET Ms SQL Server 2008, VBScript, Web (ASP.NET), VB.NET, VS 2010 (.NET 4.x)