Click here to Skip to main content
15,883,852 members
Articles / Web Development / ASP.NET

A Beginner's Tutorial for Understanding ADO.NET

Rate me:
Please Sign up or sign in to vote.
4.79/5 (80 votes)
6 Apr 2012CPOL8 min read 433.7K   10.5K   175  
A beginner's tutorial for understanding ADO.NET using a simple application.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Titles : System.Web.UI.Page
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            DataAccess dbAccess = new DataAccess();

            DropDownList1.DataSource = dbAccess.ExecuteSelectCommand("select pub_id from publishers", CommandType.Text);
            DropDownList1.DataTextField = "pub_id";
            DropDownList1.DataValueField = "pub_id";
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, new ListItem("ALL", "ALL"));
                        
            GridView1.DataSource = dbAccess.ExecuteSelectCommand("select * from titles", CommandType.Text);
            GridView1.DataBind();            
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataAccess dbAccess = new DataAccess();

        if (DropDownList1.SelectedValue.Trim() == "ALL")
        {
            GridView1.DataSource = dbAccess.ExecuteSelectCommand("select * from titles", CommandType.Text);
            GridView1.DataBind();    
        }
        else
        {
            string query = "select * from titles where pub_id = @pubid";
            SqlParameter param = new SqlParameter("@pubid", DropDownList1.SelectedValue);
            GridView1.DataSource = dbAccess.ExecuteParamerizedSelectCommand(query, CommandType.Text, new SqlParameter[] { param});
            GridView1.DataBind();    
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

If you like my articles, please visit my website for more: www.rahulrajatsingh.com[^]

  • Microsoft MVP 2015

Comments and Discussions