不知道为什么这不起作用。
我有一个带有一些文本框和下拉列表的简单表单。它显示员工的个人资料。用户应该能够手动编辑字段并单击保存。当他们点击保存时,我不断收到错误消息。
Q1:如何处理为 SmallDateTime 数据类型插入 Null 值?
Q2:JobGrade 上的 TinyInt (SqlServer 2005) 有什么问题?
Option Explicit On
Imports System
Imports System.Data
Imports System.Data.SqlClient
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sqlJobsDB As New SqlConnection(ConfigurationManager.ConnectionStrings("JobsDB").ConnectionString)
Dim sqlCmdUpdate As SqlCommand = sqlJobsDB.CreateCommand()
Try
sqlJobsDB.Open()
sqlCmdUpdate.CommandText = _
"UPDATE tblEmployee " + _
"SET Firstname = @Firstname, LastName = @LastName, HiredLastName = @HiredLastName, " + _
"DateHired = @DateHired, Role = @Role, CADate = @CADate, CAType = @CAType, " + _
"JobDate = @JobDate, JobGrade = @JobGrade " + _
"WHERE EUID = '" & Session("sProfileEUID") & "';"
sqlCmdUpdate.Parameters.Add("@FirstName", SqlDbType.VarChar)
sqlCmdUpdate.Parameters.Add("@LastName", SqlDbType.VarChar)
sqlCmdUpdate.Parameters.Add("@HiredLastName", SqlDbType.VarChar)
sqlCmdUpdate.Parameters.Add("@DateHired", SqlDbType.SmallDateTime)
sqlCmdUpdate.Parameters.Add("@Role", SqlDbType.VarChar)
sqlCmdUpdate.Parameters.Add("@CADate", SqlDbType.SmallDateTime)
sqlCmdUpdate.Parameters.Add("@CAType", SqlDbType.VarChar)
sqlCmdUpdate.Parameters.Add("@JobDate", SqlDbType.SmallDateTime)
sqlCmdUpdate.Parameters.Add("@JobGrade", SqlDbType.TinyInt)
sqlCmdUpdate.Parameters("@FirstName").Value = txtFirstName.Text
sqlCmdUpdate.Parameters("@LastName").Value = txtLastName.Text
sqlCmdUpdate.Parameters("@HiredLastName").Value = txtHiredLastName.Text
sqlCmdUpdate.Parameters("@DateHired").Value = txtDateHired.Text
sqlCmdUpdate.Parameters("@Role").Value = ddlRole.SelectedValue.ToString
If txtCADate.Text = "" Then
sqlCmdUpdate.Parameters("@CADate").Value = 0
Else
sqlCmdUpdate.Parameters("@CADate").Value = txtCADate.Text
End If
sqlCmdUpdate.Parameters("@CAType").Value = ddlCAType.SelectedValue
If txtJobDate.Text = "" Then
sqlCmdUpdate.Parameters("@JobDate").Value = 0
Else
sqlCmdUpdate.Parameters("@JobDate").Value = txtJobDate.Text
End If
sqlCmdUpdate.Parameters("@JobGrade").Value = CByte(txtJobGrade.Text)
sqlCmdUpdate.ExecuteNonQuery()
Catch ex As Exception
'Debugging
lblErrMsg.Text = ex.ToString
lblErrMsg.Visible = True
Finally
sqlJobsDB.Close()
End Try
End Sub</code>
I open the form and fill it out correctly.
I'll enter something like "4" (no quotes) for JobGrade. It still says "conversion from strink ''" like its not even seeing when I input items on the form.
Errors are below:
System.InvalidCastException: Conversion from string "" to type 'Byte' is not valid. ---> System.FormatException: Input string was not in a correct format. at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.CompilerServices.Conversions.ToByte(String Value) --- End of inner exception stack trace --- at Microsoft.VisualBasic.CompilerServices.Conversions.ToByte(String Value) at Profile.btnSave_Click(Object sender, EventArgs e) in
Update
The DBNull.Value issue is resolved.
The JobGrade, and Role are still issues. When throwing up some breakpoints on it doens't fetch the contents of the textbox or the dropdown list.
** Updated Code **
Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Session("sProfileEUID") = Nothing
Response.Redirect("~/Management/EditUsers.aspx")
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sqlJobsDB As New SqlConnection(ConfigurationManager.ConnectionStrings("JobsDB").ConnectionString)
Dim sqlCmdUpdate As SqlCommand = sqlJobsDB.CreateCommand()
Try
sqlJobsDB.Open()
sqlCmdUpdate.CommandText = _
"UPDATE tblEmployee " + _
"SET FirstName = @FirstName, LastName = @LastName, HiredLastName = @HiredLastName, " + _
"DateHired = @DateHired, Role = @Role, CADate = @CADate, CAType = @CAType, " + _
"JobDate = @JobDate, JobGrade = @JobGrade " + _
"WHERE EUID = '" & Session("sProfileEUID") & "';"
sqlCmdUpdate.Parameters.Add("@FirstName", SqlDbType.VarChar)
sqlCmdUpdate.Parameters.Add("@LastName", SqlDbType.VarChar)
sqlCmdUpdate.Parameters.Add("@HiredLastName", SqlDbType.VarChar)
sqlCmdUpdate.Parameters.Add("@DateHired", SqlDbType.SmallDateTime)
sqlCmdUpdate.Parameters.Add("@Role", SqlDbType.VarChar)
sqlCmdUpdate.Parameters.Add("@CADate", SqlDbType.SmallDateTime)
sqlCmdUpdate.Parameters.Add("@CAType", SqlDbType.VarChar)
sqlCmdUpdate.Parameters.Add("@JobDate", SqlDbType.SmallDateTime)
sqlCmdUpdate.Parameters.Add("@JobGrade", SqlDbType.TinyInt)
sqlCmdUpdate.Parameters("@FirstName").Value = txtFirstName.Text
sqlCmdUpdate.Parameters("@LastName").Value = txtLastName.Text
sqlCmdUpdate.Parameters("@HiredLastName").Value = txtHiredLastName.Text
sqlCmdUpdate.Parameters("@DateHired").Value = txtDateHired.Text
sqlCmdUpdate.Parameters("@Role").Value = ddlRole.SelectedValue.ToString
If txtCADate.Text <> "" Then sqlCmdUpdate.Parameters("@CADate").Value = CDate(txtCADate.Text)
If txtCADate.Text = "" Then sqlCmdUpdate.Parameters("@CADate").Value = DBNull.Value
If ddlCAType.Text <> "" Then sqlCmdUpdate.Parameters("@CAType").Value = ddlCAType.SelectedValue
If ddlCAType.Text = "" Then sqlCmdUpdate.Parameters("@CAType").Value = DBNull.Value
If txtJobDate.Text <> "" Then sqlCmdUpdate.Parameters("@JobDate").Value = CDate(txtJobDate.Text)
If txtJobDate.Text = "" Then sqlCmdUpdate.Parameters("@JobDate").Value = DBNull.Value
If txtJobGrade.Text <> "" Then sqlCmdUpdate.Parameters("@JobGrade").Value = CInt(txtJobGrade.Text)
If txtJobGrade.Text = "" Then sqlCmdUpdate.Parameters("@JobGrade").Value = DBNull.Value
sqlCmdUpdate.ExecuteNonQuery()
Catch ex As Exception
lblErrMsg.Text = ex.ToString
lblErrMsg.Visible = True
Finally
sqlJobsDB.Close()
End Try
End Sub
编辑2:
所以我几乎放弃了这一点,而是将表格移动到一个 FormView ItemTemplate 中,也带有一个 EditTemplate。我按照以下链接中的说明对其进行了修改。 http://www.beansoftware.com/ASP.NET-Tutorials/FormView-Control.aspx