Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted
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.
Posted

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?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...