ติด error ว่า รบกวนหน่อยคะ Conversion failed when converting the nvarchar value 'Label' to data type int.
Code
Server Error in '/' Application.
--------------------------------------------------------------------------------
Conversion failed when converting the nvarchar value 'Label' 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 'Label' to data type int.
Source Error:
Line 122: Cmd.Parameters.Add(New SqlParameter("@Resou", TxtResou.Text))
Line 123: Cmd.Parameters.Add(New SqlParameter("@Qty", TxtQty.Text))
Line 124: Cmd.ExecuteNonQuery()
Line 125: objConn.Close()
Line 126: BindData()
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
โค้ดทั้งหมด
Code (VB.NET)
Imports System.Data
Imports System.Data.SqlClient
Public Class MResou
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
TxtResou.Text = ""
TxtQty.Text = ""
TxtResou.Focus()
End If
BRDel.Attributes.Add("onclick", "if(!window.confirm('Are you sure delete record??'))" & "return false;")
If Not IsPostBack Then
BindData()
End If
End Sub
Protected Sub BREdit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BREdit.Click
If lstResou.SelectedIndex < 0 Then
lblMessageR.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 ResouID,Resou,Qty " & " FROM dbo.Resource WHERE ResouID =" & lstResou.SelectedItem.Value
Dim Cmd As New SqlCommand(cmdSQL, objConn)
Dim RR As SqlDataReader
RR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
While RR.Read()
lblResouID.Text = RR("ResouID")
TxtResou.Text = RR("Resou")
TxtQty.Text = RR("Qty")
End While
TxtResou.Enabled = True
TxtQty.Enabled = True
BRUpdate.Visible = True
BRDel.Visible = True
lblMessageR.Text = ""
lblErrorResou.Visible = False
lblErrorQty.Visible = False
End Sub
Protected Sub BRDel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BRDel.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.Resource " & " WHERE ResouID=@ResouID"
Dim Cmd As New SqlCommand(cmdSQL, objConn)
Cmd.Parameters.Add(New SqlParameter("@ResouID", lblResouID.Text))
Cmd.ExecuteNonQuery()
objConn.Close()
BindData()
lstResou.Focus()
lblMessageR.Text = "Successfully Delete:" & TxtResou.Text & " " & TxtQty.Text
TxtResou.Enabled = False
TxtQty.Enabled = False
BRUpdate.Visible = False
BRDel.Visible = False
End Sub
Protected Sub BRAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BRAdd.Click
lblErrorResou.Visible = False
lblErrorQty.Visible = False
'Check Error input
If TxtResou.Text = "" Then
lblErrorResou.Visible = True
lblErrorR.Text = "Please Enter Name"
TxtResou.Focus()
Return
End If
If TxtQty.Text = "" Then
lblErrorQty.Visible = True
lblErrorR.Text = "Please Enter age"
TxtQty.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.Resource" & "(Resou,Qty)" & "VALUES(@Resou,@Qty)"
Dim Cmd As New SqlCommand(cmdSQL, objConn)
Cmd.Parameters.Add(New SqlParameter("@Resou", TxtResou.Text))
Cmd.Parameters.Add(New SqlParameter("@Qty", TxtQty.Text))
Cmd.ExecuteNonQuery()
objConn.Close()
lblErrorR.Text = "Successfully Insert:" & TxtResou.Text & " " & TxtQty.Text
TxtResou.Text = ""
TxtQty.Text = ""
TxtResou.Focus()
End Sub
Protected Sub BRUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BRUpdate.Click
'Check Error input
lblMessageR.Text = ""
lblErrorResou.Visible = False
lblErrorQty.Visible = False
If TxtResou.Text = "" Then
lblErrorResou.Visible = True
lblErrorR.Text = "Please Enter Name"
TxtResou.Focus()
Return
End If
If TxtQty.Text = "" Then
lblErrorQty.Visible = True
lblErrorR.Text = "Please Enter age"
TxtQty.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.Resource " & " SET Resou=@Resou,Qty=@Qty " & " WHERE ResouID=@ResouID"
Dim Cmd As New SqlCommand(cmdSQL, objConn)
Cmd.Parameters.Add(New SqlParameter("@ResouID", lblResouID.Text))
Cmd.Parameters.Add(New SqlParameter("@Resou", TxtResou.Text))
Cmd.Parameters.Add(New SqlParameter("@Qty", TxtQty.Text))
Cmd.ExecuteNonQuery()
objConn.Close()
BindData()
lstResou.SelectedIndex = lstResou.Items.IndexOf(lstResou.Items.FindByValue(lblResouID.Text))
lstResou.Focus()
lblMessageR.Text = "Successfully Update:" & TxtResou.Text & " " & TxtQty.Text
TxtResou.Enabled = False
TxtQty.Enabled = False
BRUpdate.Visible = False
BRDel.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 ResouID," & " Resou+''+Qty As ResourceData " & " FROM dbo.Resource "
Dim Cmd As New SqlCommand(cmdSQL, objConn)
Dim RR As SqlDataReader
RR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
lstResou.DataSource = RR
lstResou.DataValueField = "ResouID"
lstResou.DataTextField = "ResourceData"
lstResou.DataBind()
If lstResou.Rows > 0 Then
lstResou.SelectedIndex = 0
End If
lstResou.Focus()
objConn.Close()
End Sub
End Class
Tag : .NET, Ms SQL Server 2008, JavaScript, Reporting Service, Web (ASP.NET), VS 2010 (.NET 4.x)