Click here to Skip to main content
15,887,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For everyone, solve this problem, i am new in c#,
I made one desktop app, my problem is i failed to retrieve image value from database of OLEDB like MS Access.

In code i tried the textbox_keydown event that with database;

there are 5 textbox and one picturebox for image.

In textbox of barcode, i scan or enter barcode number and then fetch result from database of MS Access when i keydown that textbox.

What is my mistake?,and what to do resolve it?

The error is generated at img = (byte[])dr[5]; . the code as follow for that event.

C#
private void txtbarcode_KeyDown(object sender, KeyEventArgs e)
       {
           if (e.KeyCode == Keys.Enter)
           {
               OleDbCommand cm = new OleDbCommand("select * from stock_tab where Barcode = @Barcode",cn);
               cm.Parameters.Add(new OleDbParameter("@Barcode", txtbarcode.Text));
               cn.Open();
               OleDbDataReader dr = cm.ExecuteReader();
               while(dr.Read()){
                   txtpnm.Text = dr.GetString(1);
                   txtdesc.Text = dr.GetString(2);
                   txtqty.Text = Convert.ToString(dr.GetInt32(3));
                   txtprc.Text = Convert.ToString(dr.GetInt32(4));
                   MemoryStream m = new MemoryStream();
                   //pbproduct.Image.Save(m, pbproduct.Image.RawFormat);
                   byte[] img = (byte[])dr[5];
                   MemoryStream ms1 = new MemoryStream(img);
                   pbproduct.Image = Image.FromStream(ms1);
               }
               dr.Close();
               cn.Close();
           }
       }


What I have tried:

I tried for memory allocation of image for retrieve values from db, but error: can't cast system.DBNull type to System.Byte[]; this is error generated at: img = (byte[])dr[5];
Posted
Updated 17-Jun-16 1:38am

1 solution

The error is pretty clear:
can't cast system.DBNull type to System.Byte[];
The value returned from your database isn't data - it's a null. That means that either you didn;t insert an image in that column when you saved the row, or it's the wrong column.
Given that you are using SELECT * FROM and then accessing your columns numerically starting from 1, that's the most likely.
Never use SELECT * - always list the columns you want. That way, changes in the DB column order don't make your app crash, and you don;t fecth information you don;t need.
So try:
SQL
SELECT PNM, Desc, Qty, PRC, Picture FROM Stock_tab ...

And use the name of the column to access the DataReader:
C#
if (dr.Read())
    {
    txtpnm.Text = (string) dr["PNM"];
    txtdesc.Text = (string) dr["Desc"];
    txtqty.Text = dr["Qty"].ToString();
    txtprc.Text = dr.["PRC"].ToString();
    byte[] img = (byte[])dr["Picture"];
    MemoryStream ms1 = new MemoryStream(img);
    pbproduct.Image = Image.FromStream(ms1);
    }
 
Share this answer
 
Comments
Member 11572517 17-Jun-16 7:48am    
thank u sir, i'll try to change according you.
Member 11572517 17-Jun-16 8:02am    
Sir, according u i changed it, but the same error is generated at Memorystream ms1 = new memorystream(img);, Will error is regard of image to store in db and retrieve from it?
OriginalGriff 17-Jun-16 8:22am    
What is the exact error message?
Member 11572517 17-Jun-16 10:49am    
extremely sorry sir, here is holy ramazan month so, that i couldn't answered it; The error: InvalidCastException was unhandeled:
Unable to cast object of type "System.DBNull" to type "Syste.byte[]".
once again extremely sorry sir..
OriginalGriff 17-Jun-16 11:09am    
That's ok - no problem.
The error message means that the value coming back from your DB is NULL - there is no image information in that field of that row.
So start there: check your DB and your insert code to find out why your DB contains nothing when you expect it to have data.
If it doesn't matter that there is no image, then compare the DB value to DBNull.Value before you try to cast it, and set a default image instead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900