Skip to content

November 9, 2013

How to code a simple cms with Asp.net and C# Part 1

We are using literal control in asp.net to display html content from database..
He we displaying how to how to display data from database display it into web page.
In the first part we putting separate web forms to display each page.
vs

 

 

 

 

We create a database ,’cms’ and created a table called ‘viewing’ .

db

 

 

 

Each row in table show each pages of our cms.Here showing 5 rows in table.so we have 5 page for this cms.Our pages are ‘Home’,’Products’,’Services’,’About Us’,’Contact Us’.

Sample code for Home Page.Default.aspx

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;
public partial class view : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=Pass123;database=cms");
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter ada = new SqlDataAdapter();
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        cmd.Connection = con;
        dt.Clear();
        cmd.CommandText = "select * from viewing";
        ada.SelectCommand = cmd;
        ada.Fill(dt);

        if (!IsPostBack)
        {

            showpage();

        }

    }

    void showpage()
    {
        Literal1.Text = dt.Rows[0][1].ToString();
        Literal2.Text = dt.Rows[0][2].ToString();
        Literal3.Text = dt.Rows[0][3].ToString();
        Literal4.Text = dt.Rows[0][4].ToString();
        Literal5.Text = dt.Rows[0][5].ToString();
        Literal6.Text = dt.Rows[0][6].ToString();
        Literal7.Text = dt.Rows[0][7].ToString();
        Literal8.Text = dt.Rows[0][8].ToString();
        Literal9.Text = dt.Rows[0][9].ToString();
        Literal10.Text = dt.Rows[0][10].ToString();
        Literal11.Text = dt.Rows[0][11].ToString();
        Literal12.Text = dt.Rows[0][12].ToString();
        Literal13.Text = dt.Rows[0][13].ToString();
    }

}

Sample code for Products Page.Products.aspx

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;
public partial class view : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=Pass123;database=cms");
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter ada = new SqlDataAdapter();
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        cmd.Connection = con;
        dt.Clear();
        cmd.CommandText = "select * from viewing";
        ada.SelectCommand = cmd;
        ada.Fill(dt);

        if (!IsPostBack)
        {

            showpage();

        }

    }

    void showpage()
    {
        Literal1.Text = dt.Rows[1][1].ToString();
        Literal2.Text = dt.Rows[1][2].ToString();
        Literal3.Text = dt.Rows[1][3].ToString();
        Literal4.Text = dt.Rows[1][4].ToString();
        Literal5.Text = dt.Rows[1][5].ToString();
        Literal6.Text = dt.Rows[1][6].ToString();
        Literal7.Text = dt.Rows[1][7].ToString();
        Literal8.Text = dt.Rows[1][8].ToString();
        Literal9.Text = dt.Rows[1][9].ToString();
        Literal10.Text = dt.Rows[1][10].ToString();
        Literal11.Text = dt.Rows[1][11].ToString();
        Literal12.Text = dt.Rows[1][12].ToString();
        Literal13.Text = dt.Rows[1][13].ToString();
    }

}

Download Demo