Question:
asp.net, sql HELP!!!!?
dsarf f
2009-02-10 15:07:55 UTC
Ok, I asked a question here in yahoo answers, asking how I can store images in a database, someone was very nice, and gave me the code:

protected void ButtonUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
HttpPostedFile _HPFimagen = FileUpload1.PostedFile;
Byte[] _byteImagen = new Byte[_HPFimagen.ContentLength];
_HPFimagen.InputStream.Read(_byteImagen, 0, FileUpload1.PostedFile.ContentLength);
string stringConnection = "The ConnectionString";
string commandText = "Insert into Table (value)"
+ "values (@Image)";
SqlConnection connection = null;
try
{
connection = new SqlConnection(stringConnection);
connection.Open();
SqlCommand command = new SqlCommand(commandText, connection);
SqlParameter sqlParameter = new SqlParameter("@Imagen", System.Data.SqlDbType.Image);
sqlParameter.Value = _byteImagen;
command.Parameters.Add(sqlParameter);
command.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (connection != null)
connection.Close();
}

}
}

and you Page.aspx have two controls




I replaced the connectionString and etc and this code makes sence to me. I don't know if it works, but when i go to my database, it says and if that is good then how do i retrieve it, I came up with this:
protected void Button1_Click(object sender, EventArgs e)
{
string a;
string connectionString = "connection string";
SqlConnection myConnection = new SqlConnection(connectionString);
string strgCommand = "SELECT Image FROM Users WHERE uID='alsdf'";


SqlCommand myCommand = new SqlCommand(strgCommand, myConnection);
myConnection.Open();
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{

a = Convert.ToString(myReader["Image"]);
}
myReader.Close();
myConnection.Close();
Image1.ImageUrl = a;
}
but it gives me a few errors, please help, I know that my conversions are bad, but I don't know how to fix it. Thank you.
Four answers:
2009-02-10 15:15:11 UTC
The concept is bad. Store the images as files on disk. Store the path and filename in the database.
goit
2009-02-10 23:41:29 UTC
Following your code, what you need to do is the following:

1 - When getting the binary image from database you have to cast that into Byte[] (byte array) something similar to

byte[] valueFromDb = (byte[])myReader["Image"];



2 - If you want to display that image, you need to save into response output stream: how you do it: (similar to this code)



System.Web.HttpContext.Current.Response.ContentType = "image/jpeg";

System.Drawing.Image image = System.Drawing.Image.FromStream(valueFromDb);

image.Save( System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg );





Note your image format may matter. But this uses the response output stream so may not be as you wanted:



If that is the case you have two options that I can think of:

1 - Initially when you save image file just save it in a file system and store in the database the file path. Then you can just construct a url when displaying it to assign that url to your image control ImageUrl property.



2 - If you follow your current approach, after getting the image from database save it in temporary file location and construct url and assign that url to your image control ImageUrl property.



[It trancates some of the text, here is the full text, please follow the order]

[Current...] -> [Current.Response.ContentType ]

[FromStrea...] -> [FromStream(valueFromDb);]

[Current...] -> [.rent.Response.OutputStream]

[ImageFo...] -> [ImageFormat.Jpeg]
schiz0phren!c.raJiv
2009-02-10 23:14:42 UTC
hello, i cant answer your question but i really need to ask something about SQL... my teacher gave this problem to me, and i dont know how to answer it.. it seems that you are good in SQL,..



how can i make a query wherein the last names ending with a is displayed on the table?







https://answersrip.com/question/index?qid=20090210150624AA8IvNK
2009-02-10 23:14:17 UTC
i would Try it on a Diff computer


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...