1

不知道为什么这不起作用。

我有一个带有一些文本框和下拉列表的简单表单。它显示员工的个人资料。用户应该能够手动编辑字段并单击保存。当他们点击保存时,我不断收到错误消息。

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

4

2 回答 2

2

Q1:确保表结构允许空值,并将参数值设置为 DBNull.Value。

Q2:

If IsNumeric(txtJobGrade.Text) Then

sqlCmdUpdate.Parameters("@JobGrade").Value = CInt(txtJobGrade.Text) 

Else

sqlCmdUpdate.Parameters("@JobGrade").Value = 0 'Or Default Value

End If

您始终可以将其设为下拉列表,以防止开放式数据输入。

于 2011-08-25T21:19:16.137 回答
1

看到你如何完成参数有点奇怪。通常,我希望看到更多类似这些方面的内容:

With sqlCmdUpdate.Parameters
   .clear()
   .addWithValue("@parm1", mytextbox1.text)
   .addWithValue("@parm2", mytextbox2.text)
End With

一方面,.add已弃用 - 仍然有效,但需要注意一些问题(http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx)。

其次,总是最好打电话.clear()

另外——您可能会考虑一种更标准的检查值的方法——例如:

If txtJobGrade.Text <> "" Then...

最好写成

If NOT string.isnullorempty(me.txtJobGrade.text) Then...

尝试进行一些更改,看看您仍然遇到什么(如果有的话)错误。

于 2011-08-31T17:41:11.847 回答