0

I have a interface with several check boxes. I want it to be checked if the relevant value available in the database.
This is the interface I have
Interface

First for check boxes are to be loaded from a different table and the others within the box should be loaded using another table.
If you can please help me to do this.

This the table for first 4 checkboxes
1st checkboxes

This is the table for lower checkboxes
lower checkboxes

I have created the query and data gets to a dataset.
This is the code I have done so far

            List<int> list = new List<int>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                list.Add(Convert.ToInt32(dr["Qulif_code"]));

            }
            int[] arr = list.ToArray();

            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] == 1)
                {
                    ck_ol.Checked==true ;
                }

            }
5
  • where is your code you have tried? and can you please elaborate as its difficult to figure out based on what conditions ans value will the checkboxes be checked what the mapping between them etc etc.. Commented Jan 10, 2017 at 4:14
  • @Manish I have added a the code I have done so far Commented Jan 10, 2017 at 4:16
  • With checkbox type, the data in the database should be boolean. Usually it will automatically check if the value = true. Commented Jan 10, 2017 at 4:28
  • ok so what i can figure out from your code is that the for loop is the issue. Because you are iterating over the array and based on the condition setting the checked property true. So it will iterate over all items in array and will be checked or not based on the value of the last item. the approach is correct but you code is not. Commented Jan 10, 2017 at 4:30
  • Thanks for you all Commented Jan 10, 2017 at 4:32

2 Answers 2

3

Database


  • I have made use of the following table Hobbies with the schema as follows. Enable disabled CheckBoxField columns in GridView in ASP.Net

    enter image description here

  • I have already inserted few records in the table Enable disabled CheckBoxField columns in GridView in ASP.Net

enter image description here

  • HTML Markup

Below is the HTML Markup of the ASP.net web page.

As you can see I have added a CheckBoxList and a Button that will allow the user to update the selections in the database.

<asp:CheckBoxList ID="chkHobbies" runat="server">
</asp:CheckBoxList>
<br />
<asp:Button ID="btnUpdate" runat="server" Text="Button" OnClick = "UpdateHobbies" />

The screenshot below describes how the User Interface looks

enter image description here

Populating the CheckBoxList from Database


The following method is used to populate the Hobbies CheckBoxList from the SQL Server Database C#

private void PopulateHobbies()
{
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = ConfigurationManager
                .ConnectionStrings["constr"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select * from hobbies";
            cmd.Connection = conn;
            conn.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    ListItem item = new ListItem();
                    item.Text = sdr["Hobby"].ToString();
                    item.Value = sdr["HobbyId"].ToString();
                    item.Selected = Convert.ToBoolean(sdr["IsSelected"]);
                    chkHobbies.Items.Add(item);
                }
            }
           conn.Close();
        }
    }
}

The above method is called up in the page load event in the following way:-

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.PopulateHobbies();
    }
}

Saving the Selections in Database


The following method is called up on the Submit Button Click event and is used to save the user selections to the database

protected void UpdateHobbies(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = ConfigurationManager
                .ConnectionStrings["constr"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "update hobbies set IsSelected = @IsSelected" +
                              " where HobbyId=@HobbyId";
            cmd.Connection = conn;
            conn.Open();
            foreach (ListItem item in chkHobbies.Items)
            {
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@IsSelected", item.Selected);
                cmd.Parameters.AddWithValue("@HobbyId", item.Value);
                cmd.ExecuteNonQuery();
            }
            conn.Close();
        }
    }
}

When program runs for the first time When program runs for the first time

After updating the values in checkbox.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

I have solved the problem and here is the code.

            List<int> list = new List<int>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                list.Add(Convert.ToInt32(dr["Qulif_code"]));

            }
            int[] arr = list.ToArray();

            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] == 1)
                {
                    ck_ol.Checked = true;
                }
                else if (arr[i] == 2)
                {
                    ck_al.Checked = true;
                }
                else if (arr[i] == 3)
                {
                    ck_dip.Checked = true;
                }
                else if (arr[i] == 4)
                {
                    ck_deg.Checked = true;
                }

            }          

I hope this might help to someone in future.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.