Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
here with i attached a code can anyone can explain me how it works in step by step:


coding:


public void bind_category()
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select category_id,category_name,description from category", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView2.DataSource = ds;
            GridView2.DataBind();
            con.Close();
        }
    }
Posted

Step By Step explanation
1. The code will check for connection first, if the connection is closed then it will open connection
2. select category_id,category_name,description from category table
3. fill the dataset with records
4. Then fill gridview with dataset, means all records will displayed in gridview

This code will execute if and only if connection is closed

Recommandation:

C#
//remove braces after if

        if (con.State == ConnectionState.Closed)
           con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView2.DataSource = ds;
        GridView2.DataBind();
        con.Close();
 
Share this answer
 
Comments
Ajvirk 12-Nov-11 1:23am    
+5 this is the real explanation. But don't understand why is the need if con is close.
In that SqlCommand They are Select the category_id,category_name,description from the Table Name category then that Data are Display in the Gridview2.
 
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