Add images to slideshow using database table Part 13804:33

  • 0
Published on October 25, 2017

Text version of the video

Slides

All ASP .NET Text Articles

All ASP .NET Slides

ASP.NET Playlist

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

In Part 137, we discussed storing the image data in an xml file. In this video, we will be using a database table to store image data. So, we can safely delete ImageData.xml file. Please watch Part 137, before proceeding.

To store image data, create table tblImages.
Create table tblImages
(
[ID] int identity primary key,
[Name] nvarchar(50),
[Order] int
)

SQL script to insert image data
Insert into tblImages values(‘Chrysanthemum.jpg’,1)
Insert into tblImages values(‘Desert.jpg’,2)
Insert into tblImages values(‘Hydrangeas.jpg’,3)
Insert into tblImages values(‘Jellyfish.jpg’,4)
Insert into tblImages values(‘Koala.jpg’,5)
Insert into tblImages values(‘Lighthouse.jpg’,6)
Insert into tblImages values(‘Penguins.jpg’,7)
Insert into tblImages values(‘Tulips.jpg’,8)
Insert into tblImages values(‘MyImage.jpg’,9)

stored procedure to retrieve image data
Create procedure spGetImageData
as
Begin
Select [Name], [Order] from tblImages
End

After the table is created, create a connection string in web.config.
[connectionStrings]
[add name=”DBCS”
connectionString=”data source=.;Integrated Security=SSPI;database=Sample”
providerName=”System.Data.SqlClient” /]
[/connectionStrings]

We now have to write ADO.NET code to retrieve image data from the database table. The rest of the logic remains unchanged. Here’s the complete code for your reference.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace ImageSlideShow
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetImageUrl();
}
}

protected void Timer1_Tick(object sender, EventArgs e)
{
int i = (int)ViewState[“ImageDisplayed”];
i = i + 1;
ViewState[“ImageDisplayed”] = i;

DataRow imageDataRow = ((DataSet)ViewState[“ImageData”]).Tables[“image”].Select().FirstOrDefault(x =] x[“order”].ToString() == i.ToString());
if (imageDataRow != null)
{
Image1.ImageUrl = “~/Images/” + imageDataRow[“name”].ToString();
lblImageName.Text = imageDataRow[“name”].ToString();
lblImageOrder.Text = imageDataRow[“order”].ToString();
}
else
{
SetImageUrl();
}
}

private void SetImageUrl()
{
DataSet ds = new DataSet();
string CS = ConfigurationManager.ConnectionStrings[“DBCS”].ConnectionString;
SqlConnection con = new SqlConnection(CS);
SqlDataAdapter da = new SqlDataAdapter(“spGetImageData”, con);
da.Fill(ds, “image”);
ViewState[“ImageData”] = ds;
ViewState[“ImageDisplayed”] = 1;

DataRow imageDataRow = ds.Tables[“image”].Select().FirstOrDefault(x =] x[“order”].ToString() == “1”);
Image1.ImageUrl = “~/Images/” + imageDataRow[“name”].ToString();
lblImageName.Text = imageDataRow[“name”].ToString();
lblImageOrder.Text = imageDataRow[“order”].ToString();
}

protected void Button1_Click(object sender, EventArgs e)
{
if (Timer1.Enabled)
{
Timer1.Enabled = false;
Button1.Text = “Start Slideshow”;
}
else
{
Timer1.Enabled = true;
Button1.Text = “Stop Slideshow”;
}
}
}
}

To add a new image to the slideshow
1. Copy the image to the images folder
2. Insert the new image name and it’s order into tblImages table.

Enjoyed this video?
"No Thanks. Please Close This Box!"