Sunday, 11 August 2013

SQL table Logic

SQL table Logic

i have 2 tables--Images and Category
images-ImageID(incremental),Image,CategoryID.
Cayegory-CategoryID(PK),CategoryName.
the tables has a pk to fk relations (one category have many images). i
want to present all the images from the Images table that have a certain
categoryID to one icon that click on it will present the images related
the the page categoryID (i use PrettyPhoto). in this certain test exmple i
have 3 images in the category. now my problem is i get 3 icons (instad of
one). the first icon presents one image. the secound and the third icon
presents the other 2 images.
this is my aspx code :
<asp:DataList ID="datalistmytest" runat="server">
<ItemTemplate>
<div class="gallery clearfix">
<a href='<%# "test.ashx?ImID=" + Eval("CatagoryID")+ "&Is=" +
Eval("ImgSorting") %>'
rel='<%# "prettyphoto[" + Eval("CatagoryID") + "]" %>'>
<asp:ImageButton ID="ImageButton3" ImageUrl="images/btnMorePictures.png"
runat="server" />
</a>
</div>
</ItemTemplate>
</asp:DataList>
this is my code behaind:
public partial class myTest : System.Web.UI.Page
{
public string MyCatagoryID { get; set; }
string strcon =
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
MyCatagoryID = (Request["CatagoryID"]);
SqlConnection connection = new SqlConnection(strcon);
connection.Open();
//Query to get ImagesName and Description from database
SqlCommand command = new SqlCommand
("select * from Images where CatagoryID=" + MyCatagoryID,
connection);
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
datalistmytest.DataSource = dt;
datalistmytest.DataBind();
connection.Close();
}
}
this is the hendler:
public class test1 : IHttpHandler
{
string strcon =
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
public void ProcessRequest(HttpContext context)
{
string catagoryid = context.Request.QueryString["ImID"];
SqlConnection connection = new SqlConnection(strcon);
connection.Open();
SqlCommand command = new SqlCommand
("SELECT Image FROM Images WHERE CatagoryID =" + catagoryid,
connection);
SqlDataReader dr = command.ExecuteReader();
try
{
while (dr.Read())
{
context.Response.BinaryWrite((Byte[])dr[0]);
}
}
catch (Exception)
{
}
finally
{
// always call Close when done reading.
dr.Close();
// always call Close when done reading.
connection.Close();
context.Response.End();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
appritiate your help.

No comments:

Post a Comment