saifullah khan

saifullah khan

  • NA
  • 335
  • 301.1k

No data is shown

Oct 25 2011 3:58 AM
hi
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();
}
}

 <div>
  <table style="width: 100%">
  <tr>
  <td>
  <asp:HyperLink ID="HyperLink1" runat="server" ImageUrl="~/Images/logo.gif"></asp:HyperLink></td>
  </tr>
  <tr>
  <td>
  <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1"
  Width="100%" onitemdatabound="DataList1_ItemDataBound">
  <ItemTemplate>
  <table style="width: 100%">
  <tr>
  <td style="height: 21px">
  <asp:Label ID="Label3" runat="server" Font-Bold="True" Text='<%# Eval("QuestionID") %>'></asp:Label>
  <strong>)</strong>
  <asp:Label ID="Label2" runat="server" Font-Bold="True" Text='<%# Eval("Question") %>'></asp:Label></td>
  </tr>
  <tr>
  <td>
  <asp:RadioButtonList ID="RadioButtonList1" runat="server">
  </asp:RadioButtonList>
  <asp:CheckBoxList ID="CheckBoxList1" runat="server">
  </asp:CheckBoxList>
  <asp:TextBox ID="TextBox1" runat="server" Columns="30" Font-Bold="False" Rows="5"
  TextMode="MultiLine"></asp:TextBox></td>
  </tr>
  </table>
  <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("AnswerType") %>' />
  </ItemTemplate>
  <HeaderTemplate>
  <asp:Label ID="Label4" runat="server" Font-Bold="True" Text="Please answer the following survey questions :"></asp:Label>
  </HeaderTemplate>
  <FooterTemplate>
  <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
  </FooterTemplate>
  </asp:DataList>
  <asp:Label ID="Label5" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="Red"></asp:Label></td>
  </tr>
  <tr>
  <td align="center">
  <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Copyright (C) 2006. All rights reserved."></asp:Label></td>
  </tr>
  </table>
  </div>
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
  SelectCommand="SELECT * FROM [SurveyQuestions] WHERE ([SurveyID] = @SurveyID)">
  <SelectParameters>
  <asp:QueryStringParameter Name="SurveyID" QueryStringField="id" Type="Int32" />
  </SelectParameters>
  </asp:SqlDataSource>

but it does not show the questions. although questions with choice are available in my db
please tell me whats the problem

Answers (1)