Fetch the image url from SQL Server database to display an image - asp.net

I have a column in my database that stores the url of the user's profile picture. In the home page' page_load event handler, I need to determine the image url, but I don't know how to do that using SQL commands.
From the images below, how can I give the query result to the image's ImageUrl property?

You can use Server.mappath("~/profilepicture/profilepicture.jpg")
OdbcDataReader reader=new OdbcDataReader();
reader=command.executereader;
Profilephotobutton.imageur=server.mappath(" & reader.item(profilepicture) & ");
hope this helps

Related

Display an image from a Byte Array from SQL into an ASPX page that is currently routed

I have the most convoluted and headache of a way to attempt to load am image on a page. I am using VB.Net, my page is routed from a Routes.XML document, I have a SQL database with images stored as varbinary(MAX) fields, I need to load said images from the page into an control, and everything that i use to use will not work with the routing.
If you need to see code examples let me know, but I was wondering if anyone knew of a way to display an image from bytes in this fashion.
use this code:
Dim Ph As Byte()
Ph = DirectCast(YourImageFromDB), Byte())
Dim img As Image = Nothing
Dim stream = New MemoryStream(Ph)
img = Image.FromStream(stream)
Ended up having a friend show me how he routed with images. What basically happens is that I needed to create an ASHX file to create the image, and put the full path from the image.ashx that I create as the URL I need. Worked like a charm. First time ever working with a handler file so i had no idea how to use it at first.

Set a string to a fileupload as it's filename

i have an update form for some of my data, if user entered an url for my fileupload in database,
i want to show this url in fileupload as it's filename in my update form,
actually i want have a code like this :
FileUpload1.FileName = "My Path";
of course i know fileName in a readOnly property,
could u please help me?
Its a bad idea since user might not have file in same location or he might have completely deleted it or he may browsing from other machine that results in invalid functionality. However, the file upload value can be set from javascript. If you really intend to do that, store the path in hidden field and in body onload event set the fileupload value to the hidden field value.

Sending data between asp.net page & Popup page?

I have a form where user selects 'fruit's from a popup form. I am using javascript to open the popup and query string to pass the text control id & hidden field id to the popup window.
How can I securely passed these parameters ? I am thinking about using Session but that would require a postback.
Edit
You should try passing parameter using query string.. but try using Encrypted Querystring values to secure your data.. http://www.codeproject.com/KB/aspnet/urlquerystring-encryption.aspx
you can use any of following methods.
Use a query string.
Get HTTP POST information from the source page.
Use session state.
Create public properties in the source page and access the property
values in the target page.
Get control information in the target page from controls in the
source page.
for more information,
http://msdn.microsoft.com/en-us/library/6c3yckfw.aspx

Image handler page

I created an image handler page which retrieves the physical path of an image on the local machine and then using filestream, resizes and displays it - using a integer (record id) passed as querystring.
What is happening is that, when the routine in pageload cannot find an image relating to the record id, it displays random images (from other records).
This only ocurrs when the related record id has no image. The routine assigns a default image if no image exists for the record, but instead of displaying the default image, the page is displaying images from other records. If I keep refreshing the page it displays different images from other records.
In my page load event, before doing anything else I have put:
Response.Cache.SetCacheability(HttpCacheability.NoCache)
But this has not changed the behaviour.
Any help appreciated.
thanks,
KS
You could handle the case in code so when no image is present, you display a static blank image instead.

Display a Photo Gallery using Asp.Net and SQL

I have recently added photos to my SQL database and have displayed them on an *.aspx page using Asp:Image. The ImageUrl for this control stored in a separate *.aspx page. It works great for profile pictures.
I have a new issue at hand. I need each user to be able to have their own photo gallery page. I want the photos to be stored in the sql database. Storing the photos is not difficult. The issue is displaying the photos. I want the photos to be stored in a thumbnail grid fashion, when the user clicks on the photo, it should bring up the photo on a separate page.
What is the best way to do this. Obviously it is not to use Asp:Image. I am curious if I should use a Gridview. If so, how do I do that and should their be a thumbnail size stored in the database for this?
Once the picture is click on how does the other page look so that it displays the correct image. I would think it is not correct to send the photoId through the url.
Below is code from the page I use to display profile pictures.
protected void Page_Load(object sender, EventArgs e)
{
string sql = "SELECT [ProfileImage] FROM [UserProfile] WHERE [UserId] = '" + User.Identity.Name.ToString() + "'";
string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["SocialSiteConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(strCon);
SqlCommand comm = new SqlCommand(sql, conn);
conn.Open();
Response.ContentType = "image/jpeg";
Response.BinaryWrite((byte[])comm.ExecuteScalar());
conn.Close();
}
"The ImageUrl for this control stored in a separate *.aspx page. It works great for profile pictures." - rather than an .aspx page, why not a generic ASP.NET handler (.ashx) instead? They are a little more lightweight than an .aspx. Just search "ASP.NET Generic Handler" and you'll find a number of samples. It's basically the code you have now behind your .aspx page, but without all the page initialization/rendering overhead.
"I want the photos to be stored in a thumbnail grid fashion, when the user clicks on the photo, it should bring up the photo on a separate page." - I would think any ASP.NET repeatable control that supports templating of the item element (such as DataGrid, GridView, Repeater, ListView in ASP.NET 3.5, etc) should do the trick for you here. Just set the image or asp:Image height and width as appropriate for your thumbnails. Wrap which ever tag you use in an HTML anchor with an href to your page that displays the image at "full size".
Let's deconstruct your question:
Should their be a thumbnail size stored in the database for this?
Yes. Generate the thumbnail the first time the photo thumbnail is requested and cache it in the DB for future access
Obviously it is not to use Asp:Image
There is no problem using Asp:Image. You will be fine with it
I am curious if I should use a Gridview
Maybe a Repeater is better, but you will be fine with a gridview if you are familiar with it
I would think it is not correct to send the photoId through the url.
It is correct, but you should check if the photo belongs to the current user (don't trust the URL.
Generating the thumbnail
You will learn that the resized image generated by .net are poor quality. You should use some GDI kung-fu to get quality pictures.
Refer to this post http://codebetter.com/blogs/brendan.tompkins/archive/2004/01/26/use-gdi-to-save-crystal-clear-gif-images-with-net.aspx to learn more
You can try this
or this. They should get you started on the grid side. Correct on sending the imageid from the url. Look at a session variable or hidden control and post the form and read in on the next page.
I would look at a gridview and use a template column in combination with the html image tag in the gridview . Create the thumbnail as they upload it and store them both in the database. From there, you can easily retrieve it. This way displaying the image is quicker and there is no thumbnail conversions necessary at display time.
Well you would have a page for just displaying a photo (like you have above) but your criteria would be different. You would not use WHERE [UserId] = '" + User.Identity.Name but would have to send a query string id for the id of the photo in the database.
Then when you want the pic in your aspx page you would have to put an image html tag <image src="photo.aspx?id=2" />
How you put the html image tags on the page is not important.
The RbmBinaryImage control will help you display images directly from your database. You could bind the Image field directly to the ImageContent property, also you could specify whether you want the display to be as a thumbnail or not and provide the thumbnail size.
to get it go to http://www.ramymostafa.com/?p=187
If you don't want to reinvent the wheel you can integrate an open source gallery like Gallery Server Pro (www.galleryserverpro.com) into your site. It is written in C# / ASP.NET and does all the things you want.
Roger
[disclaimer] I am the Creator and Lead Developer of Gallery Server Pro [/disclaimer]

Resources