hi, the below posts may help when do you want itearte the datatable rows, but if you want to show datatable records one by one in button click then you can use above approach
I dont think your approach is right to display all questions and answers by iterating questions and answers in a foreach loop...instead i would advise you to go for repeater which solves the problem without any iteration....
in a repeater control, in the itemtemplate put up a label and then 4 radiobuttons...then bind then using Eval()...
you are right . actually i see only question heading and put the for loop. but it will display only last record. another record will be overwrite.
Repeter / gridview / datalist can be use for display in such manner . i am putting the gridview code but you can modify with repeter or datalist for better performance.
kadavla
Member
270 Points
211 Posts
Forloop
Feb 24, 2012 07:44 AM|LINK
how to retrive more than one record in lable dataset trhorgh my code is like this
I have Make a quiz
public void fill()
{
SqlCommand cmd = new SqlCommand("select * from Quiz_M", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
Question.Text = ds.Tables[0].Rows[0][1].ToString();
ans1.Text = ds.Tables[0].Rows[0][3].ToString();
ans2.Text = ds.Tables[0].Rows[0][4].ToString();
ans3.Text = ds.Tables[0].Rows[0][5].ToString();
ans4.Text = ds.Tables[0].Rows[0][6].ToString();
}
in above code solution.
How to get next record to lable
button click
karthicks
All-Star
31382 Points
5424 Posts
Re: Forloop
Feb 24, 2012 08:00 AM|LINK
hi, to show next records in button click. follow the below steps
i) store the dataset and index into ViewState in fill method
sda.Fill(ds); ViewState["DS"]= ds; ViewState["Index"]=0;
ii) in button click do like below
DataSet ds=ViewState["DS"] as DataSet;
int index=Convert.ToInt32(ViewState["Index"])+1;
Question.Text = ds.Tables[0].Rows[index][1].ToString();
ans1.Text = ds.Tables[0].Rows[index][3].ToString();
ans2.Text = ds.Tables[0].Rows[index][4].ToString();
ans3.Text = ds.Tables[0].Rows[index][5].ToString();
ans4.Text = ds.Tables[0].Rows[index][6].ToString();
ViewState["Index"]=index;
hi, the below posts may help when do you want itearte the datatable rows, but if you want to show datatable records one by one in button click then you can use above approach
Karthick S
shwetamber
Participant
1252 Points
294 Posts
Re: Forloop
Feb 24, 2012 08:05 AM|LINK
Hi,
Use For Loop
for(int i=0;i<ds.Tables[0].Rows.Count;i++) { Question.Text = ds.Tables[0].Rows[i][1].ToString(); ans1.Text = ds.Tables[0].Rows[i][3].ToString(); ans2.Text = ds.Tables[0].Rows[i][4].ToString(); ans3.Text = ds.Tables[0].Rows[i][5].ToString(); ans4.Text = ds.Tables[0].Rows[i][6].ToString(); }AneevJohn
Member
140 Points
52 Posts
Re: Forloop
Feb 24, 2012 08:22 AM|LINK
Hi,
I agree with shwetamber code.
He has provided a perfect loop.
Aneev John
srinanthuram
Contributor
6800 Points
1549 Posts
Re: Forloop
Feb 24, 2012 09:12 AM|LINK
hi
{thank uramiramilu
All-Star
95503 Points
14106 Posts
Re: Forloop
Feb 24, 2012 10:45 AM|LINK
I dont think your approach is right to display all questions and answers by iterating questions and answers in a foreach loop...instead i would advise you to go for repeater which solves the problem without any iteration....
in a repeater control, in the itemtemplate put up a label and then 4 radiobuttons...then bind then using Eval()...
Thanks,
JumpStart
shwetamber
Participant
1252 Points
294 Posts
Re: Forloop
Feb 24, 2012 01:59 PM|LINK
Hi ramiramilu
you are right . actually i see only question heading and put the for loop. but it will display only last record. another record will be overwrite.
Repeter / gridview / datalist can be use for display in such manner . i am putting the gridview code but you can modify with repeter or datalist for better performance.
<asp:GridView ID="grdQuiz" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> <ItemTemplate> <span> <%#Eval("Question") %> </span> <br /> <br /> <span> <%#Eval("Answer1") %> </span> <br /> <span> <%#Eval("Answer2") %> </span> <br /> <span> <%#Eval("Answer3")%> </span> <br /> <span> <%#Eval("Answer4")%> </span> <br /> <br /> <br /> <br /> <br /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> // In Code Behind Bind the GridView grdQuiz.DataSource= ds.Tables[0]; grdQuiz.DataBind();thanks
shwetamber
Participant
1252 Points
294 Posts
Re: Forloop
Feb 24, 2012 02:03 PM|LINK
kadavla ,
If you want to display records one by one after next button click . Then Do Paging in Grid View . Set PageSize = 1 and AllowPaging = 'True'
Thanks & Regards
Shwetamber
kadavla
Member
270 Points
211 Posts
Re: Forloop
Feb 25, 2012 05:45 AM|LINK
Thanks friends