使用参数化更新子获取错误

时间:2011-08-25 21:04:42

标签: tsql asp.net-4.0 vb.net-2010

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

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

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

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>

编辑2:

所以我已经放弃了这一点,而是将表格移动到FormView ItemTemplate中,同时使用EditTemplate。我修改了它,如下面的链接所述。 http://www.beansoftware.com/ASP.NET-Tutorials/FormView-Control.aspx

2 个答案:

答案 0 :(得分: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

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

答案 1 :(得分: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...

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