RSS

How to display the questions with their answers

24 Jul

Hello ,

if you want to develop application like MCQ or Survey Conduct  Web Application Project then use below mention code and also i add complete project code . i search on that lot of time to how to implement & validation . i think this Article very helpful for you.

surveyimgClient Side:

.css

 

<style type=”text/css”>
.questiontext {
background-color: #EEF6FF;
font-family: verdana;
font-size: 12px;
font-weight: bold;
padding: 0.5em 1em;
text-align: left;
}
.radioButtonList{ list-style-type: none;
margin: 0 0 0.5em 0.5em;
text-align: left;
}

.coa-button {
background-color: #9AC51D;
background-image: -moz-linear-gradient(center top , #9AC51D, #7C9F17);
border: 1px solid #759716;
border-radius: 3px 3px 3px 3px;
color: #FFFFFF;
display: block;
padding: 7px 0;
text-align: center;
text-decoration: none !important;
width: 189px;
}
</style>

 

:: Following  below  repeater code to show all Surveys

 

<asp:Repeater id=”Repeaterforselect” runat=”server”
onitemcommand=”Repeaterforselect_ItemCommand” >

<ItemTemplate>

<li>

<i><%#(((RepeaterItem)Container).ItemIndex+1).ToString()%></i>
<asp:LinkButton ID=”LinkButton1″ runat=”server” Text='<%#Eval(“title”)%>’ CommandName=”Click”

CommandArgument='<%# Eval(“id”)%>’ >

</asp:LinkButton>

</li>

</ItemTemplate>

</asp:Repeater>

 

:: Following  below  DataList  code to show selected  Survey Question & option answer

<asp:DataList ID=”dtlQuestions” runat=”server” RepeatDirection=”Vertical” DataKeyField=”ID” OnItemDataBound=”dtlQuestions_ItemDataBound” Width=”630px”>
<ItemTemplate>
<div >
<asp:Label ID=”Label2″ Text='<%#(((DataListItem)Container).ItemIndex+1).ToString()%>’ Runat=”server”/>
<asp:Label ID=”q” runat=”server” Text='<%# Eval(“questions”) %>’ CssClass=”questiontext”></asp:Label> <asp:RequiredFieldValidator ID=”RequiredFieldValidator1″ runat=”server”
ErrorMessage=”Required” ForeColor=”Red” ControlToValidate=”dtlAnswers” ValidationGroup=”Bla”></asp:RequiredFieldValidator>
<asp:HiddenField ID=”hnQuestionsid” runat=”server” Value='<%# DataBinder.Eval(Container.DataItem, “Questionsid”)%>’ />
</div>
<br />
<asp:RadioButtonList ID=”dtlAnswers” runat=”server” ValidationGroup=”Bla” RepeatDirection=”Vertical” RepeatColumns=”1″ CssClass=”radioButtonList”>

</asp:RadioButtonList>
</ItemTemplate>
</asp:DataList>

 

:: Submit Button Handle with validation, without selection any question data not insert into database.

 

<asp:Button ID=”btnSubmit” runat=”server” onclick=”btnSubmit_Click”

ValidationGroup=”Bla” CssClass=”coa-button” Width=”100px” style=”float:right;margin-right:150px;” Visible=false
Text=”Submit”  />

 

Server Side:

 

string connectionString = ConfigurationManager.ConnectionStrings[“ApplicationServices”].ToString();
SqlConnection conn;
SqlCommand cmd = new SqlCommand();

////Load All Survey on Page Load

protected void Page_Load(object sender, EventArgs e)
{

conn = new SqlConnection(connectionString);

if (!Page.IsPostBack)
{

hdnsurveyformswomp.Value = “0”;

loadSurvey();

}
}

// Fill Repeater Control to All Survey

public void loadSurvey()
{

string connectionString = ConfigurationManager.ConnectionStrings[“ApplicationServices”].ToString();

DataSet ds = new DataSet();

ds = SqlHelper.ExecuteDataset(connectionString, System.Data.CommandType.StoredProcedure, “surveysselect”);
if (ds.Tables[0].Rows.Count > 0 && ds != null)
{
Repeaterforselect.DataSource = ds;
Repeaterforselect.DataBind();
}

}

// At this Event we Load Question & option answer on Each Survey Click to fill datalist
protected void Repeaterforselect_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case “Click”:
//get command argument here
string id = e.CommandArgument.ToString();
loadQuestion(id);

break;
}

}

// Load Question against Survey

public void   loadQuestion(string id)
{

string connectionString = ConfigurationManager.ConnectionStrings[“ApplicationServices”].ToString();

DataSet ds = new DataSet();

SqlParameter[] paramArray = new SqlParameter[] {
new SqlParameter(“@id”,Convert.ToInt32(id)),

};

ds = SqlHelper.ExecuteDataset(connectionString, System.Data.CommandType.StoredProcedure, “SurveyFormsWOMPretrive”, paramArray);
if (ds.Tables[0].Rows.Count > 0)
{

Label1.Text = ds.Tables[0].Rows[0][“title”].ToString();
hnSurveyid.Value = ds.Tables[0].Rows[0][“surveysid”].ToString();
dtlQuestions.DataSource = ds;
dtlQuestions.DataBind();
btnSubmit.Visible = true;
}

else
{
Label1.Text = “No Record Found”;
dtlQuestions.DataSource = null;
dtlQuestions.DataBind();
btnSubmit.Visible = false;
}

}

 

// Fill DataList RaiobuttonDropdownList against each Question

protected void dtlQuestions_ItemDataBound(object sender, DataListItemEventArgs e)
{

DataListItem drv = e.Item.DataItem as DataListItem;
RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl(“dtlAnswers”);
HiddenField hnQuestionsid = (HiddenField)e.Item.FindControl(“hnQuestionsid”);
DataSet ds = loadQuestionOptionbyqid(hnQuestionsid.Value);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
RadioButtonList1.DataSource = ds;
RadioButtonList1.DataTextField = “question_option”;
RadioButtonList1.DataValueField = “question_id”;
RadioButtonList1.DataBind();
}

}

// Load Question Option in Radiobuttonlist

 
public DataSet loadQuestionOptionbyqid(string id)
{

string connectionString = ConfigurationManager.ConnectionStrings[“ApplicationServices”].ToString();

DataSet ds = new DataSet();

SqlParameter[] paramArray = new SqlParameter[] {
new SqlParameter(“@id”,Convert.ToInt32(id)),

};

return SqlHelper.ExecuteDataset(connectionString, System.Data.CommandType.StoredProcedure, “Surveyquestionchoisebyqstid”, paramArray);

}
/// Submit Selected Question to db

 
protected void btnSubmit_Click(object sender, EventArgs e)
{

//Data save
//  count = 0;
if (dtlQuestions.Items.Count > 0)
{
foreach (DataListItem li in dtlQuestions.Items)
{
RadioButtonList rdList = li.FindControl(“dtlAnswers”) as RadioButtonList;
HiddenField hnQuestionsid = (HiddenField)li.FindControl(“hnQuestionsid”);
foreach (ListItem answer in rdList.Items)
{
bool isSelected = answer.Selected; if (isSelected) { int slval = Convert.ToInt32(answer.Value); InsertSurveyData(Convert.ToInt32(hnSurveyid.Value), Convert.ToInt32(hnQuestionsid.Value), Convert.ToInt32(slval)); }
}
}
}

Response.Redirect(“thanksyou.aspx”);

}

/// Insert Function

 

private void InsertSurveyData(int sid, int qid, int qoptid)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = “SubmitSurveyForminsert”;
//simple hiden field no repeater
cmd.Parameters.Add(“@Surveyid”, SqlDbType.Int).Value = sid;
cmd.Parameters.Add(“@Questionid”, SqlDbType.Int).Value = qid;
cmd.Parameters.Add(“@surveyQuestionOptionid”, SqlDbType.Int).Value = qoptid;

cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();

conn.Close();

}

 

 

// Complete Application Code With database Script

 

https://skydrive.live.com/?cid=FCCA6A631A7CE903&id=FCCA6A631A7CE903%21880

 

Advertisements
 
Leave a comment

Posted by on July 24, 2013 in ASP Dot Net C#

 

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: