Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NET
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 30 Dec '12 - 5:52


3 solutions

  Permalink  
Comments
Cto Manav Parasrampuria - 31 Dec '12 - 3:45
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:05
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....
  Permalink  
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:
 
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.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 564
1 Maciej Los 255
2 CPallini 245
3 Aarti Meswania 173
4 Mahesh Bailwal 171
0 Sergey Alexandrovich Kryukov 9,162
1 OriginalGriff 7,179
2 CPallini 3,913
3 Rohan Leuva 3,176
4 Maciej Los 2,588


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid