Wednesday 30 November 2011

NEWS Scrolling in asp.net from database using Literal Control






In source code(aspx):-

         <marquee direction="left" onmouseout="this.start()" onmouseover="this.stop()"
                    scrolldelay="10" style="height: 11px">                   
                   
             <asp:Literal ID="lt1" runat="server"></asp:Literal></marquee>

In .cs code:-

SqlConnection SqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConNews"].ConnectionString);



protected void Page_Load(object sender, EventArgs e)
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from newsmarquee", SqlCon);
        DataSet ds = new DataSet();
        da.Fill(ds, "newsmarquee");
        string s1;

        for (int num = 0; num < ds.Tables[0].Rows.Count-1; num++)
        {
            string url = "~/Images/meta3.gif";//dr[num]["ItemPath"].ToString();
            Image newpic = new Image();
            newpic.ImageUrl = url;


            ////lt1.Controls.Add(newpic);
            s1 = ds.Tables["newsmarquee"].Rows[num][1].ToString();
            lt1.Text = s1.ToString();
        }
       
}

Wednesday 9 November 2011

Export GridView to Excel in ASP.NET


       Following Code shows how you can bind data to GridView control in the Page_Load event. Please keep in mind that for brevity I am storing connection string in the code. You should always store your database connection strings in web.config. 


protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
    }

private void BindData()
{
   string constr = @"Server=SampleServer;Database=NORTHWND;uid=test; Password=test;";
   string query = "SELECT ProductID, ProductName, UnitPrice FROM Products";

   SqlDataAdapter da = new SqlDataAdapter(query, constr);
   DataTable table = new DataTable();

   da.Fill(table);

   GridView1.DataSource = table;
   GridView1.DataBind();
}

       As you can guess from the above screen shot that the button “Export to Excel” will implement the main export functionality.The code below is the button click event handler code to export GridView control contents to excel but you can use similar code to export to other formats by changing the ContentType property.

Response.Clear();

Response.AddHeader("content-disposition", "attachment; filename=Products.xls");
Response.Charset = "";

Response.ContentType = "application/vnd.xls";

StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

GridView1.RenderControl(htmlWriter);
Response.Write(stringWriter.ToString());

Response.End();

        
        Sometimes, when you render any asp.net control dynamically as I am doing with the GridView control in this tutorial, you can get HttpException with the following message:

Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server. 


The problem can be solved by overriding Page class VerifyRenderingInServerForm method which confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time. 


public override void VerifyRenderingInServerForm(Control control)

}

When you will click the Export to Excel button you will see the following dialog box asking you to open or save dynamically generated Excel file.




Following screen shot shows you how the data is saved in Excel file.


Happy Programming...................!!!!!!

Scrolling News Web Control using ASP.Net and C#




Requirement: Need To make following tables in SQL Server Database, and put corresponding records in the tables.

News Table



create table TblNews
 (
  newsId  int  primary key identity(1,1) Not null, 
  NewsDescription varchar(50) Not null, 
  dateCreated datetime 
 )

In code behind page:
News.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillNews();
        }
    }

    private void FillNews()
    {
        // Fetch News Records from Database and Store in Datatable…
        SqlConnection Conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        String strQuery = "SELECT NewsDescription FROM TblNews";
        DataTable _dt = new DataTable();
        SqlDataAdapter _da = new SqlDataAdapter(strQuery,Conn);
        _da.Fill(_dt);

        string strNews = null;

        for (int i = 0; i < _dt.Rows.Count; i++)
        {
            strNews = strNews + _dt.Rows[i]["NewsDescription"].ToString();
            strNews = strNews + "  ||  ";
        }

        lblNews.Text = strNews;
    }




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.
  • 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']

In Design page:
News.aspx


<table width="100%" cellspacing="0px" cellpadding="0px" class="NewsBgColor">
        <tr>
            <td class="Table_TD_Center">
                <marquee behavior="SCROLL" width="100%" scrolldelay="100″" onmouseover='this.stop();'
                    onmouseout="this.start();">
                    <asp:Label ID="lblNews" runat="server" Text="" CssClass="RedLabel"/>
                </marquee>
            </td>
        </tr>
    </table>



Thanx...................