m creating an online mcqs system.
the page coding which will show the ques is shown below
public partial class Question_page : System.Web.UI.Page
{
SqlConnection conn=new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Pices\\Downloads\\Compressed\\Survey\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
//HiddenField anstype=e.Item.FindControl();
HiddenField anstype = e.Item.FindControl("HiddenField1") as HiddenField;
Label questionid = e.Item.FindControl("Label3") as Label;
RadioButtonList rbl = e.Item.FindControl("RadioButtonList1") as RadioButtonList;
CheckBoxList cbl = e.Item.FindControl("CheckBoxList1") as CheckBoxList;
TextBox txt = e.Item.FindControl("TextBox1") as TextBox;
Int16 a=Convert.ToInt16(questionid.Text);
DataSet ds = GetDataSet(a);
switch (anstype.Value)
{
case "S":
rbl.Visible = true;
cbl.Visible = false;
txt.Visible = false;
rbl.DataSource = ds;
rbl.DataTextField = "Choice";
rbl.DataValueField = "ChoiceID";
rbl.DataBind();
break;
case "M":
rbl.Visible = false;
cbl.Visible = true;
txt.Visible = false;
cbl.DataSource = ds;
cbl.DataTextField = "Choice";
cbl.DataValueField = "ChoiceID";
cbl.DataBind();
break;
case "T":
rbl.Visible = false;
cbl.Visible = false;
txt.Visible = true;
break;
}
}
}
private DataSet GetDataSet(int id)
{
//SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString);
SqlCommand cmd = new SqlCommand("select * from surveychoices where questionid=@id", conn);
SqlParameter p1 = new SqlParameter("@id", id);
cmd.Parameters.Add(p1);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "choices");
return ds;
}
protected void Button1_Click(object sender, System.EventArgs e)
{
foreach (DataListItem item in DataList1.Items) {
if (item.ItemType == ListItemType.Item | item.ItemType == ListItemType.AlternatingItem) {
int questionid = 0;
int choiceid = 0;
string choicetext = "";
questionid =Convert.ToInt16( ((Label)item.FindControl("Label3")).Text);
HiddenField anstype = item.FindControl("HiddenField1")as HiddenField;
switch (anstype.Value) {
case "S":
RadioButtonList rbl = item.FindControl("RadioButtonList1") as RadioButtonList;
choiceid =Convert.ToInt16( rbl.SelectedValue);
SaveAnswer(questionid, choiceid, "");
break;
case "M":
CheckBoxList cbl = item.FindControl("CheckBoxList1")as CheckBoxList;
for (int i = 0; i <= cbl.Items.Count - 1; i++) {
if (cbl.Items[i].Selected) {
choiceid =Convert.ToInt16( cbl.Items[i].Value);
SaveAnswer(questionid, choiceid, "");
}
}
break;
case "T":
TextBox txt = item.FindControl("TextBox1") as TextBox;
choicetext = txt.Text;
SaveAnswer(questionid, 0, choicetext);
break;
}
}
}
DataList1.Visible = false;
Label5.Text = "Thank you for participating in the survey!";
}
private void SaveAnswer(int qid, int cid, string ct)
{
// SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString);
SqlCommand cmd = new SqlCommand("insert into surveyanswers(QuestionID,ChoiceID,ChoiceText) values(@qid,@cid,@ct)", conn);
//SqlParameter p1 = new SqlParameter("@qid", qid);
//SqlParameter p2 = new SqlParameter("@cid", (cid == 0 ? DBNull.Value= @cid));
//SqlParameter p3 = new SqlParameter("@ct", (string.IsNullOrEmpty(ct) ? DBNull.Value : ct));
//cmd.Parameters.Add(p1);
//cmd.Parameters.Add(p2);
//cmd.Parameters.Add(p3);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
| ||
but it does not show the questions. although questions with choice are available in my db
please tell me whats the problem
Jean PaulPosted Oct 25, 2011, 6:42 AM
Can you please do one thing.
Upload the .aspx page, .aspx.cs file, .sql file of the table structure. (If your business allows :))
I will test this here and let you know.