Imports System.Data
Imports System.Data.SqlClient
Partial Class web_Default4
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
BindData()
End Sub
Protected Sub BindData()
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strConnString As String
strConnString = "Data Source=BENZNEW-PC;Initial Catalog=Project;Integrated Security=True"
objConn = New SqlConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "SELECT * FROM product "
Dim dtReader As SqlDataReader
objCmd = New SqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
myGridView.DataSource = dtReader
myGridView.DataBind()
dtReader.Close()
dtReader = Nothing
objConn.Close()
objConn = Nothing
End Sub
Protected Sub myGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles myGridView.RowCommand
Try
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("p_id")
dt.Columns.Add("qty")
' dt.Columns.Add("p_pic")
Session("myCart") = dt
End If
dt = DirectCast(Session("myCart"), DataTable)
Dim foundRows() As DataRow
foundRows = dt.Select("p_id = '" & strProductID & "'")
If foundRows.Length = 0 Then
dr = dt.NewRow()
dr("p_id") = strProductID
dr("qty") = 1
'dr("p_pic") = 1
dt.Rows.Add(dr)
Else
Dim updateRow() As DataRow
updateRow = dt.Select("p_id = '" & strProductID & "'")
updateRow(0)("qty") = updateRow(0)("qty") + 1
End If
Session("myCart") = dt
Response.Redirect("ViewCart.aspx")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
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("p_pic")
'End If
'*** ProductID ***'
Dim lblProductID As Label = DirectCast(e.Row.FindControl("lblProductID"), Label)
If Not IsNothing(lblProductID) Then
lblProductID.Text = e.Row.DataItem("p_id")
End If
'*** ProductName ***'
Dim lblProductName As Label = DirectCast(e.Row.FindControl("lblProductName"), Label)
If Not IsNothing(lblProductName) Then
lblProductName.Text = e.Row.DataItem("p_name")
End If
'*** Price ***'
Dim lblPrice As Label = DirectCast(e.Row.FindControl("lblPrice"), Label)
If Not IsNothing(lblPrice) Then
lblPrice.Text = e.Row.DataItem("p_price")
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("p_id")
End If
End If
End Sub
End Class
มันฟ้องตามรูปโดยบรรทัดที่ฟ้องคือ
Code (VB.NET)
Response.Redirect("ViewCart.aspx")
ครับ
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), VB.NET
Imports System.Data
Imports System.Data.SqlClient
Partial Public Class web_test
Inherits System.Web.UI.Page
Dim cons As String = Class1.Constr3
Dim DA As SqlDataAdapter
Dim DS As DataSet
Dim DT As DataTable
Dim DV As DataView
Dim com As SqlCommand
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim Sql As String = "SELECT * FROM product ORDER BY ProductID"
Dim DA As New SqlDataAdapter(Sql, Cons)
Dim DS As New DataSet
DA.Fill(DS)
GridView1.DataSource = DS
GridView1.DataBind()
End If
End Sub
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
' If e.CommandName = "Mylink" Then
'Label1.Text = "รหัสหมวดสินค้า :" & e.CommandArgument
'End If
Dim x() As String = Split(e.CommandArgument, ";")
If e.CommandName = "Mylink" Then
'Label1.Text = "รหัสหมวดสินค้า : " & x(0) & "</br>" & "ชื่อหมวดสินค้า : " & x(1)
End If
Dim sql As String = "Select ProductID,Productname,Price from product Where ProductID =" & x(0) & "Order by Productname"
Dim DA As New SqlDataAdapter(sql, cons)
Dim DS As New DataSet
Dim DR As DataRow
DA.Fill(DS)
'dr = DT.NewRow()
'DR("ProductID") = DS
'DR("ProductName") = DS
'DR("Price") = DS
' DR("ProductID") = 1
'DR = DT.NewRow()
'DT.Rows.Add(DR)
GridView2.DataSource = DS
GridView2.DataBind()
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim lnkAddToCart As LinkButton = DirectCast(e.Row.FindControl("เลือก"), LinkButton)
If Not IsNothing(lnkAddToCart) Then
lnkAddToCart.Text = "เลือก"
lnkAddToCart.CommandName = "Mylink"
lnkAddToCart.CommandArgument = e.Row.DataItem("ProductID")
End If
End Sub
End Class