Hi ,
Am currently working on online examination web project.. i have a table like this..
sno question answer1 answer2 answer3 answer4 correctanswer
1 sql structured simple short none structured
i just want to bind these 4 answers to my radiobuttonlist which is present inside the gridview.. am not able to do that so please help me ....
here is what i've tried ,
Aspx code :Font-Size="X-Small">
C# code :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
myConnection.Open();
cmd = new SqlCommand("select option1 , option2 , option3 , option4 from online where pn_BranchID = '" + employee.BranchId + "' and pn_questID='" + txt_qsetno.Text + "' ", myConnection);
rea1 = cmd.ExecuteReader();
if (rea1.Read())
{
RadioButtonList rbl_responses = (RadioButtonList)e.Row.Cells[1].FindControl("RadioButtonList6");
if (e.Row.RowType == DataControlRowType.DataRow)
{
rbl_responses.Items.Add(new ListItem(Convert.ToString(rea1["option1"])));
rbl_responses.Items.Add(new ListItem(Convert.ToString(rea1["option2"])));
rbl_responses.Items.Add(new ListItem(Convert.ToString(rea1["option3"])));
rbl_responses.Items.Add(new ListItem(Convert.ToString(rea1["option4"])));
}
rea1.Close();
myConnection.Close();
}
But its not working.. i dont know how to bind the 4 answers fields to the radiobuttonlist..
Arunkumar EmmPosted Jan 19, 2011, 12:18 AM
Arunkumar EmmPosted Jan 19, 2011, 12:09 AM
I'm glad tat u found the solution. i gotta finish this thing before 25/1/2011. Sending via mail wud be better. my id is [email protected]. Need to use OnRowBounding event huh.. Never thought of it..
George OnofreiPosted Jan 18, 2011, 11:49 AM
edit: I finally understood what you were trying to do. I think I'll combine the 2 methods. When is your deadline?
edit 2: thought about it some more and indeed I'll combine the 2 methods. Now that \i understand what you wanted, indeed you need to use the OnRowBounding event
Arunkumar EmmPosted Jan 18, 2011, 3:18 AM
George OnofreiPosted Jan 18, 2011, 3:04 AM
Arunkumar EmmPosted Jan 18, 2011, 3:00 AM
Mmm. Think of a way. Pls Make it asap. Its also ok for me to follow ur logic (List
George OnofreiPosted Jan 18, 2011, 2:21 AM
the if probably gave you the first results and the while gave you the last results in your example.
I'll think of a way, but I would go for the List
Arunkumar EmmPosted Jan 18, 2011, 1:43 AM
sno questions
1 sql means?
@ structured @ simple @ short @none @dbms @cdbms @rdbms @none
2. sql is an example 4?
@ structured @ simple @ short @none @dbms @cdbms @rdbms @none
So what do i do?.. Is there anyway to get my logic work? or i should revert back to ur logic i.e, making a List and then bind it..
Arunkumar EmmPosted Jan 16, 2011, 9:14 AM
George OnofreiPosted Jan 13, 2011, 1:52 PM
why while instead of if? the reader when is used with .Read() reads only one of the extracted values, so you need a while to iterate until there are nu more values.
now for the binding stuff. I think a better aproach is to make a List
public class T
{
public string Answer1 {get; set;}
public string Answer2 {get; set;}
...............
public string RightAnswer {get; set;}
}
and then bind them
What I don't know is if your approach is the best way. I mean, every time I worked with gridviews, datagrids, listview etc, I'd do something like:
public partial class
{
public void Page_Load(object sender, EventArgs e)
{
List
list=GetList();
GridView1.DataSource = list;
GridView1.DataBind();
}
public List
{
List
//add all questions, answers and right answer
}
}
and in the aspx page:
<%# Eval("Answer1") %>
.....................................................//each Answer
this is a solution which I think will work. Let's get back to your idea and what can be done. Well, I don't know, because if you replace the if with the while and do the adding, I guess all questions will get the same answer.
Before you try my idea, do a save as you don't want to lose data. And please give a feedback. Also, if any questions rise, please post. Good luck!!!
Arunkumar EmmPosted Jan 13, 2011, 11:39 AM
Yes , i'm reading all the questions and bind them into a gridview in a single page. in the gridview , there is no problem with the questions.it displays in the correct order. but my problem is with the answers. the same answer is repeating for all the questions. I'm populating the radiobuttonlist which contains the answer from the gridview_rowdatabound event. lets say i have a database like the following,
sno question answer1 answer2 answer3 answer4 correctanswer
1 sql means? structured simple short none structured
2 sql is an example 4? dbms cdbms rdbms none rdbms
The grid view that i'm getting will look like ,
sno questions
1 sql means?
@ structured @ simple @ short @none
2. sql is an example 4?
@ structured @ simple @ short @none
see , the options are same for question 1 and 2. Hope u understood what i'm trying to say..
George OnofreiPosted Jan 13, 2011, 7:37 AM
Are you reading all the questions and then bind them in gridview or are you accessing them based on a sort of a "Next" button. I mean, are you reading all the values, or just one and then the next after an event?
Arunkumar EmmPosted Jan 13, 2011, 4:12 AM
The same answers are repeating for all the questions.. I think there is something to do with sno in the query.. but i dont know how to do it.
cmd = new SqlCommand("select option1 , option2 , option3 , option4 from online where pn_BranchID = '" + employee.BranchId + "' and pn_questID='" + txt_qsetno.Text + "' and sno=??? ", myConnection);
George OnofreiPosted Jan 13, 2011, 2:43 AM
rbl_responses.Items.Add(new ListItem(rea1.GetString(rea1.GetOrdinal("option1"))));