Friday 23 August 2013

How to refresh Images which are present in database with Web Services in Asp.Net

How to refresh images which are present in database with Web Services in Asp.Net


Note: My concept in this code is we create a web services and with the help of this web services  we  want to show all data (images,links,name) one by one in another  website

We take  a web service in  our Website and create a web Method………………………………………

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

public WebService () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

                          
[WebMethod]
public void RefreshImage(ref int a, ref string b, ref string c, ref string d)
{
SqlConnection con = new SqlConnection(@"Data Source=KUSH-PC\KUSH;Initial Catalog=test;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select top 1 * from SiteImage order by NEWID()", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
a =(Int32) dr[0];
b = dr[1].ToString();
c = dr.GetString(2);
d= dr[3].ToString();

}
con.Close();

}

}

We take  another web Site and call above Web Services ………………………….
// code for Default.aspx.…………………………………..

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblimageid" runat="server" Text="Label"></asp:Label>
<asp:ImageButton ID="ImageButton1" runat="server" Height="69px" Width="64px" />
<br />
<asp:HyperLink ID="HyperLink1" runat="server">HyperLink</asp:HyperLink>
<asp:Timer ID="Timer1" runat="server" Interval="1000" Enabled="true">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>

</div>
</form>
</body>
</html>

// code for Default.aspx.cs…………………………………..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int a = 0;
string b = ""; string c = ""; string d = "";
localhost.WebService ww = new localhost.WebService();
ww.RefreshImage(ref a,ref b, ref c, ref d);
lblimageid.Text = a.ToString();
HyperLink1.Text = b;
HyperLink1.NavigateUrl = c;
HyperLink1.Target = "_blank";
ImageButton1.ImageUrl = "~/SiteImage/" + d;
ImageButton1.PostBackUrl = b;
ImageButton1.ToolTip = c;
}
}



Output: Images which are saving in our database are show in our output one by one within a difference of one second

select top 1 * from SiteImage order by NEWID()


0 comments:

Post a Comment