如何修复“ExecuteNonQuery:CommandText属性尚未初始化?”错误?

时间:2015-04-24 06:32:32

标签: vb.net sql-server-2005

我收到了错误,

  

“ExecuteNonQuery:CommandText属性尚未初始化”

请帮忙!!!我一直在执行此代码以保存我的其他形式的数据与我们的任何错误,但当我尝试使用它与图像保存它给我这个错误。

 Sub Save_Employee()
    Dim conn As New SqlConnection(ConnectionString)
    Dim comm As SqlCommand = New SqlCommand()
    comm.Connection = conn
    conn.Open()

    Dim str As String = "INSERT INTO Employee_Tbl VALUES (@RegNo,@FullName,@Country,@Occupation,@WorkSite,@DOB,@Photo,@Remarks,@Status)"
    comm.Parameters.AddWithValue("@RegNo", RegNo_TextBox.Text)
    comm.Parameters.AddWithValue("@FullName", FullName_TextBox.Text)
    comm.Parameters.AddWithValue("@Country", Country_ComboBox.Text)
    comm.Parameters.AddWithValue("@Occupation", Occupation_ComboBox.Text)
    comm.Parameters.AddWithValue("@WorkSite", WorkSite_TextBox.Text)
    comm.Parameters.AddWithValue("@DOB", DOB_DateTimePicker.Text)

    Try
        Dim ms As New System.IO.MemoryStream
        Dim bmpImage As New Bitmap(myimage.Image)

        bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
        bytImage = ms.ToArray()
        ms.Close()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

    comm.Parameters.AddWithValue("@Photo", bytImage)
    comm.Parameters.AddWithValue("@Remarks", Remarks_TextBox.Text)
    comm.Parameters.AddWithValue("@Status", Status_ComboBox.Text)
    comm.ExecuteNonQuery()
    MessageBox.Show("Saved successful!", "Employee Created", MessageBoxButtons.OK, MessageBoxIcon.Information)
    comm.ExecuteNonQuery()
    conn.Close()
End Sub

2 个答案:

答案 0 :(得分:0)

您需要在CommandText

之前设置comm.ExecuteNonQuery()属性
comm.CommandText = str 
comm.ExecuteNonQuery()

答案 1 :(得分:0)

以下代码可能会对您有所帮助。

Sub Save_Employee()
    Dim conn As New SqlConnection(ConnectionString)
    Dim comm As SqlCommand
    Dim str As String

    Try
        Dim ms As New System.IO.MemoryStream
        Dim bmpImage As New Bitmap(myimage.Image)

        bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
        bytImage = ms.ToArray()
        ms.Close()
        str = "INSERT INTO Employee_Tbl VALUES (@RegNo,@FullName,@Country,@Occupation,@WorkSite,@DOB,@Photo,@Remarks,@Status)"
        comm.Parameters.AddWithValue("@RegNo", RegNo_TextBox.Text)
        comm.Parameters.AddWithValue("@FullName", FullName_TextBox.Text)
        comm.Parameters.AddWithValue("@Country", Country_ComboBox.Text)
        comm.Parameters.AddWithValue("@Occupation", Occupation_ComboBox.Text)
        comm.Parameters.AddWithValue("@WorkSite", WorkSite_TextBox.Text)
        comm.Parameters.AddWithValue("@DOB", DOB_DateTimePicker.Text)
        comm.Parameters.AddWithValue("@Photo", bytImage)
        comm.Parameters.AddWithValue("@Remarks", Remarks_TextBox.Text)
        comm.Parameters.AddWithValue("@Status", Status_ComboBox.Text)
    Catch ex As Exception
        MS.Close()
        MsgBox(ex.ToString)

        str = "INSERT INTO Employee_Tbl VALUES (@RegNo,@FullName,@Country,@Occupation,@WorkSite,@DOB)"
        comm.Parameters.AddWithValue("@RegNo", RegNo_TextBox.Text)
        comm.Parameters.AddWithValue("@FullName", FullName_TextBox.Text)
        comm.Parameters.AddWithValue("@Country", Country_ComboBox.Text)
        comm.Parameters.AddWithValue("@Occupation", Occupation_ComboBox.Text)
        comm.Parameters.AddWithValue("@WorkSite", WorkSite_TextBox.Text)
        comm.Parameters.AddWithValue("@DOB", DOB_DateTimePicker.Text)
    End Try

    Try
        conn.Open()
        comm = New SqlCommand(str, conn)
        comm.ExecuteNonQuery()
        MessageBox.Show("Saved successful!", "Employee Created", MessageBoxButtons.OK, MessageBoxIcon.Information)
        conn.Close()
    Catch ex As Exception
        conn.Close()
        MsgBox(ex.ToString)
    End Try
End Sub