Monday, August 29, 2011

News Scroll in asp.net Using Marquee


1) Drag A Repeater from  ToolBox into NewsScroller.aspx Design Surface
2) Create Table News  with Columns as id int,URL varchar(50),Description varchar(50)
3) go to solution explorer--> right Click--> Add New Item--> add Class file-->Name it as connection.cs

in connection.cs write as
===========================================================

using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for Connection
/// </summary>
public class Connection
{
public SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
public SqlCommand cmd = new SqlCommand();
public SqlDataReader dr;
public SqlDataAdapter da = new SqlDataAdapter();
public DataSet ds = new DataSet();
}






Write in Html as
=========================================================================
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NewsReelMarquee.aspx.cs" Inherits="NewsReelMarquee" %>

<!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">
<div>
<marquee id="ml" style="text-align: center" direction="up" width="195" height="170"
scrolldelay="20" scrollamount="1">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("URL") %>'>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Description") %>'></asp:Label></asp:HyperLink><br />
</ItemTemplate>
</asp:Repeater>
</marquee>
</div>
</form>
</body>
</html>


In News Scroll.aspx.cs page Write as
======================================================


public partial class NewsReelMarquee : System.Web.UI.Page
{
Connection obj = new Connection();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Repeater1.DataSource = newsscroll();
Repeater1.DataBind();
}

}
public DataSet newsscroll()
{
obj.con.Close();
obj.con.Open();
obj.cmd = new SqlCommand("select url,description from news ",obj.con);
obj.da = new SqlDataAdapter(obj.cmd);
obj.da.Fill(obj.ds, "sdfasd");
return obj.ds;
}





Reference:

Insert Images Into Data Base



For table Creation and Connection.cs class File Refer Previous Post

In Default.aspx.cs file
--------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
public partial class UI_ImagesUpload : System.Web.UI.Page
{
DashBoard obj = new DashBoard();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridData();
}
}
protected void btn_SaveImage_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
//getting length of uploaded file
int length = FileUpload1.PostedFile.ContentLength;
//create a byte array to store the binary image data
byte[] imgbyte = new byte[length];
//store the currently selected file in memeory
HttpPostedFile img = FileUpload1.PostedFile;
//set the binary data
img.InputStream.Read(imgbyte, 0, length);
string productName = txt_ImageName.Text;
obj.con.Close();
obj.con.Open();
obj.cmd = new SqlCommand("INSERT INTO tbl_ModuleImages (ImageName,Image) VALUES (@imagename,@imagedata)", obj.con);
obj.cmd.Parameters.Add("@imagename", SqlDbType.VarChar, 50).Value = txt_ImageName.Text;
obj.cmd.Parameters.Add("@imagedata", SqlDbType.Image).Value = imgbyte;
int count = obj.cmd.ExecuteNonQuery();
if (count == 1)
{
BindGridData();
txt_ImageName.Text = string.Empty;
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + txt_ImageName.Text + " image inserted successfully')", true);
}
else
{
lblMessage.Text = "Please Select Product Image File";
}
}


}
private void BindGridData()
{
//Connection obj = new Connection();
obj.con.Close();
obj.con.Open();
obj.cmd = new SqlCommand("Select * from tbl_ModuleImages", obj.con);
obj.da = new SqlDataAdapter(obj.cmd);
obj.da.Fill(obj.ds, "asdd");
DataList1.DataSource = obj.ds;
DataList1.DataBind();
obj.con.Close();
}
protected void btn_UpdateImage_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile == true)
{
int imagelength = FileUpload1.PostedFile.ContentLength;
Byte[] imagebytes = new byte[imagelength];
HttpPostedFile image = FileUpload1.PostedFile;
image.InputStream.Read(imagebytes, 0, imagelength);

string ModuleId=txt_ImageName.Text;
obj.con.Close();
obj.con.Open();
obj.cmd = new SqlCommand("Update tbl_Modules set ModuleImage=@imagedata where Moduleid='"+ModuleId+"'",obj.con);
obj.cmd.Parameters.Add("@ModuleID", SqlDbType.VarChar, 50).Value = txt_ImageName.Text;
obj.cmd.Parameters.Add("@imagedata", SqlDbType.Image).Value = imagebytes;
obj.cmd.ExecuteNonQuery();
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + txt_ImageName.Text + " Image Updated successfully')", true);
txt_ImageName.Text = "";
obj.con.Close();
}
}
}



in Handler.ashx
=================================================

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;


public class Handler : IHttpHandler
{
Connection obj = new Connection();
public void ProcessRequest (HttpContext context)
{
obj.con.Close();
obj.con.Open();
obj.cmd = new SqlCommand("Select ImageName,Image from tbl_ModuleImages where ImageID =@ImageID",obj.con);
obj.cmd.CommandType = CommandType.Text;
SqlParameter ImageID = new SqlParameter("@ImageID", System.Data.SqlDbType.Int);
ImageID.Value = context.Request.QueryString["ImageID"];
obj.cmd.Parameters.Add(ImageID);
obj.dr=obj.cmd.ExecuteReader();
obj.dr.Read();
context.Response.BinaryWrite((Byte[])obj.dr["Image"]);
obj.dr.Close();
obj.con.Close();
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}
}


------------------------


In Default .aspx HTML Source code


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


<!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>Image UpLoads</title>
<style type="text/css">
.style1
{
width: 100%;
float: left;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<table class="style1">
<tr>
<td>
ImageName</td>
<td>
<asp:TextBox ID="txt_ImageName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Select Image</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
<asp:Button ID="btn_SaveImage" runat="server" onclick="btn_SaveImage_Click"
Text="Save & Display" />
<asp:Button ID="btn_UpdateImage" runat="server" Text="Update_Image"
onclick="btn_UpdateImage_Click" />
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</td>
</tr>
</table>
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3"
RepeatDirection="Horizontal">
<ItemTemplate>
<asp:Image ID="img2" runat="server" Height="150px"
ImageUrl='<%#"Handler.ashx?ImageId="+ Eval("ImageID") %>' Width="200px" />
<br />
<asp:Label ID="lb4" runat="server" Text='<%# Eval("ImageName") %>'></asp:Label>
<br />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ImageId") %>'></asp:Label>
<br />

</ItemTemplate>
</asp:DataList>
</form>
</body

------------------------


Out Put
--------------