Click here to Skip to main content
15,909,498 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Can anyone explain this coding step by step?

C#
public void city()
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlCommand cmd = new SqlCommand("select distinct area from product", con);
        SqlDataReader dr = cmd.ExecuteReader();
        BulletedList2.DataSource = dr;
        BulletedList2.DataTextField = "area";
        BulletedList2.DataValueField = "area";
        BulletedList2.DataBind();
        con.Close();
    }
Posted
Updated 17-Nov-11 17:10pm
v2
Comments
koolprasad2003 17-Nov-11 23:24pm    
why you Question subject and body are mismatched ?
sathiyak 17-Nov-11 23:32pm    
sorry....hereafter i takecare...

Go step by step

1. check for the coonection if it's closed then open the connection
2. Execute Query to select distinct area from product table
3. strore result in reader
4. bind the BulletedList2 with area
5. connection close.
 
Share this answer
 
If the connection is not opened, then open it.
Construct a SqlCommand by providing a SQL statement
Execute the SqlCommand and return a SqlDataReader that contains the resultset
Assign SqlDataReader to the datasource of BulletedList
Tell BulletedList2 what fields in the SqlDatReader results to use
Cause BulletedList2 to bind to the results in the SqlDataReader in the DataSource and construct the html elements

Now, next time learn to read some documentation. All of this is explained in great detail in a great many sources.
 
Share this answer
 
The main Idea of this code is to get the cities from the database and bind it to a list.
So the SqlCommand and DataReader fetch the data from the Database.

The DataSource of list is bound to the dr where the data from Database has been stored.

DataTextField and DataValueField refers to the text to be displayed to you in the page and the value of the item selected to be posted back respectively.

Finally you are binding the data to the list
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900