ติด error บรรทัดนี้คะ Line 33: Dim Cmd As New SqlCommand(cmdSQL, objConn) รบกวนหน่อยนะคะ
โค้ดทั้งหมดนะคะ
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 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 SS As SqlDataReader
SS = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
While SS.Read()
lblStaffID.Text = SS("StaffID")
TxtSurname.Text = SS("StaffName")
Txtage.Text = SS("StaffAge")
TxtP.Text = SS("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
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
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 SS As SqlDataReader
SS = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
lstStaff.DataSource = SS
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
และ error ว่า
Server Error in '/' Application.
--------------------------------------------------------------------------------
Incorrect syntax near '.'.
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 '.'.
Source Error:
Line 33: Dim Cmd As New SqlCommand(cmdSQL, objConn)
Line 34: Dim SS As SqlDataReader
Line 35: SS = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
Line 36: While SS.Read()
Line 37: lblStaffID.Text = SS("StaffID")
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
รบกวนหน่อยนะคะ
Tag : .NET, Ms SQL Server 2008, JavaScript, Reporting Service, Web (ASP.NET), VS 2010 (.NET 4.x)