Add images to slideshow using database table Part 138

  • دعنا نشارك هذا :
ترتيب الدرس : 138
المدة : 00:07:25
التحكم :
Text version of the video
http://csharp-video-tutorials.blogspot.com/2013/07/add-images-to-slideshow-using-database.html

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation1

Slides
http://csharp-video-tutorials.blogspot.com/2013/08/part-138-add-images-to-slideshow-using.html

All ASP .NET Text Articles
http://csharp-video-tutorials.blogspot.com/p/free-aspnet-video-tutorial.html

All ASP .NET Slides
http://csharp-video-tutorials.blogspot.com/p/aspnet-slides.html

ASP.NET Playlist
https://www.youtube.com/playlist?listPL4cyC4G0M1RQcB4IYS_zwkyBwMyx5AnDM

All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view1&sortdd

All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists

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 SecuritySSPI;databaseSample"
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"];
ii + 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.TextimageDataRow["name"].ToString();
lblImageOrder.TextimageDataRow["order"].ToString();
}
else
{
SetImageUrl();
}
}

private void SetImageUrl()
{
DataSet dsnew DataSet();
string CSConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection connew SqlConnection(CS);
SqlDataAdapter danew SqlDataAdapter("spGetImageData", con);
da.Fill(ds, "image");
ViewState["ImageData"]ds;
ViewState["ImageDisplayed"]1;

DataRow imageDataRowds.Tables["image"].Select().FirstOrDefault(x ] x["order"].ToString()"1");
Image1.ImageUrl"~/Images/" + imageDataRow["name"].ToString();
lblImageName.TextimageDataRow["name"].ToString();
lblImageOrder.TextimageDataRow["order"].ToString();
}

protected void Button1_Click(object sender, EventArgs e)
{
if (Timer1.Enabled)
{
Timer1.Enabledfalse;
Button1.Text"Start Slideshow";
}
else
{
Timer1.Enabledtrue;
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.

سيتم تفعيل التعليقات قريبا

y
Sep 23, 2020 07:32:26
youtube