Click here to Skip to main content
14,422,107 members
Rate this:
Please Sign up or sign in to vote.
See more:
Can Anyone reply how to rectify below error?



Error:

Cannot implicitly convert type 'string' to 'byte[]'


Error Coding:


if (uploadcollogo.HasFile == true)
      {
          try
          {
              //string fileName = Server.HtmlEncode(uploadcollogo.FileName);
                  System.Drawing.Image image_file = System.Drawing.Image.FromStream(uploadcollogo.PostedFile.InputStream);
                  int image_height = image_file.Height;
                  int image_width = image_file.Width;
                  int max_height = 120;
                  int max_width = 160;
                  image_height = (image_height * max_width) / image_width;
                  image_width = max_width;
                  if (image_height > max_height)
                  {
                      image_width = (image_width * image_height) / image_width;
                      image_width = max_width;
                  }
                  else
                  {
                  }
                  Bitmap bitmap_file = new Bitmap(image_file, image_width, image_height);
                  System.IO.MemoryStream stream = new System.IO.MemoryStream();
                  bitmap_file.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                  stream.Position = 0;
                  byte[] image = new byte[stream.Length + 1];
                  stream.Read(image, 0, image.Length);
                  image =  uploadcollogo.FileName.Substring(uploadcollogo.FileName.IndexOf('/') + 1);  //Error
                  Byte[] image1 = "~/collegelogo/" + image.ToString();
                  string status = "Active";
                  ////SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
                  SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["constring"].ToString());
                  cn.Open();
                  SqlCommand cmd = new SqlCommand();
                  cmd.CommandText = "pcolinsert";
                  cmd.CommandType = CommandType.StoredProcedure;
                  cmd.Parameters.AddWithValue("@colname", txtcolname.Text);
                  cmd.Parameters.AddWithValue("@colmobile1", txtcolmobile1.Text);
                  cmd.Parameters.AddWithValue("@colmobile2", txtcolmobile2.Text);
                  cmd.Parameters.AddWithValue("@collandline", txtcollandline.Text);
                  cmd.Parameters.AddWithValue("@collemail1", txtcolemail.Text);
                  cmd.Parameters.AddWithValue("@colaltemail", txtcolaltemail.Text);
                  cmd.Parameters.AddWithValue("@coladdress", txtcoladdr.Text);
                  cmd.Parameters.AddWithValue("@colwebsite", txtcolwebsite.Text);
                  cmd.Parameters.AddWithValue("@colcountry", ddlcountry.SelectedItem.Text);
                  cmd.Parameters.AddWithValue("@colstate", ddlstate.SelectedItem.Text);
                  cmd.Parameters.AddWithValue("@colcity", txtcolcity.Text);
                  cmd.Parameters.AddWithValue("@collandmark", txtcollandmark.Text);
                  cmd.Parameters.AddWithValue("@collogo", image1);


                  //SqlParameter UploadedImage = new SqlParameter("@collogo", SqlDbType.Image, image.Length);
                  //UploadedImage.Value = image;
                  //cmd.Parameters.Add(UploadedImage);

                  cmd.Parameters.AddWithValue("@status", status);
                  cmd.Connection = cn;
                  cmd.ExecuteNonQuery();
                  uploadcollogo.SaveAs(Server.MapPath(image1));
                  cn.Close();
                  clear();
                  Page.ClientScript.RegisterStartupScript(this.GetType(), "Inserted", "<script>alert('Record Inserted Sucessfuly')</script>");


          }

          catch (Exception ex)
          {
              lblerrmsg.Text = "Record Not Inserted Successfully";
          }
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

This is what it is: these two types are unrelated; you cannot do this assignment.

First of all, characters are not bytes, they are Unicode characters. Internally, the string is represented using Unicode UTF-16. In this encoding, a character is a 16-bit word or a pair of such words (called surrogate pair). Of course, a string can be serialized into an array of bytes, but the result depends on the encoding you choose.

For further considerations, please see my recent answer:
Unable to cast object of type 'System.String' to type 'System.Byte[]'. in VB.Net[^].

—SA
   
Comments
Abhinav S 10-Feb-12 1:20am
   
My 5.
   
Thank you, Abhinav.
--SA
Espen Harlinn 11-Feb-12 5:19am
   
5'ed!
   
Thank you, Espen.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 2

As the error suggests, you cannot convert one type to another.
Something like
byte[] dataB = System.Text.Encoding.Unicode.GetBytes(data);
this ought to help you.
   
Comments
   
You should have explained the ambiguity of encoding. The encoding "Unicode" is rarely used (except memory representation); and its name is a bad Microsoft tradition; it is actually Unicode UTF-16LE. I voted 4 this time.
--SA
Abhinav S 10-Feb-12 2:23am
   
Sure SA.
Espen Harlinn 11-Feb-12 5:20am
   
5'ed!
Abhinav S 11-Feb-12 8:40am
   
Thank you Espen.
Rate this:
Please Sign up or sign in to vote.

Solution 4

Hi,

You may see my article...

Display/Store and Retrieve Image Data from Database to Gridview


Happy coding...
   
Rate this:
Please Sign up or sign in to vote.

Solution 5

You seem to have confused yourself as to what you are trying to do.

byte[] image = new byte[stream.Length + 1];
stream.Read(image, 0, image.Length);

So at this point you have a byte array which contains the resized picture data that was uploaded by the user.

You then do this;
image =  uploadcollogo.FileName.Substring(uploadcollogo.FileName.IndexOf('/') + 1);  //Error

What is it you think you are attempting to do here?
What it looks like is you are wanting to get the name of the file without the path information if this is correct you need to create another variable which is a string such as fileName and look at Path.GetFileName[^] don't try to assign the name of the file to the byte array containing the data it will not work.
string fileName =  Path.GetFileName(uploadcollogo.FileName);


This bit has the same problem you are trying to assign a string to a byte array
Byte[] image1 = "~/collegelogo/" + image.ToString();

Try this instead.
string relativeFilePath = "~/collegelogo/" + fileName;
   
Rate this:
Please Sign up or sign in to vote.

Solution 6

may help you.

string strImageType = uxImageUpload.PostedFile.ContentType;

            //Reads the Image
            Stream ImageStream = uxImageUpload.PostedFile.InputStream;

            byte[] ImageContent = new byte[intImageSize + 1];
            ImageStream.Read(ImageContent, 0, intImageSize);
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100