-
Jul 25th, 2021, 07:28 PM
#1
Thread Starter
Lively Member
Image retrieve from SQL DB
Hi,
I am trying to retrieve image and display it on the picturebox2 on datagridview cellmouseclick but receive the following error message:
Code:
System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'
Code:
This exception was originally thrown at this call stack:
[External Code]
RCMS.FrmRegisterTenant.DgTenantDetails_CellMouseClick(Object, System.Windows.Forms.DataGridViewCellMouseEventArgs) in FrmRegisterTenant.vb
[External Code]
The code I am using is:
Code:
Private Sub DgTenantDetails_CellMouseClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DgTenantDetails.CellMouseClick
Dim conn As SqlConnection = GetDbConnection()
Call TenantsDetailsDisable()
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow = DgTenantDetails.Rows(e.RowIndex)
TxtFNameUp.Text = row.Cells(1).Value.ToString
TxtLNameUp.Text = row.Cells(2).Value.ToString
TxtTelUp.Text = row.Cells(3).Value.ToString
TxtEmailUp.Text = row.Cells(4).Value.ToString
TxtNationalIDUp.Text = row.Cells(5).Value.ToString
Dim img As Byte
img = DgTenantDetails.Item(7, e.RowIndex).Value
Dim ms As New MemoryStream(img)
PictureBox2.Image = Image.FromStream(ms)
PictureBox2.SizeMode = PictureBoxSizeMode.StretchImage
End If
End Sub
Secondly, issue (2) when I click on the datagridview I also receive the following error message:
Code:
The following exception occurred in the DataGridView.
System.Data.NoNullAllowedException: Column 'firstname' does not allow null
Note: firstname is not NULL - I dont understand.
Thanks
-
Jul 26th, 2021, 06:27 AM
#2
Re: Image retrieve from SQL DB
The first error message tells you that the index you used was outside the valid range. What was the index and what was the valid range? Did you look? If not, why not? If so, why didn't you pass that information on to us?
As for the second issue, I'm not even sure where that error is occurring because you didn't bother to tell us or possibly even find out. If the error message says that the firstname column contains null then it contains null. You need to debug to find out why. You don't diagnose these issue simply by reading small portions of code in isolation unless they are glaring issues. You need to actually watch the code as it runs. That's why VS has a debugger. You need to do a lot more work for yourself and then provide us with all the relevant information if you still need help.
If you don't know how to use the debugger then stop what you're doing and learn. You should not be surprised that you cannot do your work effectively if you don't know how to use the required tools.
-
Jul 27th, 2021, 09:04 AM
#3
Thread Starter
Lively Member
Image Retrieve
Hi,
I have the following code that works fine if the PictureBox2.Image is commented out. All the other text boxes get populated with data from the datagridview.
Now, I want the image to be displayed from the datagridview to picturebox.
Code:
Dim img As Byte
img = DgTenantDetails.Item(6,e.RowIndex).Value
Dim ms As New MemoryStream(img)
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow = DgTenantDetails.Rows(e.RowIndex)
TxtFNameUp.Text = row.Cells(1).Value.ToString
TxtLNameUp.Text = row.Cells(2).Value.ToString
TxtTelUp.Text = row.Cells(3).Value.ToString
TxtEmailUp.Text = row.Cells(4).Value.ToString
TxtNationalIDUp.Text = row.Cells(5).Value.ToString
PictureBox2.Image = Image.FromStream(ms)
End If
The code runs and gives the following error message:
Code:
System.InvalidCastException: 'Conversion from type 'Byte()' to type 'Byte' is not valid.'
Please help!
Thank you
Moderator Actions: Merged threads.
Last edited by dr225; Jul 27th, 2021 at 09:24 AM.
-
Jul 27th, 2021, 09:30 AM
#4
Addicted Member
Re: Image retrieve from SQL DB
You have presumably saved the image to your database as a byte array, but you have only declared img as a single byte, this should be,
Dim img As Byte()
-
Jul 27th, 2021, 09:35 AM
#5
Thread Starter
Lively Member
Re: Image retrieve from SQL DB
Thanks - it worked!
Just one more question:
When I navigated from one search option to another by cellmouseclick of the datagridview I received an error message:
Code:
System.ArgumentNullException: 'Buffer cannot be null.
Parameter name: buffer'
The offending line of code:
Code:
Dim ms As New MemoryStream(img)
Do you know why?
Last edited by dr225; Jul 27th, 2021 at 09:41 AM.
-
Jul 27th, 2021, 09:43 AM
#6
Addicted Member
Re: Image retrieve from SQL DB
Following on from Jmc's comment above, is you will need to debug the code and check what is happening when you perform that action. Somewhere you are failing to provide an argument to a function or check to see if it nothing.
-
Jul 27th, 2021, 07:05 PM
#7
Re: Image retrieve from SQL DB
Take a look at the following repository.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|