Hi Everyone,
I have a problem that is quite frusturating.
I am passing in an image from a database, which is to be accessed in an image button. When I dynamically add the string for an sql command, the image is not found, and a red X appears in place of the image. However, when I use a static string, everything works and the image is there.
I have tested my code, and special characters is not the problem.
I posted my code below.
Thanks,
Austin
I have a problem that is quite frusturating.
I am passing in an image from a database, which is to be accessed in an image button. When I dynamically add the string for an sql command, the image is not found, and a red X appears in place of the image. However, when I use a static string, everything works and the image is there.
I have tested my code, and special characters is not the problem.
I posted my code below.
Thanks,
Austin
Code:
public void ProcessRequest(HttpContext context) { string image_name = context.Request.QueryString["image_name"]; string connString = @"Data Source=WOLVERINE;user id=sa;password=Osprey22;database=FileUpload";SqlConnection connection = new SqlConnection(connString); connection.Open(); StringBuilder sb = new StringBuilder();sb.AppendFormat("SELECT image_data FROM Available_Pics where image_name = '{0}'", image_name); [B]// SqlCommand command = new SqlCommand(sb.ToString(), connection); // The above SqlCommand fails even though sb.ToString() has an identical string to the one shown below that is // passed into the function. // Why would a string created dynamically not work, yet a static string like the one below work successfully ?[/B] SqlCommand command = new SqlCommand("SELECT image_data FROM Available_Pics where image_name = 'test'", connection);SqlDataReader dr = command.ExecuteReader(); dr.Read(); context.Response.BinaryWrite((Byte[])dr[0]); context.Response.ContentType = "image/png"; connection.Close(); context.Response.End(); }
Comment