Friday, 3 February 2012

how to bind table all columns to dropdownlist using c#

protected void ddl_Catagory_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList1.ClearSelection(); 
        string selectvalue=ddl_Catagory.SelectedItem.Value.ToString() ; 
        if (selectvalue == "Agent")
        {
            Tablename = "BBIMS_NewAgent";
           
            cmd = new SqlCommand("select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'BBIMS_NewAgent'", con);
            datafill();
        }
        else if (selectvalue == "Customer")
        {
            Tablename = "BBIMS_NewCustomer";
            cmd = new SqlCommand("select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'BBIMS_NewCustomer'", con);
            datafill();
        }
        else if (selectvalue == "Tenant")
        {
            Tablename = "BBIMS_Tenant";
            cmd = new SqlCommand("select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'BBIMS_Tenant'", con);
            datafill();
        }


    }

--------------------------------------------------------------------------------------------------------

private void datafill()
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }

        SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
      
        sqlDa.Fill(ds);
        foreach (DataTable dt in ds.Tables)
        {
         
            foreach (DataRow dr in dt.Rows)
            {
                string str=dr[0].ToString();
                if (str == "Postcode" || str == "Surname" || str == "Address" || str == "Telephone" || str == "JobId" || str == "Name" || str == "Tenantname")
                {
                    //if (str == "Addres")
                    //{
                    //    str = "Address";
                    //}
                  //  DropDownList1.Items.Add(dr[0].ToString());
                    DropDownList1.Items.Add(str);
                }

            }

        }
    }

No comments:

Post a Comment