简体   繁体   中英

C# asp.net Create multiple check box dynamically with value of mysql database

i am trying to create multiple check box with the value of mysql database fetched rows say for me if i have 10 rows in DB i need 10 check box to be created dynamically in a div.

am not sure how to do that but i have done a slimier code which add's a "," after each fetch.

string constr = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;


using (MySqlConnection con = new MySqlConnection(constr))
{

    using (MySqlCommand MySqlCommand = new MySqlCommand("SELECT FatherFullName FROM xxxxxx where sssss='xxxxx'", con))
    {
        MySqlCommand.CommandType = CommandType.Text;
        con.Open();
        MySqlDataReader MySqlDataReader = MySqlCommand.ExecuteReader();
        var list = new List<string>();
        while (MySqlDataReader.Read())
        {

            string name = MySqlDataReader["FatherFullName"].ToString();
            if (!String.IsNullOrEmpty(name))
                list.Add(name);



        }
        con.Close();



    }
}

I will suggest use asp.net CheckBoxList Control, Suppose its name is CheckBoxList1 then Declare Datatable and get the data to be bind in that datatables Object

using (MySqlCommand MySqlCommand = new MySqlCommand("SELECT FatherFullName FROM xxxxxx where sssss='xxxxx'", con))
    {
        MySqlCommand.CommandType = CommandType.Text;
        con.Open();
        DataTable dt = MySqlCommand.ExecuteTable();
        if(dt.Rows.Count > 0)
        {
           CheckBoxList1.DataSource = dt;
           CheckBoxList1.DataTextField = "Title";
           CheckBoxList1.DataValueField = "ArticleID";
           CheckBoxList1.DataBind();
        }

                   con.Close();   

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM