2014年7月13日 星期日

[RESOLVED] Order gallery images as they are uploaded


I am using facebox on my asp.net web site for my image gallery. when i am uploading images to the gallery, they are saved on my disc and url data is stored in my sql database. After the uploading, my gallery displays thumbnails from the images but not in
the order as they are uploaded. I want to display the last uploaded image as first in the gallery (order by last uploaded), but i don't know what i should add in the code.


This is the code:







Cs code:


protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{

BindDataList();


}

}
protected void BindDataList()
{
String strConnString = System.Configuration.ConfigurationManager
.ConnectionStrings["makbazaConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
if (Request.QueryString["ID"] == "Znamenitosti")
{
//Query to get ImagesName and Description from database

SqlCommand command = new SqlCommand("SELECT ime, imethumb, imeslika, kategorija, datum, opis, slikapateka, thumbpateka, userid FROM Ohrid WHERE kategorija='Znamenitosti' AND grad='Ohrid' ", con);
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dlImages.DataSource = dt;
dlImages.DataBind();
}
.
.
.
.
con.Close();
}









If you use an integer for a primary key, just add


order by [fieldname] desc


to the end of your sql query to show in the order added with newest first (or change desc to asc if you want first added to be first and the most recent one to be last in the list).[fieldname] is the name of the primary key. If you have a field that is a
datetime of when they were added, use that. Otherwise you need to have some field that tells you the order they occur. The database will have no knowledge otherwise since it relies on primary keys for that. In the case of a GUID for a primary key you have
to use another time-sensitive identifier such as a date.



SqlCommand command = new SqlCommand( "SELECT ime, imethumb, imeslika, kategorija, datum, opis, slikapateka, thumbpateka, userid FROM Ohrid WHERE kategorija='Znamenitosti' AND grad='Ohrid' order by id desc" , con);






沒有留言:

張貼留言