Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:52am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Cto Manav Parasrampuria at 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 at 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....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
0 Sergey Alexandrovich Kryukov 410
1 Maciej Los 300
2 OriginalGriff 288
3 CHill60 190
4 Sinisa Hajnal 180


Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100