Zulfikar Bakır
Zulfikar Bakır

Reputation: 5

How to fetch images from database dynamically?

I am trying to fetch images from my database, i can fetch images manually one by one. The code below is asp.net and its C#:

  <asp:Image ID="img1" runat="server" />
            <asp:Image ID="img2" runat="server" />
            <asp:Image ID="img3" runat="server" />
            <asp:Image ID="Image3" runat="server" />
            <asp:Image ID="img4" runat="server" />
            <asp:Image ID="img5" runat="server" />
            <asp:Image ID="img6" runat="server" />
            <asp:Image ID="Image7" runat="server" />
            <asp:Image ID="Image8" runat="server" />
            <asp:Image ID="Image9" runat="server" />


    dr.Read();
    img1.ImageUrl = "~/Resimler/800/" + dr[2].ToString();
    dr.Read();
    img2.ImageUrl = "~/Resimler/800/" + dr[2].ToString();
    dr.Read();
    img3.ImageUrl = "~/Resimler/800/" + dr[2].ToString();
    dr.Read();
    img4.ImageUrl = "~/Resimler/800/" + dr[2].ToString();
    dr.Read();
    img5.ImageUrl = "~/Resimler/800/" + dr[2].ToString();
    dr.Read();
    img6.ImageUrl = "~/Resimler/800/" + dr[2].ToString();

So i want to fetch images dynamically.

Upvotes: 0

Views: 129

Answers (1)

Aycan Yaşıt
Aycan Yaşıt

Reputation: 2104

After you've bound dr to your images table, you may loop through images and set these images to your Image components.

//We are creating a List to contain our Image components
List<Image> ArrayImages = new List<Image>();
//Insert Image components into array
ArrayImages.Add(img1);
ArrayImages.Add(img2);
ArrayImages.Add(img3);
//TODO: Add other Image components in array

int counter = 0; //We will use this counter to identify array index
while(dr.Read()){
  //Assign image url to Image component in our array's current index
  ArrayImages[counter].ImageUrl = "~/Resimler/800/" + dr[2].ToString();
  //Implementing counter for next index in our Image array
  counter++;
}

Upvotes: 1

Related Questions