hi guys i have an application which adds a patient with his/her own picture.. im doing it in vb.net and my database is stored in sql server 2005.. i have my column for saving the picture.. i have already set it to varbinary(max) my problem is i dont have a clue how to do it.. i want to have a picture box where the picture will be saved, and open file directory to browse for picture.
so far this is my code for add button
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
da.InsertCommand = New SqlCommand("SET IDENTITY_INSERT Patients ON; INSERT INTO Patients(Patient_ID, Case_number, Patient_Type, LName, FName, MName) VALUES(@Patient_ID, @Case_number, @Patient_Type, @LName, @FName,@MName) SET IDENTITY_INSERT Patients OFF", cs)
da.InsertCommand.Parameters.Add("@Patient_ID", SqlDbType.VarChar).Value = Txtpatient_id.Text
da.InsertCommand.Parameters.Add("@Case_Number", SqlDbType.VarChar).Value = IIf(String.IsNullOrEmpty(txtCaseNumber.Text), DBNull.Value, txtCaseNumber.Text)
da.InsertCommand.Parameters.Add("@Patient_Type", SqlDbType.VarChar).Value = IIf(String.IsNullOrEmpty(cboxpatient_Type.Text), DBNull.Value, cboxpatient_Type.Text)
da.InsertCommand.Parameters.Add("@LName", SqlDbType.VarChar).Value = IIf(String.IsNullOrEmpty(TxtLName.Text), DBNull.Value, TxtLName.Text)
da.InsertCommand.Parameters.Add("@FName", SqlDbType.VarChar).Value = IIf(String.IsNullOrEmpty(TxtFName.Text), DBNull.Value, TxtFName.Text)
da.InsertCommand.Parameters.Add("@MName", SqlDbType.VarChar).Value = IIf(String.IsNullOrEmpty(TxtMName.Text), DBNull.Value, TxtMName.Text)
'// this line i just copied the code from another user here
Dim myfilelocation As String = "C:\"
Dim cmda As New SqlCommand("Insert Into Patients(Picture) Values (@Picture)", cs)
Dim param As New SqlParameter("@Picture", SqlDbType.VarBinary)
Dim ImageData As Byte() = IO.File.ReadAllBytes(myfilelocation)
param.Value = ImageData
cmda.Parameters.Add(param)
cmda.Parameters.AddWithValue("@Patient_ID", 3)
Try
cs.Open()
cmda.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cs.Close()
End Try
'And Below is the code to retrieve
Dim cmd As New SqlCommand("select Picture from Patients where Patient_ID=@Patient_ID ", cs)
cmd.Parameters.AddWithValue("@Patient_ID", 3)
Try
cs.Open()
PictureBox1.Image = Image.FromStream(New IO.MemoryStream(CType(cmd.ExecuteScalar, Byte())))
' or you can save in a file
'IO.File.WriteAllBytes("c:\backup\image3.jpg", CType(cmd.ExecuteScalar, Byte()))
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cs.Close()
End Try
cs.Open()
da.InsertCommand.ExecuteNonQuery()
cs.Close()
MsgBox("New Patient Successfully admitted!")
End Sub