Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strSQL As String
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim strConnString As String
strConnString = "Server=localhost\SQLExpress;Database=Myweb;Trusted_Connection=True;"
objConn = New SqlConnection(strConnString)
objConn.Open()
If Not Page.IsPostBack() Then
BindData()
End If
End Sub
Sub BindData()
strSQL = "SELECT * FROM ACCESSORIES"
Dim dtReader As SqlDataReader
objCmd = New SqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
'*** BindData to GridView ***'
myGridView.DataSource = dtReader
myGridView.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
Sub modEditCommand(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
myGridView.EditIndex = e.NewEditIndex
myGridView.ShowFooter = False
BindData()
End Sub
Sub modCancelCommand(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
myGridView.EditIndex = -1
myGridView.ShowFooter = True
BindData()
End Sub
Sub modDeleteCommand(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
strSQL = "DELETE FROM ACCESSORIES WHERE PartID = '" & myGridView.DataKeys.Item(e.RowIndex).Value & "'"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
myGridView.EditIndex = -1
BindData()
End Sub
Sub myGridView_RowCommand(ByVal source As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "Add" Then
'*** PartID ***'
Dim txtPartID As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartID"), TextBox)
'*** PartNo ***'
Dim txtPartNo As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartNo"), TextBox)
'*** PartSort ***'
Dim ddlAddPartSort As DropDownList = CType(myGridView.FooterRow.FindControl("ddlAddPartSort"), DropDownList)
'*** PartName ***'
Dim txtPartName As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartName"), TextBox)
'*** PartModel ***'
Dim txtPartModel As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartModel"), TextBox)
'*** CustomerPrice ***'
Dim txtCustomerPrice As TextBox = CType(myGridView.FooterRow.FindControl("txtAddCustomerPrice"), TextBox)
'*** PartSub ***'
Dim txtPartSub As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartSub"), TextBox)
strSQL = "INSERT INTO ACCESSORIES (PartNo,PartSort,PartName,PartModel,CustomerPrice,PartSub) " & _
" VALUES ('" & txtPartNo.Text & "','" & ddlAddPartSort.SelectedItem.Value & "','" & txtPartName.Text & "' " & _
" ,'" & txtPartModel.Text & "','" & txtCustomerPrice.Text & "','" & txtPartSub.Text & "') "
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
BindData()
End If
End Sub
Sub modUpdateCommand(ByVal s As Object, ByVal e As GridViewUpdateEventArgs)
'*** PartID ***'
Dim txtPartID As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartID"), TextBox)
'*** PartNo ***'
Dim txtPartNo As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartNo"), TextBox)
'*** PartSort ***'
Dim ddlAddPartSort As DropDownList = CType(myGridView.Rows(e.RowIndex).FindControl("ddlAddPartSort"), DropDownList)
'*** PartName ***'
Dim txtPartName As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartName"), TextBox)
'*** PartModel ***'
Dim txtPartModel As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartModel"), TextBox)
'*** CustomerPrice ***'
Dim txtCustomerPrice As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditCustomerPrice"), TextBox)
'*** PartSub ***'
Dim txtPartSub As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartSub"), TextBox)
strSQL = "UPDATE ACCESSORIES SET PartID = '" & txtPartID.Text & "' " & _
" ,PartNo = '" & txtPartNo.Text & "' " & _
" ,PartSort = '" & ddlAddPartSort.SelectedItem.Value & "' " & _
" ,PartName = '" & txtPartName.Text & "' " & _
" ,PartModel = '" & txtPartModel.Text & "' " & _
" ,CustomerPrice = '" & txtCustomerPrice.Text & "' " & _
" ,PartSub = '" & txtPartSub.Text & "' " & _
" WHERE PartID = '" & myGridView.DataKeys.Item(e.RowIndex).Value & "'"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
myGridView.EditIndex = -1
myGridView.ShowFooter = True
BindData()
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
BindData()
End Sub
Tag : .NET, VBScript, Web (ASP.NET), VB.NET, VS 2005 (.NET 2.x)
Dim strKeyWord As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Session("UserAuthentication") = "" Then
Response.Redirect("Login1.aspx")
End If
End Sub
Private Sub BindData()
Dim objConn As New SqlConnection
Dim objCmd As New SqlCommand
Dim dtAdapter As New SqlDataAdapter
Dim ds As New DataSet
Dim strConnString, strSQL As String
strConnString = "Server=localhost\SQLExpress;Database=Myweb;Trusted_Connection=True;"
strSQL = "SELECT * FROM ACCESSORIES"
objConn.ConnectionString = strConnString
objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
End Sub
Sub modEditCommand(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
myGridView.EditIndex = e.NewEditIndex
myGridView.ShowFooter = False
BindData()
End Sub
Sub modCancelCommand(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
myGridView.EditIndex = -1
myGridView.ShowFooter = True
BindData()
End Sub
Sub modDeleteCommand(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
strSQL = "DELETE FROM ACCESSORIES WHERE PartID = '" & myGridView.DataKeys.Item(e.RowIndex).Value & "'"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
myGridView.EditIndex = -1
BindData()
End Sub
Sub myGridView_RowCommand(ByVal source As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "Add" Then
'*** PartID ***'
Dim txtPartID As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartID"), TextBox)
'*** PartNo ***'
Dim txtPartNo As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartNo"), TextBox)
'*** PartSort ***'
Dim ddlAddPartSort As DropDownList = CType(myGridView.FooterRow.FindControl("ddlAddPartSort"), DropDownList)
'*** PartName ***'
Dim txtPartName As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartName"), TextBox)
'*** PartModel ***'
Dim txtPartModel As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartModel"), TextBox)
'*** CustomerPrice ***'
Dim txtCustomerPrice As TextBox = CType(myGridView.FooterRow.FindControl("txtAddCustomerPrice"), TextBox)
'*** PartSub ***'
Dim txtPartSub As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartSub"), TextBox)
strSQL = "INSERT INTO ACCESSORIES (PartNo,PartSort,PartName,PartModel,CustomerPrice,PartSub) " & _
" VALUES ('" & txtPartNo.Text & "','" & ddlAddPartSort.SelectedItem.Value & "','" & txtPartName.Text & "' " & _
" ,'" & txtPartModel.Text & "','" & txtCustomerPrice.Text & "','" & txtPartSub.Text & "') "
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
BindData()
End If
End Sub
Sub modUpdateCommand(ByVal s As Object, ByVal e As GridViewUpdateEventArgs)
'*** PartID ***'
Dim txtPartID As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartID"), TextBox)
'*** PartNo ***'
Dim txtPartNo As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartNo"), TextBox)
'*** PartSort ***'
Dim ddlAddPartSort As DropDownList = CType(myGridView.Rows(e.RowIndex).FindControl("ddlAddPartSort"), DropDownList)
'*** PartName ***'
Dim txtPartName As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartName"), TextBox)
'*** PartModel ***'
Dim txtPartModel As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartModel"), TextBox)
'*** CustomerPrice ***'
Dim txtCustomerPrice As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditCustomerPrice"), TextBox)
'*** PartSub ***'
Dim txtPartSub As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartSub"), TextBox)
strSQL = "UPDATE ACCESSORIES SET PartID = '" & txtPartID.Text & "' " & _
" ,PartNo = '" & txtPartNo.Text & "' " & _
" ,PartSort = '" & ddlAddPartSort.SelectedItem.Value & "' " & _
" ,PartName = '" & txtPartName.Text & "' " & _
" ,PartModel = '" & txtPartModel.Text & "' " & _
" ,CustomerPrice = '" & txtCustomerPrice.Text & "' " & _
" ,PartSub = '" & txtPartSub.Text & "' " & _
" WHERE PartID = '" & myGridView.DataKeys.Item(e.RowIndex).Value & "'"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
myGridView.EditIndex = -1
myGridView.ShowFooter = True
BindData()
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
BindData()
End Sub
</script>
เป็น Compilation Error อ่าคะ