Click here to Skip to main content
15,921,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

Can anyone know. why the following error comes.

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


code:

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_company1 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"]);//error is here like "Unable to cast object of type 'System.String' to type 'System.Byte[]'"//
       dr.Close();
       cn.Close(); 
        
   }
Posted
Updated 9-Feb-12 18:46pm
v3
Comments
uspatel 10-Feb-12 0:54am    
Question Heading Changed by OP.

1 solution

use as
C#
if(dr.Read())
{
       //context.Response.ContentType = dr["imagename"].ToString();
       context.Response.BinaryWrite((byte[])dr["clogo"]);//error is here//
}
 
Share this answer
 
Comments
sathiyak 10-Feb-12 0:42am    
context.Response.BinaryWrite((byte[])dr["clogo"]);//here error comes like below:

Unable to cast object of type 'System.String' to type 'System.Byte[]'.
uspatel 10-Feb-12 0:53am    
These types are unrelated;See

http://www.codeproject.com/Questions/327497/Unable-to-cast-object-of-type-System-String-to-typ
sathiyak 10-Feb-12 0:57am    
these code are in vb.net...i need c# code..i dt know vb...
uspatel 10-Feb-12 0:55am    
check waht is your clogo type, is it image /binary
sathiyak 10-Feb-12 0:58am    
image type is varchar......do u the alternative code for below;
context.Response.BinaryWrite((byte[])dr["clogo"]);//here error comes like this//
if u know pls let me know...

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