将控制值转换为SQL参数时出现InvalidCastException

时间:2012-07-19 01:16:39

标签: sql vb.net tostring datetimepicker

nudAge.Value数字向上。在我的sql数据库中,它是Age(int, null)。 我做了,

      insert.Parameters.AddWithValue("@age", nudAge.Value.ToString())

我不确定是否应该使用.ToString()

date_of_confinement是我的数据库中的日期时间。格式应该是这样吗?

      insert.Parameters.AddWithValue("@date_of_confinement", dtpDate.Value.ToShortDateString)

另外,对于number_of_bottles,它是vb.net中的文本和Int中的db,我使用了Int32.Parse但不确定,

      insert.Parameters.AddWithValue("@number_of_bottles", Int32.Parse(txtBottle.Text))

但我仍然收到错误'System.InvalidCastException'。

这是我的代码:

      PatientInfoConnection.Open() 'open database connection

      Dim sql As String = ""
      sql = "insert into PatientInfo (name, age, date_of_confinement,type_of_sickness, type_of_IVfluid, number_of_bottles, drop_rate)" & _
      " values (@name, @age, @date_of_confinement, @type_of_sickness, @type_of_IV_fluid, @number_of_bottles, @drop_rate)"


      Dim insert As New SqlCommand(sql, PatientInfoConnection)
      insert.Parameters.AddWithValue("@name", txtName.Text)
      insert.Parameters.AddWithValue("@age", Int32.Parse(txtAge.Text))
      insert.Parameters.AddWithValue("@date_of_confinement", Date.Parse(dtpDate.Value))
      insert.Parameters.AddWithValue("@type_of_sickness", txtSickness.Text)
      insert.Parameters.AddWithValue("@type_of_IV_fluid", txtFluid.Text)
      insert.Parameters.AddWithValue("@number_of_bottles", Int32.Parse(txtBottle.Text))
      insert.Parameters.AddWithValue("@drop_rate", Int32.Parse(txtDrop.Text))

      insert.ExecuteNonQuery()

这是我的新代码:(我修改了一些。我没有得到任何错误,但是当我想点击保存按钮保存时,它没有做任何事情。我的数据库连接有问题吗?谢谢!)

      Private Sub PatientInfoBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PatientInfoBindingNavigatorSaveItem.Click
    Me.Validate()
    Me.PatientInfoBindingSource.EndEdit()
    Me.PatientInfoTableAdapter.Update(Me.PatientInfoDBDataSet.PatientInfo)

End Sub

Private Sub EditInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'PatientInfoDBDataSet.PatientInfo' table. You can move, or remove it, as needed.
    Me.PatientInfoTableAdapter.Fill(Me.PatientInfoDBDataSet.PatientInfo)

End Sub
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
    Try

        'To check if all values have been filled up
        If txtName.Text = "" Or txtAge.Text = "" Or dtpDate.Value = "" _
        Or txtSickness.Text = "" Or txtFluid.Text = "" Or txtBottle.Text = "" Or txtDrop.Text = "" _
        Then

            MsgBox("Please Complete All the Required Fields")

        Else
            Try

                Dim PatientInfoConnection As SqlConnection = New _
                SqlConnection("Server=CATH-PC; database=PatientInfoDB;user id=sa;password=********") 'connection to SQL database

                PatientInfoConnection.Open() 'open database connection

                Dim sql As String = ""
                sql = "insert into PatientInfo (name, age, date_of_confinement,type_of_sickness, type_of_IVfluid, number_of_bottles, drop_rate)" & _
                                   " values (@name, @age, @date_of_confinement, @type_of_sickness, @type_of_IV_fluid, @number_of_bottles, @drop_rate)"


                Dim insert As New SqlCommand(sql, PatientInfoConnection)
                insert.Parameters.AddWithValue("@name", txtName.Text)
                insert.Parameters.AddWithValue("@age", Convert.ToInt32(txtAge.Text))
                insert.Parameters.AddWithValue("@date_of_confinement", Date.Parse(dtpDate.Value))
                insert.Parameters.AddWithValue("@type_of_sickness", txtSickness.Text)
                insert.Parameters.AddWithValue("@type_of_IV_fluid", txtFluid.Text)
                insert.Parameters.AddWithValue("@number_of_bottles", Convert.ToInt32(txtBottle.Text))
                insert.Parameters.AddWithValue("@drop_rate", Convert.ToInt32(txtDrop.Text))

                insert.ExecuteNonQuery()

                PatientInfoConnection.Close() 'close database connection

                MsgBox("Successfully Saved")
                Me.Visible = False
                Mainform.Show()


            Catch myerror As MySqlException
                MessageBox.Show("Error Connecting to Database: " & myerror.Message & ". Please contact the operator")

            End Try
        End If
    Catch ex As Exception
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

在每个行的步骤中为您的方法添加断点,以找到发生异常的确切行的位置。

试试这个:

Dim sql As String
sql = "insert into PatientInfo (name, age, date_of_confinement,type_of_sickness, type_of_IVfluid, number_of_bottles, drop_rate)" & _
                      " values (@name, @age, @date_of_confinement, @type_of_sickness, @type_of_IV_fluid, @number_of_bottles, @drop_rate)"


                Dim insert As New SqlCommand(sql, PatientInfoConnection)
                insert.Parameters.AddWithValue("@name", txtName.Text)
                insert.Parameters.AddWithValue("@age", Convert.ToInt32(txtAge.Text))
                insert.Parameters.AddWithValue("@date_of_confinement", Date.Parse(dtpDate.Value))
                insert.Parameters.AddWithValue("@type_of_sickness", txtSickness.Text)
                insert.Parameters.AddWithValue("@type_of_IV_fluid", txtFluid.Text)
                insert.Parameters.AddWithValue("@number_of_bottles", Convert.ToInt32(txtBottle.Text))
                insert.Parameters.AddWithValue("@drop_rate", Convert.ToInt32(txtDrop.Text))

                insert.ExecuteNonQuery()

此致