Click here to Skip to main content
15,921,351 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

Can anyone know?

Unable to cast object of type 'System.String' to type 'System.Byte[]'.how to solve this error?

this is the code...i have insert image and all things but it shows the above error....help me..
C#
public void ProcessRequest (HttpContext context)
   {
       //SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
       SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["constring"].ToString());
       cn.Open();

       string sql = "Select clogo from tbl_company where cid=@cid";
       SqlCommand cmd = new SqlCommand(sql, cn);
       cmd.Parameters.Add("@cid", SqlDbType.Int).Value = context.Request.QueryString["cid"];
       cmd.Prepare();
       SqlDataReader dr = cmd.ExecuteReader();
       dr.Read();
       //context.Response.ContentType = dr["imagename"].ToString();
       context.Response.BinaryWrite((byte[])dr["clogo"]);
       dr.Close();
       cn.Close();

   }
Posted
Updated 6-Feb-12 23:16pm
v3
Comments
CPallini 7-Feb-12 5:22am    
What is the 'clogo' column type (why do you want to write it into a binary file?)?
sathiyak 7-Feb-12 6:29am    
clogo is an image name
sathiyak 9-Feb-12 5:46am    
clogo is imagename its datatype is image....and it stored as binary data in db..but when i upload image it shows error like"Failed to convert parameter value from a String to a Byte[]".how to solve this error?

Of course you cannot cast a string object into an array of bytes. You should tell us what is the purpose of such a cast in order to get a better help.
 
Share this answer
 
Comments
sathiyak 9-Feb-12 5:53am    
there is any wayfor save the string image as byte images...

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