Server Error in '/' Application.
--------------------------------------------------------------------------------
Input string was not in a correct format.
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.FormatException: Input string was not in a correct format.
Source Error:
Line 122: Cmd.Parameters.Add("@Resou", SqlDbType.NVarChar).Value = Me.TxtResou.Text
Line 123: Cmd.Parameters.Add("@Qty", SqlDbType.Char).Value = Me.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 Resource"
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("@ResouID", SqlDbType.Int).Value = Me.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("@Resou", SqlDbType.NVarChar).Value = Me.TxtResou.Text
Cmd.Parameters.Add("@Qty", SqlDbType.Char).Value = Me.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("@ResouID", SqlDbType.Int).Value = Me.lblResouID.Text
Cmd.Parameters.Add("@Resou", SqlDbType.NVarChar).Value = Me.TxtResou.Text
Cmd.Parameters.Add("@Qty", SqlDbType.Char).Value = Me.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, VBScript, Reporting Service, Web (ASP.NET), VS 2010 (.NET 4.x)
Date :
2012-05-24 10:28:48
By :
neel
View :
4952
Reply :
2
No. 1
Guest
Code
Line 122: Cmd.Parameters.Add("@Resou", SqlDbType.NVarChar).Value = Me.TxtResou.Text
Line 123: Cmd.Parameters.Add("@Qty", SqlDbType.Char).Value = Me.TxtQty.Text
Line 124: Cmd.ExecuteNonQuery()
Line 125: objConn.Close()
Line 126: BindData()
จาก code ที่ให้มา ไม่แน่ใจนะครับว่าตรง Qty นี่ type เป็นแบบไหน เพราะที่ทำ ๆ มาถ้านึกถึง Qty น่าจะเป็น Int มากกว่า
พอดีอ้างอิง error ที่มันฟ้องด้วย : Input string was not in a correct format.
ลองค่อย ๆ เช็คดูครับ
นี่ code ผมนะครับดู ๆ แล้วเหมือนจะคล้าย ๆ กัน
Code
sqlUserName = "SELECT MemberID,MemberName,MemberLV FROM RRMember_V2 WHERE MemberName=@UserName AND MemberPWD=@Password"
objCmd = New SqlCommand(sqlUserName, objConn)
objCmd.Parameters.AddWithValue("@UserName", tbUsername.Text.Trim)
objCmd.Parameters.AddWithValue("@Password", tbPwd.Text.Trim)
ถ้าเก็บแบบ Int อาจจะต้อง convert ค่าก่อนด้วยมั้ง เป็นแบบนี้ CInt(TxtQty.Text)