jeudi 23 avril 2015

Hot to fix ExecuteNonQuery: CommandText property has not been initialized?

I am getting an error , "ExecuteNonQuery: CommandText property has not been initialized" , Please help !!! I have been executing this code to save data in my other forms with our any error but when i try to use this with image save its giving me this error.

 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

Aucun commentaire:

Enregistrer un commentaire