Create DropDowns and populate ASP.NET

5 pts.
Tags:
ASP.NET
C
SQL
I am trying to build a asp.net application with C#. It has a gridview in it that contains dropdownlist. I am looking to have a SQL query run that returns a record set. Lets say it returns 10 items. It should create 10 new rows (which it does). Then it should populate each one of those drop downs with 1 of the 10 results. So for each result it would insert and select the item in the dropdown list for each.
Here is my code so far:
protected void BindDepartments(params ListControl[] controls)
        {

            SqlConnection sqlCon = new SqlConnection(WebConfigurationManager.ConnectionStrings["TimeClock"].ConnectionString);
            try
            {
                DataTable dt = new DataTable();
                using (sqlCon)
                {
                    using (SqlCommand cmd = new SqlCommand("select title from oci_project where client_id='42'", sqlCon))
                    {
                        cmd.CommandType = CommandType.Text;
                        sqlCon.Open();
                        using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                        {
                           
                            adp.Fill(dt);
                          
                            foreach (DataRow projectdata in dt.Rows)
                            {
                                addrecordset();
                                FillDropDownList(ddl1);
                            }
                            if (dt.Rows.Count > 0)
                            {
                                
                                foreach (ListControl ctrl in controls)
                                {
                                    
                                    foreach (DataRow dtRow in dt.Rows)
                                    {

                                        ctrl.DataBind();
                                        ctrl.Items.Add(new ListItem(dtRow["title"].ToString()));
                                       

                                    }
                                    if (ctrl.Items.Count > 0)
                                    {
                                        //Insert "-select-" at the first position in dropdownlist
                                        ctrl.Items.Insert(0, new ListItem("-Select-", "0"));
                                    }
                                    else
                                    {
                                        //Insert "-No data-" at the first position in dropdownlist
                                        ctrl.Items.Insert(0, new ListItem("-No Data-", "0"));
                                    }
                                }
                            }
                        }
                        sqlCon.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write("Error:" + ex.Message.ToString());
            }
        }
 private void addrecordset()
        {
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = null;

                if (dtCurrentTable.Rows.Count > 0)
                {
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
                    //add new row to DataTable
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    //Store the current data to ViewState


                    //Rebind the Grid with the current data
                    //Gridview1.DataSource = dtCurrentTable;
                    Gridview1.DataBind();
                   // FillDropDownList(ddl1);

                }
                else
                {
                    Response.Write("ViewState is null");
                }
            }
        }

Picture Located Here

0

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: