Imports System.Data
Imports System.Data.SqlClient
Public Class Home
Inherits System.Web.UI.Page
Dim objConn As SqlConnection
Dim strConnString, strSQL As String
Dim objCmd As SqlCommand
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
strConnString = "Server=localhost;UID=sa;PASSWORD=1234;database=mydata;Max Pool Size=400;Connect Timeout=600;"
objConn = New SqlConnection
objConn.ConnectionString = strConnString
objConn.Open()
If Not Page.IsPostBack() Then
BindData()
End If
End Sub
Sub BindData()
Dim strSQL As String
strSQL = "SELECT * FROM Detail"
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 Detail WHERE ComputerName = '" & myGridView.DataKeys.Item(e.RowIndex).Value & "'"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
myGridView.EditIndex = -1
BindData()
End Sub
Protected Sub myGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles myGridView.RowCommand
If e.CommandName = "Add" Then
'*** Computer name ***'
Dim txtComputerName As TextBox = CType(myGridView.FooterRow.FindControl("txtAddComputerName"), TextBox)
'*** User Name ***'
Dim txtUserName As TextBox = CType(myGridView.FooterRow.FindControl("txtAddUserName"), TextBox)
'*** IP Address ***'
Dim txtIPAddress As TextBox = CType(myGridView.FooterRow.FindControl("txtAddIPAddress"), TextBox)
'*** Domain name ***'
Dim txtDomainName As TextBox = CType(myGridView.FooterRow.FindControl("txtAddDomainName"), TextBox)
'*** Section ***'
Dim txtSection As TextBox = CType(myGridView.FooterRow.FindControl("txtAddSection"), TextBox)
'*** Telephone Number ***'
Dim txtTelephone As TextBox = CType(myGridView.FooterRow.FindControl("txtAddTelephone"), TextBox)
'*** Com Purchase date ***'
Dim txtComputerPurchaseDate As TextBox = CType(myGridView.FooterRow.FindControl("txtAddComputerPurchaseDate"), TextBox)
'*** Com purchase expire ***'
Dim txtComputerPurchaseExpire As TextBox = CType(myGridView.FooterRow.FindControl("txtAddComputerPurchaseExpire"), TextBox)
'*** ComputerBand ***'
Dim txtComputerBand As TextBox = CType(myGridView.FooterRow.FindControl("txtAddComputerBand"), TextBox)
'*** ComputerModel ***'
Dim txtComputerModel As TextBox = CType(myGridView.FooterRow.FindControl("txtAddComputerModel"), TextBox)
'*** Com Serial ***'
Dim txtComputerSerialNo As TextBox = CType(myGridView.FooterRow.FindControl("txtAddComputerSerialNo"), TextBox)
'*** Spec Computer ***'
Dim txtSpecComputer As TextBox = CType(myGridView.FooterRow.FindControl("txtAddSpecComputer"), TextBox)
'*** OS ***'
Dim txtOS As TextBox = CType(myGridView.FooterRow.FindControl("txtAddOS"), TextBox)
'*** License Key ***'
Dim txtLicenseKey As TextBox = CType(myGridView.FooterRow.FindControl("txtAddLicenseKey"), TextBox)
'*** SpecialApp ***'
Dim txtSpecialApp As TextBox = CType(myGridView.FooterRow.FindControl("txtAddSpecialApp"), TextBox)
'*** MonitorBand ***'
Dim txtMonitorBand As TextBox = CType(myGridView.FooterRow.FindControl("txtAddMonitorBand"), TextBox)
'*** MonitorModel ***'
Dim txtMonitorModel As TextBox = CType(myGridView.FooterRow.FindControl("txtAddMonitorModel"), TextBox)
'*** MonitorSerialNo ***'
Dim txtMonitorSerialNo As TextBox = CType(myGridView.FooterRow.FindControl("txtAddMonitorSerialNo"), TextBox)
'*** SpecMonitor ***'
Dim txtSpecMonitor As TextBox = CType(myGridView.FooterRow.FindControl("txtAddSpecMonitor"), TextBox)
'*** Monitor Purchase date ***'
Dim txtMonitorPurchaseDate As TextBox = CType(myGridView.FooterRow.FindControl("txtAddMonitorPurchaseDate"), TextBox)
'*** Monitor purchase expire ***'
Dim txtMonitorPurchaseExpire As TextBox = CType(myGridView.FooterRow.FindControl("txtAddMonitorPurchaseExpire"), TextBox)
'*** Note Remark ***'
Dim txtNoteRemark As TextBox = CType(myGridView.FooterRow.FindControl("txtAddNoteRemark"), TextBox)
strSQL = "INSERT INTO Detail (ComputerName,UserName,IPAddress,DomainName,Section,Telephone, ComputerPurchaseDate,ComputerPurchaseExpire,ComputerBand, ComputerModel,ComputerSerialNo,SpecComputer,OS,LicenseKey, SpecialApp,MonitorBand,MonitorModel,MonitorSerialNo,SpecMonitor, MonitorPurchaseDate,MonitorPurchaseExpire,NoteRemark) " & _
" VALUES ('" & txtComputerName.Text & "','" & txtUserName.Text & "' ,'" & txtIPAddress.Text & "','" & txtDomainName.Text & "','" & txtSection.Text & "','" & txtTelephone.Text & "', '" & txtComputerPurchaseDate.Text & "','" & txtComputerPurchaseExpire.Text & "','" & txtComputerBand.Text & "' ,'" & txtComputerModel.Text & "','" & txtComputerSerialNo.Text & "','" & txtSpecComputer.Text & "', '" & txtOS.Text & "','" & txtLicenseKey.Text & "','" & txtSpecialApp.Text & "','" & txtMonitorBand.Text & "' ,'" & txtMonitorModel.Text & "','" & txtMonitorSerialNo.Text & "','" & txtSpecMonitor.Text & "','" & txtMonitorPurchaseDate.Text & "' ,'" & txtMonitorPurchaseExpire.Text & "','" & txtNoteRemark.Text & "',) "
objCmd = New SqlCommand(strSQL, objConn)
objConn.Close()
objConn = Nothing
BindData()
End If
End Sub
มันขึ้น error แบบนี้ครับ
Server Error in '/' Application.
ExecuteReader: Connection property has not been initialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: ExecuteReader: Connection property has not been initialized.
Source Error:
Line 28: Dim dtReader As SqlDataReader
Line 29: objCmd = New SqlCommand(strSQL, objConn) Line 30: dtReader = objCmd.ExecuteReader()
Line 31:
Line 32: '*** BindData to GridView ***'