473,325 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

How to handle picturebox from datagridview if database record is null

I am trying to get images from my datagridview and got success, but if some records dont have image or (null), than i got error ( Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.). Following is my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  private void dgridEmployees_SelectionChanged(object sender, EventArgs e)
  3.         {
  4.             DataGridViewCell cell = null;
  5.             foreach (DataGridViewCell selectedCell in dgridEmployees.SelectedCells)
  6.             {
  7.                 cell = selectedCell;
  8.                 break;
  9.  
  10.             }
  11.  
  12.             if (cell != null)
  13.             {
  14.                 DataGridViewRow row = cell.OwningRow;
  15.                 lbl_ID.Text = row.Cells[0].Value.ToString();
  16.                 tboxEmployeeID.Text = row.Cells[1].Value.ToString();
  17.                 tboxEmployeeName.Text = row.Cells[2].Value.ToString();
  18.                 dtboxJoiningDate.Text = row.Cells[3].Value.ToString();
  19.                 tboxDepartment.Text = row.Cells[4].Value.ToString();
  20.                 tboxPath.Text = row.Cells[5].Value.ToString();
  21.  
  22.                 byte[] img = (byte[])dgridEmployees.CurrentRow.Cells[6].Value;
  23.                 if (img == null)
  24.                     pictureBox1.Image = null;
  25.                 else
  26.                 {
  27.                     MemoryStream ms = new MemoryStream(img);
  28.                     pictureBox1.Image = Image.FromStream(ms);
  29.                 }
  30.  
  31.             }
  32.  
Oct 1 '15 #1
6 2871
Luk3r
300 256MB
Just because the data from the datagridview is null does not mean that a picturebox property can be null. Try this instead:
Expand|Select|Wrap|Line Numbers
  1. pictureBox1.Image = Nothing;
Oct 1 '15 #2
The name "Nothing" does not exist .
Oct 1 '15 #3
Luk3r
300 256MB
My apologies. I thought I was in the VB.NET forum and misunderstood where the error was at. If I'm correct (given the lack of information), your code is actually erroring out when trying to convert a null datagridview value to a byte. I think your code should check the value of the cell as null instead of trying to convert the null value as a byte first. Hopefully something like this makes more sense:

Expand|Select|Wrap|Line Numbers
  1. if (dgridEmployees.CurrentRow.Cells[6].Value == null)
  2.   pictureBox1.Image = null;
  3. else
  4.   {
  5.     byte[] img = (byte[])dgridEmployees.CurrentRow.Cells[6].Value;
  6.     MemoryStream ms = new MemoryStream(img);
  7.     pictureBox1.Image = Image.FromStream(ms);
  8.   }
  9.  
Oct 1 '15 #4
Dear Luker,

Thanks for your help but I got the same error as before.

( Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.).
Oct 1 '15 #5
Luk3r
300 256MB
I tested your code and it fails. I tested my code and it works. Can you show me your code after modifying it per my suggestion?
Oct 1 '15 #6
Dear Luker,

Following code is suggested by my other friend whis works perfect, but i appriciate your help. Thanks for your help.

Expand|Select|Wrap|Line Numbers
  1. if (dgridEmployees.CurrentRow.Cells[6].Value != DBNull.Value)
  2.                 {
  3.                     byte[] img = (byte[])dgridEmployees.CurrentRow.Cells[6].Value;
  4.                     MemoryStream ms = new MemoryStream(img);
  5.                     pictureBox1.Image = Image.FromStream(ms);
  6.                 }
  7.  
  8.                 else
  9.                 {
  10.                     pictureBox1.Image = null;
  11.                 }
  12.  
Oct 2 '15 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Fred Geurtsen | last post by:
Question about updating a access Database Record! Normally i'm working with SQLserver, but i have to do this with access and it's not working. My error is: Operation must use an updateable query....
1
by: cc | last post by:
Hi, using the DataGridView (.NET 2005) : I have a row currently selected in the Grid (say the 3rd row showing 'Product3') Now, when I click on a column to sort the records is the 'Product3'...
0
by: russganz | last post by:
It seems to me there are real problems with the datagridview (along with the detailsview and form view) control when using optimistic concurrency with fields that can be null. Trying to use these...
4
by: zfarooq01 | last post by:
Asp Javascript language paging larger database record sets i am firmiliar with asp javascript and not asp VB. i can display the results ok, but if i return 100 records from my table i would...
2
by: JJ | last post by:
OK - I asked this earlier but thought I'd solved it. Clearly not. I have a control that I need to display text from an sql database. The control is being used as a web part so can be placed on a...
2
by: =?Utf-8?B?VG9ueSBBLg==?= | last post by:
Before saving information in a datagridview to a database I'm trying to verify tha certain cells are not blank (dbNull). When I run this code: If IsDBNull(Me.dgvHistory(2,...
4
by: captainB | last post by:
hi and thanks for reading. I am working on a web page that will display records from a database. Each record has a total of 8 columns. One column may be as long as 500 characters, and the other 7...
1
neo novice
by: neo novice | last post by:
hello all, im trying insert fields from a web page form into an access database. within dreamweaver, i inserted server behaviors from and from that tab in dreamweaver, by clicking insert record for...
12
by: M1kkelZU | last post by:
I'm trying to add a function now that lets the user open 1 or more files and import them to the database and dataGridView. Now the way it is now (should) work. But when it has finished with FILE1, it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.