Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have table question having fields: qid,question,and 4 choices,
i want to use these 4 choices to show in radiobutton list using datalist.so please help me how can i do...
Posted

 
Share this answer
 
Comments
Cto Manav Parasrampuria 31-Dec-12 3:45am    
let me try again...i try earlier but it not work.....anyaways thanks...i will try agian .....reply u....
Cto Manav Parasrampuria 2-Jan-13 8:05am    
hello i try this but it return only one value ..below is my code :
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
//Get questionID here
int QID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "QID"));
//pass Question ID to your DB and get all available options for the question
//Bind the RadiobUttonList here
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();

con.ConnectionString = ConfigurationManager.ConnectionStrings["TQConnectionString"].ConnectionString;



string q = "SELECT Ans1,Ans2,Ans3,Ans4 from TestQuestion where QId=" + QID;

cmd = new SqlCommand(q, con);

cmd.Connection.Open();

SqlDataAdapter da = new SqlDataAdapter(q, con);
DataSet ds = new DataSet();
da.Fill(ds); DataTable dt = ds.Tables[0];
RadioButtonList1.DataSource = dt;
RadioButtonList1.DataTextField = dt.Columns[0].ToString();
RadioButtonList1.DataValueField = dt.Columns[0].ToString();

RadioButtonList1.DataBind();
}

}
but i want to add all these 4 ans1,ans2,ans3,ans4 in my RadiobuttonList1.
so please help me....
hello i try this but it return only one value ..below is my code :
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
//Get questionID here
int QID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "QID"));
//pass Question ID to your DB and get all available options for the question
//Bind the RadiobUttonList here
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();

con.ConnectionString = ConfigurationManager.ConnectionStrings["TQConnectionString"].ConnectionString;



string q = "SELECT Ans1,Ans2,Ans3,Ans4 from TestQuestion where QId=" + QID;

cmd = new SqlCommand(q, con);

cmd.Connection.Open();

SqlDataAdapter da = new SqlDataAdapter(q, con);
DataSet ds = new DataSet();
da.Fill(ds); DataTable dt = ds.Tables[0];
RadioButtonList1.DataSource = dt;
RadioButtonList1.DataTextField = dt.Columns[0].ToString();
RadioButtonList1.DataValueField = dt.Columns[0].ToString();

RadioButtonList1.DataBind();
}

}
but i want to add all these 4 ans1,ans2,ans3,ans4 in my RadiobuttonList1.
so please help me....
 
Share this answer
 
I have worked on a similar application but my Table structure was different. It had "answerID", "questionId", "answerText" and "isCorrect" columns. So I used to pass "questionId" as variable and it fetched all the answers for the selected question. This was a better solution as a question can have 2, 3 or 4 options. In your case the resultset returned by your query is going to give you only a single row and you are binding the data to radiobuttonlist with only single row. So it will always give you answer 1. You can change the structure of your table or modify your code if you are sure that there are exactly 4 answers for every question. Try:

C#
string q = "SELECT Ans1,Ans2,Ans3,Ans4 from TestQuestion where QId=" + QID;
 
cmd = new SqlCommand(q, con);

cmd.Connection.Open();

SqlDataAdapter da = new SqlDataAdapter(q, con);
DataSet ds = new DataSet();
da.Fill(ds); 
DataTable dt = ds.Tables[0];

DataTable dtRecords = new DataTable();
dtRecords.Columns.Add("answer",typeof("string"));
dtRecords.Rows.Add(dt.Rows[0][0].ToString().Trim());
dtRecords.Rows.Add(dt.Rows[0][1].ToString().Trim());
dtRecords.Rows.Add(dt.Rows[0][2].ToString().Trim());
dtRecords.Rows.Add(dt.Rows[0][3].ToString().Trim());

RadioButtonList1.DataSource = dtRecords;
RadioButtonList1ataTextField = dtRecords.Columns[0].ToString();
RadioButtonList1.DataValueField = dtRecords.Columns[0].ToString();
 
RadioButtonList1.DataBind();


But this is just a workaround. You should modify your table structure and make it more generic.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900