Note that there are some explanatory texts on larger screens.

plurals
  1. POadding controls dependent on sql table?
    text
    copied!<p>Hello all I'm working on a little project where I'm adding controls to a page based on a SQL table of questions, this table will grow overtime. I just wanted to share the code and see if there was any better way or if any of the experts could chime in and give me any insight on future problems. Here is the code:</p> <pre><code> protected void Page_Load(object sender, EventArgs e) { try { SqlParameter[] paramz = new SqlParameter[1]; paramz[0] = new SqlParameter("@c_id", 1); dt = SqlHelper.ExecuteDataTable(ConfigurationManager.ConnectionStrings["sql"].ToString(), CommandType.StoredProcedure, "get_Questions", paramz); clinicName.Text = "&lt;b&gt;" + dt.Rows[0]["Clinic Name"].ToString(); for(int row = 0; row &lt;= dt.Rows.Count; row++) { if (row == dt.Rows.Count) //if we're on the last question put a break for spacing(this could be fixed with styling) { Literal alit = new Literal(); alit.Text = "&lt;br/&gt;"; questionsPanel.Controls.Add(alit); } else { addQuestion(dt.Rows[row], row); } } } catch (Exception err) { Response.Write(err.Message); } } private void addQuestion(DataRow row, int i) { Label lbl = new Label(); lbl.Text = row["question"].ToString(); questionsPanel.Controls.Add(lbl); Literal lit = new Literal(); lit.Text = "&lt;br/&gt;"; questionsPanel.Controls.Add(lit); TextBox txt = new TextBox(); txt.ID = "txt" + i.ToString(); questionsPanel.Controls.Add(txt); Literal lit2 = new Literal(); lit2.Text = "&lt;br/&gt;"; questionsPanel.Controls.Add(lit2); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload