Sub BindData()
Dim strConn As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Rescuedb.mdf;Integrated Security=True;User Instance=True"
Dim objConn As New SqlConnection(strConn)
objConn.Open()
Dim cmdSQL As String = "SELECT StaffID+''+StaffName+''+StaffAge+''+StaffP As StaffData" & "FROM dbo.Staff "
Dim Cmd As New SqlCommand(cmdSQL, objConn)
' Dim objDR As SqlDataReader
'objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
' lstStaff.DataSource = objDR
lstStaff.DataValueField = "StaffID"
lstStaff.DataTextField = "StaffData"
lstStaff.DataBind()
If lstStaff.Rows > 0 Then
lstStaff.SelectedIndex = 0
End If
lstStaff.Focus()
objConn.Close()
End Sub
ด้านล่างนี้เป็นโค้ด แบบเต็ม ในไฟล์ MStaff.aspx.vb
Code (VB.NET)
Imports System.Data
Imports System.Data.SqlClient
Public Class MStaff
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
TxtSurname.Text = ""
Txtage.Text = ""
TxtP.Text = ""
TxtSurname.Focus()
End If
BSDel.Attributes.Add("onclick", "if(!window.confirm('Are you sure delete record??'))" & "return false;")
If Not IsPostBack Then
BindData()
End If
End Sub
Protected Sub BSDel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BSDel.Click
'Delete Data
Dim strConn As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Rescuedb.mdf;Integrated Security=True;User Instance=True"
Dim objConn As New SqlConnection(strConn)
objConn.Open()
Dim cmdSQL As String = "DELETE FROM dbo.Staff" & "WHERE StaffID=@StaffID"
Dim Cmd As New SqlCommand(cmdSQL, objConn)
Cmd.Parameters.Add(New SqlParameter("@StaffID", lblStaffID.Text))
Cmd.ExecuteNonQuery()
objConn.Close()
BindData()
lstStaff.Focus()
lblMessage.Text = "Successfully Delete:" & TxtSurname.Text & " " & Txtage.Text & " " & TxtP.Text
TxtSurname.Enabled = False
Txtage.Enabled = False
TxtP.Enabled = False
BSUpdate.Visible = False
BSDel.Visible = False
End Sub
Protected Sub BSAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BSAdd.Click
lblErrorSurname.Visible = False
lblErrorage.Visible = False
lblErrorP.Visible = False
'Check Error input
If TxtSurname.Text = "" Then
lblErrorSurname.Visible = True
lblError.Text = "Please Enter Name"
TxtSurname.Focus()
Return
End If
If Txtage.Text = "" Then
lblErrorage.Visible = True
lblError.Text = "Please Enter age"
Txtage.Focus()
Return
End If
If TxtP.Text = "" Then
lblErrorP.Visible = True
lblError.Text = "Please Enter P"
TxtP.Focus()
Return
End If
'Insert Data into SQL Server 2008
Dim strConn As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Rescuedb.mdf;Integrated Security=True;User Instance=True"
Dim objConn As New SqlConnection(strConn)
objConn.Open()
Dim cmdSQL As String = "INSERT INTO dbo.Staff" & "(StaffName,StaffAge,StaffP)" & "VALUES(@StaffName,@StaffAge,@StaffP)"
Dim Cmd As New SqlCommand(cmdSQL, objConn)
Cmd.Parameters.Add(New SqlParameter("@StaffName", TxtSurname.Text))
Cmd.Parameters.Add(New SqlParameter("@StaffAge", Txtage.Text))
Cmd.Parameters.Add(New SqlParameter("@StaffP", TxtP.Text))
Cmd.ExecuteNonQuery()
objConn.Close()
lblError.Text = "Successfully Insert:" & TxtSurname.Text & " " & Txtage.Text & " " & TxtP.Text
TxtSurname.Text = ""
Txtage.Text = ""
TxtP.Text = ""
TxtSurname.Focus()
End Sub
Protected Sub BSUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BSUpdate.Click
'Check Error input
lblMessage.Text = ""
lblErrorSurname.Visible = False
lblErrorage.Visible = False
lblErrorP.Visible = False
If TxtSurname.Text = "" Then
lblErrorSurname.Visible = True
lblError.Text = "Please Enter Name"
TxtSurname.Focus()
Return
End If
If Txtage.Text = "" Then
lblErrorage.Visible = True
lblError.Text = "Please Enter age"
Txtage.Focus()
Return
End If
If TxtP.Text = "" Then
lblErrorP.Visible = True
lblError.Text = "Please Enter P"
TxtP.Focus()
Return
End If
'Update Data
Dim strConn As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Rescuedb.mdf;Integrated Security=True;User Instance=True"
Dim objConn As New SqlConnection(strConn)
objConn.Open()
Dim cmdSQL As String = "UPDATE dbo.Staff" & "SET StaffName=@StaffName,StaffAge=@StaffAge,StaffP=@StaffP " & "WHERE StaffID=@StaffID"
Dim Cmd As New SqlCommand(cmdSQL, objConn)
Cmd.Parameters.Add(New SqlParameter("@StaffID", lblStaffID.Text))
Cmd.Parameters.Add(New SqlParameter("@StaffName", TxtSurname.Text))
Cmd.Parameters.Add(New SqlParameter("@StaffAge", Txtage.Text))
Cmd.Parameters.Add(New SqlParameter("@StaffP", TxtP.Text))
Cmd.ExecuteNonQuery()
objConn.Close()
BindData()
lstStaff.SelectedIndex = lstStaff.Items.IndexOf(lstStaff.Items.FindByValue(lblStaffID.Text))
lstStaff.Focus()
lblMessage.Text = "Successfully Update:" & TxtSurname.Text & " " & Txtage.Text & " " & TxtP.Text
TxtSurname.Enabled = False
Txtage.Enabled = False
TxtP.Enabled = False
BSUpdate.Visible = False
BSDel.Visible = False
End Sub
Protected Sub BSEdit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BSEdit.Click
If lstStaff.SelectedIndex < 0 Then
lblMessage.Text = "Please Select Staff"
Return
End If
Dim strConn As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Rescuedb.mdf;Integrated Security=True;User Instance=True"
Dim objConn As New SqlConnection(strConn)
objConn.Open()
Dim cmdSQL As String = "SELECT StaffID,StaffName,StaffAge,StaffP" & "FROM dbo.Staff Where StaffID =" & lstStaff.SelectedItem.Value
Dim Cmd As New SqlCommand(cmdSQL, objConn)
Dim objDR As SqlDataReader
objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
While objDR.Read()
lblStaffID.Text = objDR("StaffID")
TxtSurname.Text = objDR("StaffName")
Txtage.Text = objDR("StaffAge")
TxtP.Text = objDR("StaffP")
End While
TxtSurname.Enabled = True
Txtage.Enabled = True
TxtP.Enabled = True
BSUpdate.Visible = True
BSDel.Visible = True
lblMessage.Text = ""
lblErrorSurname.Visible = False
lblErrorage.Visible = False
lblErrorP.Visible = False
End Sub
Sub BindData()
Dim strConn As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Rescuedb.mdf;Integrated Security=True;User Instance=True"
Dim objConn As New SqlConnection(strConn)
objConn.Open()
Dim cmdSQL As String = "SELECT StaffID+''+StaffName+''+StaffAge+''+StaffP As StaffData" & "FROM dbo.Staff "
Dim Cmd As New SqlCommand(cmdSQL, objConn)
' Dim objDR As SqlDataReader
'objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
' lstStaff.DataSource = objDR
lstStaff.DataValueField = "StaffID"
lstStaff.DataTextField = "StaffData"
lstStaff.DataBind()
If lstStaff.Rows > 0 Then
lstStaff.SelectedIndex = 0
End If
lstStaff.Focus()
objConn.Close()
End Sub
End Class
Tag : .NET, Ms SQL Server 2008, VBScript, Reporting Service, Web (ASP.NET), VS 2010 (.NET 4.x)
Server Error in '/' Application.
--------------------------------------------------------------------------------
Incorrect syntax near 'dbo'.
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.Data.SqlClient.SqlException: Incorrect syntax near 'dbo'.
Source Error:
Line 181: Dim Cmd As New SqlCommand(cmdSQL, objConn)
Line 182: Dim SS As SqlDataReader
Line 183: SS = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
Line 184: lstStaff.DataSource = SS
Line 185: lstStaff.DataValueField = "StaffID"
Server Error in '/' Application.
--------------------------------------------------------------------------------
Conversion failed when converting the nvarchar value 'อัจฉราพร ชูสิทธิ์' to data type int.
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.Data.SqlClient.SqlException: Conversion failed when converting the nvarchar value 'อัจฉราพร ชูสิทธิ์' to data type int.
Source Error:
Line 185: lstStaff.DataValueField = "StaffID"
Line 186: lstStaff.DataTextField = "StaffData"
Line 187: lstStaff.DataBind()
Line 188: If lstStaff.Rows > 0 Then
Line 189: lstStaff.SelectedIndex = 0
[SqlException (0x80131904): Conversion failed when converting the nvarchar value 'อัจฉราพร ชูสิทธิ์' to data type int.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2030802
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5009584
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275
System.Data.SqlClient.SqlDataReader.HasMoreRows() +211
System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout) +217
System.Data.SqlClient.SqlDataReader.Read() +9
System.Data.Common.DbEnumerator.MoveNext() +34
System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) +278
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +109
System.Web.UI.WebControls.ListControl.PerformSelect() +34
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
Rescue.MStaff.BindData() in C:\Users\Administrator\Desktop\RescuesPlan\Rescue\Rescue\MStaff.aspx.vb:187
Rescue.MStaff.Page_Load(Object sender, EventArgs e) in C:\Users\Administrator\Desktop\RescuesPlan\Rescue\Rescue\MStaff.aspx.vb:19
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1