Hi all,
can anyone please help me to save an image to database. My code was saving the images previously but do know I might have changed something.
Code is given below and is not giving any errors.
Any advise will be considered as a great help.
Thanks.
What I have tried:
<pre> Dim strSQL As String
Dim lngID As Double
lngID = SQLGetNumericFieldValue("SELECT IsNull(Max(RequestID), 0)+1 AS MaxVal From TravelApplicationRequest", "MaxVal")
txtRequestNo.Text = lngID
strSQL = "INSERT INTO TravelApplicationRequest (RequestID, ApplicationNo, CustomerCode, CustomerName, StoreName, Package, VisaType, " & _
"VisaNo, VisaIssuedate, VisaExpiryDate, PassportNo, PassportIssueDate, PassportExpiryDate, NIDNo, " & _
"HotelName, HotelCity, HotelCountry, HotelAddress, HotelChkInDate, Days, " & _
"Rooms, Meals, ChckOutdate, Notes, DepAirline, DepFlightNo, DepFromCity, DepToCity, DepFlightDate, DepTktNo, " & _
"ArrAirline, ArrFlightNo, ArrFromCity, ArrToCity, ArrFlightDate, ArrTktNo, isTravelWithDependents, IsApproved, SelectApprover, IsProcessed) VALUES ("
strSQL = strSQL & "'" & txtRequestNo.Text & "', "
strSQL = strSQL & "'" & txtApplicationNo.Text & "', "
strSQL = strSQL & "'" & txtCustomerCode.Text & "', "
strSQL = strSQL & "'" & txtCustomerName.Text & "', "
strSQL = strSQL & "'" & txtBranch.Text & "', "
strSQL = strSQL & "'" & txtPackage.Text & "', "
strSQL = strSQL & "'" & txtVisaType.Text & "', "
strSQL = strSQL & "'" & txtVisaNo.Text & "', "
strSQL = strSQL & "'" & txtVisaIssueDate.Text & "', "
strSQL = strSQL & "'" & txtVisaExpiry.Text & "', "
strSQL = strSQL & "'" & txtPassportNo.Text & "', "
strSQL = strSQL & "'" & txtPassportIssueDate.Text & "', "
strSQL = strSQL & "'" & txtPassportExpiry.Text & "', "
strSQL = strSQL & "'" & txtIDNo.Text & "', "
strSQL = strSQL & "'" & txtHotelName.Text & "', "
strSQL = strSQL & "'" & txtCity.Text & "', "
strSQL = strSQL & "'" & txtCountry.Text & "', "
strSQL = strSQL & "'" & txtAddress.Text & "', "
strSQL = strSQL & "'" & txtChkInDate.Text & "', "
strSQL = strSQL & "'" & txtStay.Text & "', "
strSQL = strSQL & "'" & txtRooms.Text & "', "
strSQL = strSQL & "'" & txtMeals.Text & "', "
strSQL = strSQL & "'" & txtChkOut.Text & "', "
strSQL = strSQL & "'" & txtDetails.Text & "', "
strSQL = strSQL & "'" & txtDepAirLine.Text & "', "
strSQL = strSQL & "'" & txtDepFlightNo.Text & "', "
strSQL = strSQL & "'" & txtDepFrCity.Text & "', "
strSQL = strSQL & "'" & txtDepToCity.Text & "', "
strSQL = strSQL & "'" & txtDepFlightDate.Text & "', "
strSQL = strSQL & "'" & txtDepTktNo.Text & "', "
strSQL = strSQL & "'" & txtArrAirline.Text & "', "
strSQL = strSQL & "'" & txtArrFlightNo.Text & "', "
strSQL = strSQL & "'" & txtArrFCity.Text & "', "
strSQL = strSQL & "'" & txtArrToCity.Text & "', "
strSQL = strSQL & "'" & txtArrFlightDate.Text & "', "
strSQL = strSQL & "'" & txtArrTktNo.Text & "', "
strSQL = strSQL & "'" & txtDependents.Text & "', "
strSQL = strSQL & "'" & txtIsApproved.Text & "', "
strSQL = strSQL & "'" & txtApprovedBy.Text & "',"
strSQL = strSQL & "'" & txtIsProcessed.Text & "')"
ExecuteSQLQuery(strSQL)
If TxtImageCust.BackgroundImage IsNot Nothing Then
strSQL = "UPDATE TravelApplicationRequest SET Image = @Image WHERE RequestID = " & lngID
Dim SqlConn As New SqlClient.SqlConnection
Dim Sqlcmmd As New SqlClient.SqlCommand
Sqlcmmd.CommandTimeout = 1000
Try
SqlConn.ConnectionString = ConnectionStrinG
SqlConn.Open()
Dim cmd As New SqlCommand(strSQL, SqlConn)
Dim ms As New MemoryStream()
TxtImageCust.BackgroundImage.Save(ms, TxtImageCust.BackgroundImage.RawFormat)
Dim data As Byte() = ms.GetBuffer()
Dim p As New SqlParameter("@Image", SqlDbType.Image)
p.Value = data
cmd.Parameters.Add(p)
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
Finally
SqlConn.Close()
SqlConn.Dispose()
End Try