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
Discuss This Question: