0

i'm trying to retrieve options from database to checkbox and if it has the same product ID it has to be checked by default

this is the insert code to database

for (int i = 0; i < ddlcaroptions.Items.Count; i++)
{
   if (ddlcaroptions.Items[i].Selected == true)
{
Int64 PCarOptionID = Convert.ToInt64(ddlcaroptions.Items[i].Value);
SqlCommand cmd2 = new SqlCommand("insert into tblCarOptionQuant values('" + PID + "','" + PCarOptionID + "')", con);
cmd2.ExecuteNonQuery();
}
}

it works fine now i want to edit this checkbox and update database now i bind the checkbox from another table that has name and values


SqlCommand cmd = new SqlCommand("select * from tblCarOption", con);

                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                if (dt.Rows.Count != 0)
                {
                    ddlcaroptions.DataSource = dt;
                    ddlcaroptions.DataTextField = "CarOptionNameAR";
                    ddlcaroptions.DataValueField = "CarOptionID";
                    ddlcaroptions.DataBind();
                }

now i have all the options but i want the selected values to be the ones that user already checked before and saved in tblCarOptionQuant table,

i tried to get the values by this command

using (SqlCommand getselectedop = new SqlCommand("select PCarOptionID from tblCarOptionQuant where PID = " + PID + "", con))

its okay but now what can i do to set selected values from this command result !??

2 Answers2

0

you can use FindByValue to get the specific item and set it to selected, refer the code below

string value = "SomeValue";
               var item = ddlcompany.Items.FindByValue(value);
               if (item != null)
                   item.Selected = true;
  • Thank you for your answer, yes it is working but still have a problem, how can i give more than 1 value ? or what can i do if i want to select more than one option ,, i tried what you said but still have to give 1 value to be selected in `string value = "SomeValue";` it is not 1 value !! – Mando Basha Jun 21 '19 at 02:20
  • Please refer this post maybe it will help you. https://stackoverflow.com/questions/9185770/programmatically-check-an-item-in-checkboxlist-where-text-is-equal-to-what-i-wan – Abdul Muheet Shaikh Jun 21 '19 at 07:12
0

the problem is when you retrieve data and convert to string you get only 1st row so i used StringBuilder to build 1 string has all rows in table :) !! Attention !! before you use this code make sure you bind your checkbox with all the values that user checked or not ,, then use this code to get the options that selected before (by the user)

                using (SqlCommand getselectedop = new SqlCommand("RetrieveCarOptions", con))
            {
                getselectedop.CommandType = CommandType.StoredProcedure;
                getselectedop.Parameters.AddWithValue("@PID", Convert.ToInt64(Request.QueryString["PID"]));
                con.Open();
                using (SqlDataReader reader = getselectedop.ExecuteReader())
                {
                    StringBuilder sb = new StringBuilder();
                    if (reader.HasRows)
                    {
                        if (sb.Length > 0) sb.Append("___");
                        while (reader.Read())
                        {
                            for (int i = 0; i < reader.FieldCount; i++)
                                if (reader.GetValue(i) != DBNull.Value)
                                    sb.AppendFormat("{0}-", Convert.ToString(reader.GetValue(i)));
                            SelectCheckBoxList(reader["PCarOptionID"].ToString(), ddlcaroptions);
                        }
                    }
                }
            }

now split and check if it has the same value to mark as checked

        private void SelectCheckBoxList(string valueToSelect, CheckBoxList ddlcaroptions)
    {
        string[] aray = valueToSelect.Split(',');
        foreach (string listItem2 in aray)
        {
            ListItem listItem = ddlcaroptions.Items.FindByValue(valueToSelect);
            listItem.Selected = true;                
        }
    }

i hope this answer is clear and help someone else :)