shahab Posted January 11, 2004 Posted January 11, 2004 I had wrote a program that I upload images and show them in another pages(MS SQL Server2000). Now when I want to show them I want to show them in an Image control but I have this code!:D What should I do? try { //get the image id from the url string ImageId = Request.QueryString["img"];//ImageId //build our query statement string sqlText = "SELECT img_data, img_contenttype FROM Image WHERE img_pk =1 "; SqlConnection connection = new SqlConnection( ConfigurationSettings.AppSettings["ConnStr"].ToString() ); SqlCommand command = new SqlCommand( sqlText, connection); //open the database and get a datareader connection.Open(); SqlDataReader dr = command.ExecuteReader(); if ( dr.Read()) //yup we found our image { Response.ContentType = dr["img_contenttype"].ToString(); Response.BinaryWrite( (byte[]) dr["img_data"] ); //Image1.ImageUrl=? } connection.Close(); } catch (Exception e01) { //string ImageId = Request.QueryString["img"]; Response.Write("<br>"+e01.Message.ToString()); } Instead of response.? Thanks Quote
bungpeng Posted January 12, 2004 Posted January 12, 2004 I don't think the "Response.BinaryWrite" allow you to write the Image to client (where the Image file physically store at?). Normally after you retrieve your image from database, you need to save it in your web server virtual directory, then your "Image1.ImageUrl" can point to that directory. Quote
Moderators Robby Posted January 12, 2004 Moderators Posted January 12, 2004 This should help....http://aspalliance.com/141 He's displaying in a datagrid but it's pretty much the same thing. Quote Visit...Bassic Software
shahab Posted January 12, 2004 Author Posted January 12, 2004 I read the article and convert the codes to c# : public string FormatURL(string strArgument) { return ("News.aspx?id=" + strArgument); } private void Page_Load(object sender, System.EventArgs e) { try { //get the image id from the url string ImageId = Request.QueryString["img"];//ImageId //build our query statement string sqlText = "SELECT img_data, img_contenttype FROM Image WHERE img_pk = "+ImageId; SqlConnection connection = new SqlConnection( ConfigurationSettings.AppSettings["ConnStr"].ToString() ); SqlCommand command = new SqlCommand( sqlText, connection); //open the database and get a datareader connection.Open(); SqlDataReader dr = command.ExecuteReader(); if ( dr.Read()) //yup we found our image { Response.ContentType = dr["img_contenttype"].ToString(); Response.BinaryWrite( (byte[]) dr["img_data"] ); //Image1.ImageUrl=? } connection.Close(); } catch (Exception e01) { //string ImageId = Request.QueryString["img"]; Response.Write("<br>"+e01.Message.ToString()); } But : Server Error in '/edu/nahie' Application. -------------------------------------------------------------------------------- Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS1502: The best overloaded method match for 'nahie._Default.FormatURL(string)' has some invalid arguments Source Error: Line 116: <asp:TemplateColumn HeaderText="Image"> Line 117: <ItemTemplate> Line 118: <asp:Image id="Image1" ImageUrl='<%# FormatURL(DataBinder.Eval(Container.DataItem, "img_pk")) %>' runat="server"> Line 119: </asp:Image> Line 120: What is wrong? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.