|
|
Please I am using VB.net 2019 and Sql 2016 and I have the below codes which inserts the pdf into the database successfully. But retrieving it becomes a problem for me and that is not the case for an image. I want to convert and retrieve this line of code into pdf "Me.Pdf1.Image=Image.fromStream(ms)" which gives an exception because it is not an image but pdf in the database. Please help me. Thank
OpenFileDialog1.ShowDialog()
txtPdfPath.Text = OpenFileDialog1.FileName
Dim ms1 As New MemoryStream
frmBPA1.Pic1.Image.Save(ms1, frmBPA1.Pic1.Image.RawFormat)
SqlQuery = "Insert into BPA1 (Pdf1)Values(@Pdf1)"
com.Parameters.AddWithValue("@Pdf1", ms1.ToArray)
com.CommandText = SqlQuery
com.CommandType = CommandType.Text
SQLCon.Open()
com.ExecuteNonQuery()
SQLCon.Close()
Dim strQuery As String = "Select df1 from BPA1"
SQLCon1.Open()
comFile = New SqlCommand(strQuery, SQLCon1)
comFile.Parameters.AddWithValue("@ReceiptNo", Me.txtReceiptNo.Text.Trim)
daFile = New SqlDataAdapter(comFile)
daFile.Fill(taFile)
If taFile.Rows(0).Item("Pic1") IsNot DBNull.Value Then
Dim img() As Byte
img = taFile.Rows(0).Item("Pdf1")
Dim ms As New MemoryStream(img)
If img.Length <> "0" Then
Me.Pic1.Image = Image.FromStream(ms)
End If
|
|
|
|
|
txtPdfPath.Text = OpenFileDialog1.FileName
That is not uploading anything, OpenFileDialog gets filenamse from the uiser, but it does not read them.
frmBPA1.Pic1.Image.Save(ms1, frmBPA1.Pic1.Image.RawFormat)
SqlQuery = "Insert into BPA1 (Pdf1)Values(@Pdf1)"
You create a memory stream object ms1 from some image data. You then try to save something that you have saved in the variable named Pdf1 , but you have not placed any data in Pdf1 that I can see. And why are you treating a PDF file as image data?
|
|
|
|
|
Thanks for the reply. Please storing the pdf is not a problem but retrieving it is issue for now. Please how do I retrieve this line Me.Pic1.Image = Image.FromStream(ms) and convert it into pdf. This line of code was used to retrieve image file correctly and I used the same process to store a pdf file but the above code to retrieve is it is the problem. Thanks
Dim strQuery As String = "Select ReceiptNo,Pic1 from BPA1 where ReceiptNo=@ReceiptNo"
SQLCon1.Open()
comFile = New SqlCommand(strQuery, SQLCon1)
comFile.Parameters.AddWithValue("@ReceiptNo", Me.txtReceiptNo.Text.Trim)
daFile = New SqlDataAdapter(comFile)
daFile.Fill(taFile)
If taFile.Rows(0).Item("Pic1") IsNot DBNull.Value Then
Dim img() As Byte
img = taFile.Rows(0).Item("Pic1")
Dim ms As New MemoryStream(img)
If img.Length <> "0" Then
Me.Pic1.Image = Image.FromStream(ms)
Else
Me.Pic1.Image = Nothing
End If
End If
|
|
|
|
|
If you cannot retrieve the PDF data then how can you be sure that you have saved it correctly? As I mentioned in my previous response, I cannot see any PDF data being saved.
|
|
|
|
|
That demonstrates a misunderstanding of what a PDF is.
To view an image such as a png you use an image viewer that supports png images. To view a PDF you use a PDF viewer. Those are two different things.
Your thought process should be - first section.
1. Find the file (it does not matter that it is a PDF.)
2. Store the bytes in the file as binary/raw in the database. At this point it is not actually a PDF (and certainly not an image), but rather just binary/raw data.
Second section
1. Load the binary data
2. Handle the binary data as a PDF file. Such as figuring out how to either use a PDF viewer or deferring to something (like browser).
From the above this means that the following code is always going to be wrong. Because it is not an image.
Image.FromStream(ms)
|
|
|
|
|
How to access scanner device in vb?
|
|
|
|
|
This question can only be answered with the documentation of this device and perhaps additional information from the distributer.
|
|
|
|
|
|
<pre>Hi, i am quit new to .net. But this should be easy for you guys
the output of a string is 202205W24 or 202205W1
I want the last digit so 24 or 1 from the string and use those digits as a string</pre>
|
|
|
|
|
|
|
Was the extra 'c' a test for the observant?
|
|
|
|
|
|
|
I've been struggling to send this CURL request in VB.NET and hope someone can help..
Here is a link to the documentation of the exact request I'm trying to send.
This is the sample CURL request:
curl -u 'username:token' 'https://api.dev.name.com/v4/domains' -X POST -H 'Content-Type: application/json' --data '{"domain":{"domainName":"example.org"},"purchasePrice":12.99}'
Here is the code that I put together and tested:
Dim uri As New Uri("https://api.dev.name.com/v4/domains")
Dim myUsername As String = txtUsername.Text.Trim & ":" & txtApiKey.Text.Trim
Dim data As String = "{""domain"":{""domainName"":""example.org""},""purchasePrice"":12.99}"
Dim dataByte As Byte()
request = WebRequest.Create(uri)
request.Headers.Add(HttpRequestHeader.Authorization, "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes(myUsername)))
request.ContentType = "application/json"
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
request.Method = "POST"
dataByte = Encoding.UTF8.GetBytes(data)
request.GetRequestStream.Write(dataByte, 0, dataByte.Length)
Dim myResp As HttpWebResponse
myResp = request.GetResponse()
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
WebResponse = myreader.ReadToEnd()
MsgBox(WebResponse)
However, the code above always returns a (400) Bad Request.
Here is another variation of pretty much the same code which I tried:
Dim myReq As HttpWebRequest
Dim myResp As HttpWebResponse
Dim myUsername As String = "username:token"
myReq = HttpWebRequest.Create("https://api.dev.name.com/v4/domains")
myReq.Method = "POST"
myReq.ContentType = "application/json"
myReq.Headers.Add("Authorization", "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes(myUsername)))
Dim myData As String = "{""domain"":{""domainName"":""example.org""},""purchasePrice"":12.99}"
Dim dataBytes As Byte() = Encoding.UTF8.GetBytes(myData)
myReq.GetRequestStream.Write(dataBytes, 0, dataBytes.Length)
myResp = myReq.GetResponse()
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
WebResponse = myreader.ReadToEnd()
MsgBox(WebResponse)
But, as you can already guess, it also returns a 400 (Bad Request).
|
|
|
|
|
In your CURL command, you are using the '-u' option to provide the username and token as a basic authentication header. In your VB.NET code, you're attempting to encode the credentials manually. Make sure that 'myUsername' has the correct username and token values, and try using the Credentials property of the 'WebRequest' object instead of manually adding the header -
request.Credentials = New NetworkCredential(txtUsername.Text.Trim, txtApiKey.Text.Trim)
The JSON data you're sending in the CURL command needs to be properly serialized before sending it, we use Newtonsoft.Json, not sure if it will help in your situation, if so you can use it where you create an anonymous type to represent the JSON structure and then serializes it using 'JsonConvert.SerializeObject()' -
Imports Newtonsoft.Json
Dim domainData As New With {
.domain = New With {
.domainName = "example.org"
},
.purchasePrice = 12.99
}
Dim data As String = JsonConvert.SerializeObject(domainData)
You need to close the request stream before sending the request -
request.GetRequestStream.Close()
Instead of directly displaying returned responses in a message box, you can return the status codes and any error messages further by using -
Dim statusCode As Integer = CType(myResp, HttpWebResponse).StatusCode
Dim responseText As String = String.Empty
If statusCode = HttpStatusCode.OK Then
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
responseText = myreader.ReadToEnd()
End If
MsgBox(responseText)
I hope any of these suggestions help.
|
|
|
|
|
Hey Andre,
Thanks so much for your detailed reply and explanation! I imported Newtonsoft.json no problem and tried implementing your changes. On a positive note, I'm no longer getting a (400) Bad Request reply. However, I'm now getting a (401) Unauthorized response. I've double checked my login details and made sure they were correct. I also tried testing in both the OTE (operational test environment) and the LIVE environment because the details are slightly different for each, but received the same error on both.
Could this be due to how the credentials are now being passed?
Also, if I close "GetRequestStream" before sending the request then I get a message the connection was suddenly terminated when trying to send the request. However, if I put it directly after writing to the stream that issue goes away. Here is a look at my updated code:
Dim uri As New Uri("https://api.dev.name.com/v4/domains")
Dim domainData As New With {
.domain = New With {
.domainName = "example.org"
},
.purchasePrice = 12.99
}
Dim data As String = JsonConvert.SerializeObject(domainData)
Dim dataByte As Byte()
request = WebRequest.Create(uri)
request.Credentials = New NetworkCredential(txtUsername.Text.Trim, txtApiKey.Text.Trim)
request.ContentType = "application/json"
request.Method = "POST"
dataByte = Encoding.UTF8.GetBytes(data)
request.GetRequestStream.Write(dataByte, 0, dataByte.Length)
request.GetRequestStream.Close()
Dim myResp As HttpWebResponse
myResp = request.GetResponse()
Dim statusCode As Integer = CType(myResp, HttpWebResponse).StatusCode
Dim responseText As String = String.Empty
If statusCode = HttpStatusCode.OK Then
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
responseText = myreader.ReadToEnd()
End If
MsgBox(responseText)
Any suggestions? Thanks again!
|
|
|
|
|
Closing the stream - my bad, I added it prematurely. Please disregard my suggestion to close the request stream before sending the request or add it after the stream were read.
(401) Unauthorized response, it seems that your server did not accept the given credentials as valid. - There are a few things you can try.
Make sure that the API you are accessing is expecting basic authentication and that the credentials should be provided in the Authorization header. If the API expects a different authentication method, such as API keys or OAuth, you will need to adjust your authentication to accept the request as per their requirements.
Check that the API endpoint you are using (https://api.dev.name.com/v4/domains) is 100% correct.
To isolate or debug the issue, you could try using a different client, such as cURL or Postman, with the same credentials and endpoint. This might help determine if the problem lies with the code or if there might be an issue with the credentials or API itself.
Check your API documentation to make sure that you are following the correct authentication process and that there are no additional steps required.
Hope this points you in the right direction.
|
|
|
|
|
hey..me again..at least i keep the forum moving :P ...thanks for your help...i have another question sorry...
i have this code
<pre>Public Class SSVoiceRecordCtrl
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Dim lSamples, lRet, lBits, lChannels As Integer
Dim iBlockAlign As Short
Dim lBytesPerSec As Integer
Dim J As Integer = 1
Public Enum SoundFormats
Mono_6kbps_8_Bit
Mono_8kbps_8_Bit
Mono_11kbps_8_Bit
Mono_16kbps_8_Bit
Mono_22kbps_8_Bit
Mono_32kbps_8_Bit
Mono_44kbps_8_Bit
Mono_48kbps_8_Bit
Stereo_24kbps_16_Bit
Stereo_32kbps_16_Bit
Stereo_44kbps_16_Bit
Stereo_64kbps_16_Bit
Stereo_88kbps_16_Bit
Stereo_1288kbps_16_Bit
Stereo_176kbps_16_Bit
Stereo_192kbps_16_Bit
End Enum
Public Enum MyState
Idle
Recording
Paused
End Enum
Private xState As MyState
Public ReadOnly Property State() As MyState
Get
State = xState
End Get
End Property
Private _SoundFormat As SoundFormats
Public Property SoundFormat() As SoundFormats
Get
SoundFormat = _SoundFormat
End Get
Set(ByVal Value As SoundFormats)
_SoundFormat = Value
End Set
End Property
Private Sub GetSoundFormat()
If _SoundFormat = SoundFormats.Mono_6kbps_8_Bit Then
lSamples = 6000 : lBits = 8 : lChannels = 1
ElseIf _SoundFormat = SoundFormats.Mono_8kbps_8_Bit Then
lSamples = 8000 : lBits = 8 : lChannels = 1
ElseIf _SoundFormat = SoundFormats.Mono_11kbps_8_Bit Then
lSamples = 11025 : lBits = 8 : lChannels = 1
ElseIf _SoundFormat = SoundFormats.Mono_16kbps_8_Bit Then
lSamples = 16000 : lBits = 8 : lChannels = 1
ElseIf _SoundFormat = SoundFormats.Mono_22kbps_8_Bit Then
lSamples = 22050 : lBits = 8 : lChannels = 1
ElseIf _SoundFormat = SoundFormats.Mono_32kbps_8_Bit Then
lSamples = 32000 : lBits = 8 : lChannels = 1
ElseIf _SoundFormat = SoundFormats.Mono_44kbps_8_Bit Then
lSamples = 44100 : lBits = 8 : lChannels = 1
ElseIf _SoundFormat = SoundFormats.Mono_48kbps_8_Bit Then
lSamples = 48000 : lBits = 8 : lChannels = 1
ElseIf _SoundFormat = SoundFormats.Stereo_24kbps_16_Bit Then
lSamples = 6000 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_32kbps_16_Bit Then
lSamples = 8000 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_44kbps_16_Bit Then
lSamples = 11025 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_64kbps_16_Bit Then
lSamples = 16000 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_88kbps_16_Bit Then
lSamples = 22050 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_1288kbps_16_Bit Then
lSamples = 32000 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_176kbps_16_Bit Then
lSamples = 44100 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_192kbps_16_Bit Then
lSamples = 48000 : lBits = 16 : lChannels = 2
End If
iBlockAlign = lChannels * lBits / 8
lBytesPerSec = lSamples * iBlockAlign
End Sub
Private FName As String
Public Property FileName() As String
Get
FileName = FName
End Get
Set(ByVal Value As String)
FName = Value
End Set
End Property
Public Function StartRecord() As Boolean
Call GetSoundFormat()
On Error GoTo ER
If FName = "" Then GoTo ER
Dim i As Integer
i = mciSendString("open new type waveaudio alias capture", vbNullString, 0, 0)
I = mciSendString("set capture samplespersec " & lSamples & " channels " & lChannels & " bitspersample " & lBits & " alignment " & iBlockAlign & " bytespersec " & lBytesPerSec, vbNullString, 0, 0)
I = mciSendString("record capture", vbNullString, 0, 0)
xState = MyState.Recording
StartRecord = True
picStatus.Image = imgList.Images(1)
Timer1.Enabled = True
Exit Function
ER:
StartRecord = False
Dim SSVoiceRecordControlExec As New ArgumentException("File Name Not Specified.")
Throw SSVoiceRecordControlExec
End Function
Public Function StopRecord() As Boolean
On Error GoTo ER
If FName = "" Then GoTo ER
Dim i As Integer
I = mciSendString("save capture " & FName, vbNullString, 0, 0)
I = mciSendString("close capture", vbNullString, 0, 0)
xState = MyState.Idle
StopRecord = True
picStatus.Image = imgList.Images(0)
Timer1.Enabled = False
Exit Function
ER:
i = mciSendString("close capture", vbNullString, 0, 0)
StopRecord = False
Dim SSVoiceRecordControlExec As New ArgumentException("File Name Not Specified.")
Throw SSVoiceRecordControlExec
End Function
Public Sub CloseRecord()
Dim i As Integer
i = mciSendString("close capture", vbNullString, 0, 0)
xState = MyState.Idle
picStatus.Image = imgList.Images(0)
Timer1.Enabled = False
End Sub
Private Sub Class_Initialize_Renamed()
xState = MyState.Idle
End Sub
Private Sub Class_Terminate_Renamed()
StopRecord()
End Sub
Protected Overrides Sub Finalize()
Class_Terminate_Renamed()
MyBase.Finalize()
End Sub
Public Function PauseRecord() As Boolean
On Error GoTo ER
If FName = "" Then GoTo ER
Dim RS As String
Dim cb, I As Integer
RS = Space(128)
If xState = MyState.Paused Then
I = mciSendString("record capture", vbNullString, 0, 0)
xState = MyState.Recording
picStatus.Image = imgList.Images(1)
Timer1.Enabled = True
ElseIf xState = MyState.Recording Then
I = mciSendString("pause capture", vbNullString, 0, 0)
xState = MyState.Paused
picStatus.Image = imgList.Images(0)
Timer1.Enabled = False
End If
PauseRecord = True
Exit Function
ER:
PauseRecord = False
Dim SSVoiceRecordControlExec As New ArgumentException("File Name Not Specified.")
Throw SSVoiceRecordControlExec
End Function
Private Sub SSVoiceRecordCtrl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
picStatus.Image = imgList.Images(0)
End Sub
Private Sub picStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picStatus.DoubleClick
System.Diagnostics.Process.Start("SNDVOL32.EXE")
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If J = 1 Then
picStatus.Image = imgList.Images(J)
J += 1
Else
picStatus.Image = imgList.Images(J)
J = 1
End If
End Sub
End Class
i understand the code by i do not see where to change the sound source for ex. to line in...
they say this
Notes : A record class, which when turned
' : into an object lets you record sound
' : through mic, cd-line, etc... into a file
|
|
|
|
|
Try asking Suresh Sutha, it's his/her code.
|
|
|
|
|
Probably deprecated...i will try NAudio...is it good alternative?
|
|
|
|
|
according to the code...may i add more of these (24-bits)
lSamples = 32000 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_176kbps_16_Bit Then
lSamples = 44100 : lBits = 16 : lChannels = 2
ElseIf _SoundFormat = SoundFormats.Stereo_192kbps_16_Bit Then
lSamples = 48000 : lBits = 16 : lChannels = 2
End If
i would like to have
ElseIf _SoundFormat = SoundFormats.Stereo_192kbps_24_Bit Then
lSamples = 48000 : lBits = 24 : lChannels = 2
|
|
|
|
|
Sorry, no idea. As I said you need to talk to the person who wrote the code.
|
|
|
|
|
Ok thanks ...i will try..i just would like to know how to select the input source device...i think is just what misses in my new project....now im trying to make a sound recording..but i would like to record mic,line in and if possible desktop sounds..
|
|
|
|
|