Click here to Skip to main content
15,997,043 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Windows Forms
VS2013

I have a profile form and a profile edit form

on the profile form when it loads the image should be from the properties if the field in the db is NULL which at account creation it is.

However when it loads I get a error blow is the code for the form load

C#
mcon.Open();
          label9.Text = label10.Text;
           using (MySqlCommand cmd = mcon.CreateCommand())
           {
               cmd.CommandText = "SELECT * FROM users WHERE username='" + label10.Text + "'";
               using (MySqlDataReader rd = cmd.ExecuteReader())
               {
                   while (rd.Read())
                   {
                       lbluser.Text = rd.GetString("username");
                       lblname.Text = rd.GetString("full_name");
                       lblage.Text = rd.GetString("age");
                       lblcountry.Text = rd.GetString("country");
                       lblmartialstatus.Text = rd.GetString("martial_status");
                       lblgender.Text = rd.GetString("gender");
                       lblhobbies.Text = rd.GetString("hobbies");

                    byte[] imgg = (byte[])(rd["profile_pic"]);


                       if (imgg == null)
                       {
                           profilepic.Image = Properties.Resources._default;
                           path.Text = "Default Image";

                       }

                       else
                       {
                           MemoryStream mstream = new MemoryStream(imgg);
                           profilepic.Image = System.Drawing.Image.FromStream(mstream);
                       }




                   }

               }
           }



the line of code
C#
byte[] imgg = (byte[])(rd["profile_pic"]);
I get the error on and this happens the same way on the profile edit

If anyone has any ideas it would be great
Posted
Comments
Dave Kreskowiak 11-Oct-15 19:41pm    
...and the error message would be ......?? Hint: IMPORTANT!
[no name] 11-Oct-15 19:44pm    
An unhandled exception of type System.InvalidCastException occurred in client.exe
Unable to cast object of type system.DBNULL to type System.Byte

Well, then it's pretty obvious. A C# null is not the same as a DbNull coming from the database. You cannot cast that to anything in C#. You have to check to see if the value is DbNull BEFORE you attempt to convert the value to an array of bytes.
 
Share this answer
 
Comments
Dave Kreskowiak 11-Oct-15 20:17pm    
if (rd["profile_pic"] == DbNull)
I got it working

I change my code to

C#
if (rd["profile_pic"] == DBNull.Value)
                   {
                       profilepic.Image = Properties.Resources._default;
                       path.Text = "Default Image";

                   }

                      else
                      {
                          byte[] imgg = (byte[])(rd["profile_pic"]);
                       MemoryStream mstream = new MemoryStream(imgg);
                          profilepic.Image = System.Drawing.Image.FromStream(mstream);
                      }
 
Share this answer
 

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