On many of the web portals/sites it is observed that latest news start scrolling from down to top.
To do this need to write JavaScript in detail, there could be number of storing news format. I have written this web control to avoid extensive use of JavaScript by using Marquee at server side.
I have tested this application on following browsers:
- Internet Explorer 6.0
- Netscape Navigator 7.2
- Mozilla Firefox 1.5
So no need to worry much about cross browser performance issue, it work simply great on all above browsers.
Requirement: Need To make following tables in SQL Server Database, and put corresponding records in the tables.
This is a very simple table structure in Master Detail format. In tbl_News all the titles and date created will be stored and corresponding newsDetail are stored in tbl_NewsDetail.
News Table
create table tbl_News
(
newsId int primary key identity(1,1) Not null,
newTitle varchar(50) Not null,
dateCreated datetime
)
News Detail Table
create table tbl_NewsDetail
(
dtlId int primary key identity(1,1) Not Null,
newsId int foreign key references tbl_news,
newsDetail varchar(8000)
)
Explanation in detail about the coding
In folder called as "userControll" I have web control as "Scrolling_News.ascx".
The code behind of this page i.e. on page load I have following code
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
SqlConnection myCon = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
string strSql = "SELECT * FROM tbl_News order by dateCreated asc";
string strScrolling = "";
HtmlTableCell cellScrolling = new HtmlTableCell();
SqlCommand myComd = new SqlCommand(strSql,myCon);
SqlDataReader sqlRdr;
try
{
myCon.Open();
sqlRdr = myComd.ExecuteReader();
strScrolling = "<Marquee OnMouseOver='this.stop();' OnMouseOut='this.start();' direction='up' scrollamount='2' bgcolor='#000000' width='40%'>";
while(sqlRdr.Read())
{
strScrolling = strScrolling + "<a href='#' OnClick="+"javascript:window.open
('newsDetail.aspx?NewsId="+sqlRdr.GetValue(0)
+"','NewsDetail','width=400,height=400;toolbar=no;');"+"><font
face='verdana' size='2' color='#ffffff'>"+ sqlRdr.GetValue(1)
+"</a> "+sqlRdr.GetValue(2).ToString()+"</font><br><br>";
}
strScrolling = strScrolling +"</Marquee>";
sqlRdr.Close();
cellScrolling.InnerHtml = strScrolling;
rowScrolling.Cells.Add(cellScrolling);
}
catch(Exception msg)
{
Response.Write(msg.Message);
}
finally
{
//close sql connection
myCon.Close();
}
}
Which just prepare a connection with the database and get the newsTitle and dateCreated to scroll in the marquee in direction='UP'. I have prepared a string which can be added dynamically in a table row to fit in appropriate table row, which generates scrolling news section with anchor (link) to open detail in the new window. Idea behind opening in new window to stick the user to same site. You can do following with the marquee and JavaScript functionality used in the same.
- Change the direction of the scrolling [direction='up']
- Can stop on mouseover of the link [OnMouseOver='this.stop();']
- Can start on mouseout of the link [OnMouseOut='this.start();']
- Can controll the speed of the scrolling [direction='up' scrollamount='2']
Microsoft also provide Add rotator component which can be used with XML, but it differs with respect that by having above functionality.
In newsDetail.aspx page, I have again simple code to get the detail based on querystring of newsId.
Using this control in a page
Register this control as below in a file in which you want to use this
<%@ Register TagPrefix="Scrolling" TagName="News" src="userControll/Scrolling_News.ascx" %>
and use this in a table as below
<table width="100%" bgColor="#ccccff">
<tr>
<td width="60%"><STRONG><FONT face="Tahoma">News Section</FONT></STRONG></td>
</tr>
<tr>
<td><SCROLLING:NEWS id="scroller" runat="server"></SCROLLING:NEWS></td>
</tr>
</table>
That' all enjoy...
What you can do as enhancement?
As an enhancement to this tutorial/article one can add field imageURL in newsDetail table. Also an admin control panel can be provided, to add news and news details to the database by simply having additional screen, which will become complete web application
I think this code snippet is very simple and handy to use as a control in day to day life.
Next article >> I am thinking of writing dynamic drop down, Menu as web control (0 to 1 levels)

Manish MahapatraPosted Aug 18, 2014, 7:48 AM
sir, i create scolling news.ascx and newsDetail.aspx and also create specified tables and insert values in to it . i write all code in this articles to my code file.i can run this program without error .but i can not view the scrolling news. only disply 'news section'.how can i run this code suuccessfuly? please help me... yours Manish Mahapatra
subbu maniPosted Jul 5, 2014, 5:10 AM
hi the news appear in 2 table cells how could i show only 1 ? How to make this to show in one column..???
sa iPosted Dec 20, 2012, 5:43 AM
hi the news appear in 2 table cells how could i show only 1 ?
prachi chakkePosted Sep 20, 2011, 6:57 AM
i want this code in visual studio 2005 ,plz can u send it to my mail id
SasiPosted Sep 9, 2011, 4:47 AM
Hi, Thank for this article. very useful and essential one. but this is not work for me. kindly give me a source code. send to my id. thank U.
MANSOOR ARSHADPosted Dec 19, 2009, 4:17 AM
HELLO MUNIR, I SOLVE THE ABOVE ERROR, BUT NOW I WANT TO KNOW HOW I CAN SET THE HEITHT OF MARQUEE.
MANSOOR ARSHADPosted Dec 19, 2009, 3:40 AM
hi munir i received the following error please help me i am using vb code Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.FormatException: Input string was not in a correct format. Source Error: Line 21: While (sqlRdr.Read()) Line 22: 'strScrolling = strScrolling + "<a href='#' OnClick=" + "javascript:window.open('newsDetail.aspx?NewsId=" + sqlRdr.GetValue(0) + "','NewsDetail','width=400,height=400;toolbar=no;');" + "><font face='verdana' size='2' color='#ffffff'>" + sqlRdr.GetValue(1) + "</a> " + sqlRdr.GetValue(2).ToString() + "</font><br><br>" Line 23: strScrolling = strScrolling + "<a href='#' OnClick=" + "javascript:window.open('newsDetail.aspx?NewsId=" + sqlRdr.GetValue(0) + "','NewsDetail','width=400,height=400;toolbar=no;');" + "><font face='verdana' size='2' color='#ffffff'>" + sqlRdr.GetValue(1) + "</a> " + sqlRdr.GetValue(2).ToString() + "</font><br><br>" Line 24: End While Line 25: strScrolling = strScrolling + "</Marquee>" Source File: F:\Mansoor\Dar-e-Arqam WebApplication\Arqam.Web.UI\UserControls\Scrolling_News.ascx.vb Line: 23 Stack Trace: [FormatException: Input string was not in a correct format.] Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) +216 Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat) +83 [InvalidCastException: Conversion from string "<Marquee OnMouseOver='this.stop(" to type 'Double' is not valid.] Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat) +234 Microsoft.VisualBasic.CompilerServices.Operators.AddObject(Object Left, Object Right) +2828 UserControls_Scrolling_News.Page_Load(Object sender, EventArgs e) in F:\Mansoor\Dar-e-Arqam WebApplication\Arqam.Web.UI\UserControls\Scrolling_News.ascx.vb:23 System.Web.UI.Control.OnLoad(EventArgs e) +80 System.Web.UI.Control.LoadRecursive() +49 System.Web.UI.Control.LoadRecursive() +132 System.Web.UI.Control.LoadRecursive() +132 System.Web.UI.Control.LoadRecursive() +132 System.Web.UI.Control.LoadRecursive() +132 System.Web.UI.Control.LoadRecursive() +132 System.Web.UI.Control.LoadRecursive() +132 System.Web.UI.Control.LoadRecursive() +132 System.Web.UI.Control.LoadRecursive() +132 System.Web.UI.Control.LoadRecursive() +132 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3750 Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433 <!-- [FormatException]: Input string was not in a correct format. at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat) [InvalidCastException]: Conversion from string "<Marquee OnMouseOver='this.stop(" to type 'Double' is not valid. at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.CompilerServices.Operators.AddObject(Object Left, Object Right) at UserControls_Scrolling_News.Page_Load(Object sender, EventArgs e) in F:\Mansoor\Dar-e-Arqam WebApplication\Arqam.Web.UI\UserControls\Scrolling_News.ascx.vb:line 23 at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) [HttpUnhandledException]: Exception of type 'System.Web.HttpUnhandledException' was thrown. at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.default_aspx.ProcessRequest(HttpContext context) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) -->
Ernest AlfordPosted Apr 16, 2009, 6:27 PM
Hello Sir I did get it to work in visual studio 2008 with the SQL express. Thanks for your tutorial. Two issues remains: The test text that I'm using is underlined and I keep getting a a network/database error stating: (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) I would appreciate any pointers or tips.
jobitha johnPosted Apr 1, 2009, 2:44 AM
sir, i create scolling news.ascx and newsDetail.aspx and also create specified tables and insert values in to it . i write all code in this articles to my code file.i can run this program without error .but i can not view the scrolling news. only disply 'news section'.how can i run this code suuccessfuly? please help me... yours faithfully jobitha
adline jerryPosted Dec 18, 2008, 2:26 AM
I dont know how to use this dll in my application . so please send me mail in [email protected] to accept this detail in my web application. Thankyou, by,Jerry
tomcy thomasPosted Oct 23, 2008, 11:16 AM
I search many area ... but this code really nice .......i copyed the code in my web application ...working nice....thanks ..
shubhang nathurPosted Sep 22, 2008, 8:16 AM
can a document file is open when clicking the link
melissa pPosted Aug 25, 2008, 12:27 AM
Hi Munir thanks 4 ur nice code i have question , why we should use two table? is it possible to use one tabel which it has newsID , datecreated,newsDetail ,Newstitle ?
melissa pPosted Aug 25, 2008, 12:26 AM
Hi Munir thanks 4 ur nice code i have question , why we should use two table? is it possible to use one tabel which it has newsID , datecreated,newsDetail ,Newstitle ?
Toby CorballisPosted Mar 3, 2008, 10:38 AM
I get this error: c:\Inetpub\wwwroot\userControl\Scrolling_News.ascx.cs(912305,33): error CS0115: 'ASP.usercontrol_scrolling_news_ascx.FrameworkInitialize()': no suitable method found to override My first page is default.aspx. The codebehind page, default.aspx.cs is blank (nothing in it at all) The other folders are in my userControl (one L) folder and are: Scrolling_News.ascx with the simple table and a code behind page of Scrolling_News.ascx.cs with the code you have put here Any help would be great. What about making the full source code available? (Yes, I am just starting out so sorry for any complete show of stupidity
sudhirPosted Aug 7, 2007, 2:54 AM
Please tell me how i should remove this error-"An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) "..it is showing when i run the page ..i'm using VS2005
DonPosted Jul 11, 2007, 11:04 AM
Hi munir, First i thankful for ur easy coding. its working nice only if i follow ur instructions such as creating table name invoking dll etc., but the problem is if i create a table in some other like news_details its saying error as "tbl_News" invalid object name though in my project nowhere i created a table name(tbl_news), Pls help me out.
DonPosted Jul 11, 2007, 10:53 AM
Hi munir, Nice code, but the problem if i implement ur code as such its working fine.i.e i need to invoke the dll and creating tables as per ur instruction.but, my question is if i create table on some other name its saying error as "tbl_News" invalid object name.Pls help me out.
luke williamsonPosted May 10, 2007, 11:43 AM
Can you post a site where this is working so i can have a look before trying it out. Cheers.
hiral patelPosted Mar 10, 2007, 12:29 AM
hi munir its a nice code. i try to implement it. but it gives error here rowScrolling.Cells.Add(cellScrolling); wht's this rowScrolling kindly reply
ktilleyPosted Mar 8, 2006, 9:49 AM
Just a thought, but wouldn't it be better & much quicker to use a stringbuilder object rather than just string + ?