Hello
I am new in .NET Completed VCC.NET From NIIT I am working on a project which is online examination system
and i have almost done but the the problem arise on showing question when i Press the next button it goes upto only 2 rows pls help me how to display further rows/questions. i am using this code...
using System.Data.SqlClient;
SqlConnection cn = new SqlConnection("Data Source=.;Persist Security Info=False;Initial Catalog=admins;User ID=sa;Password=123456"); //global declaration
DataSet ds = new DataSet(); //global declaration
int i=0; //global declaration
at page load event
protected void Page_Load(object sender, EventArgs e)
{
cn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from ques", cn);
da.Fill(ds, "ques");
question.Text = ds.Tables[0].Rows[0][1].ToString();
objective1.Text = ds.Tables[0].Rows[0][2].ToString();
objective2.Text = ds.Tables[0].Rows[0][3].ToString();
objective3.Text = ds.Tables[0].Rows[0][4].ToString();
objective4.Text = ds.Tables[0].Rows[0][5].ToString();
}
at next button event
protected void next_Click(object sender, EventArgs e)
{
if (i == ds.Tables[0].Rows.Count - 1)
{
Response.Redirect("result.aspx");
}
else
{
i = i + 1;
question.Text = ds.Tables[0].Rows[i][1].ToString();
objective1 .Text = ds.Tables[0].Rows[i][2].ToString();
objective2 .Text = ds.Tables[0].Rows[i][3].ToString();
objective3 .Text = ds.Tables[0].Rows[i][4].ToString();
objective4 .Text = ds.Tables[0].Rows[i][5].ToString();
}
and the database is
create database admins
use admins
create table ques
(
quesno int identity(1,1),
ques char(100),
ans1 char(50),
ans2 char(50),
ans3 char(50),
ans4 char(50),
rans char(50)
)
insert ques values('what is mouse','input device','output device','hard disk','printer','input device')
insert ques values('what is keybord','input device','output device','hard disk','printer','input device')
insert ques values('what is speaker','input device','output device','hard disk','printer','output device')
insert ques values('what is cpu','input device','processing unit','hard disk','printer','processing unit')
insert ques values('what is ram','input device',' temporay storage','hard disk','printer','temporay storage')
Please help me as soon as possible.
Regards:
Praveen Kuamr
Loading


Krishna GaradRank #191 · 12.8K reputationPosted Apr 26, 2012, 12:56 AM EDT
First load the dataset in page load
Page_Load(..)
{
dataset ds=YourQuestions();
if(!IsPostBack)
{
//Default Bind first question to your controls and store the current row value in viewstate
ViewState["curRow"] = 0;
}
}
Next in your next button click event do something like this
nextbutton_click(..)
{
if (ds!=null && (int)ViewState["curRow"]
ViewState["curRow"] = (int)ViewState["curRow"] + 1;
//BindData here again like
question.Text = ds.Tables[0].Rows[(int)ViewState["curRow"]]["Question"].ToString();
objective1 .Text = ds.Tables[0].Rows[(int)ViewState["curRow"]]["OptionA"].ToString();
objective2 .Text = ds.Tables[0].Rows[(int)ViewState["curRow"]]["OptionB"].ToString();
objective3 .Text = ds.Tables[0].Rows[(int)ViewState["curRow"]]["OptionC"].ToString();
objective4 .Text = ds.Tables[0].Rows[(int)ViewState["curRow"]]["OptionD"].ToString();
}
}
Hope this will helps you.
Kunal VaishyaRank #990 · 2K reputationPosted Apr 25, 2012, 9:03 PM EDT
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
cn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from ques", cn);
da.Fill(ds, "ques");
question.Text = ds.Tables[0].Rows[0][1].ToString();
objective1.Text = ds.Tables[0].Rows[0][2].ToString();
objective2.Text = ds.Tables[0].Rows[0][3].ToString();
objective3.Text = ds.Tables[0].Rows[0][4].ToString();
objective4.Text = ds.Tables[0].Rows[0][5].ToString();
}
}
praveen maurya103 reputationPosted Apr 25, 2012, 9:27 PM EDT
praveen maurya103 reputationPosted Apr 25, 2012, 9:32 PM EDT
at next button click event
V S N Raju KanumuriRank #8,839 · 215 reputationPosted Apr 25, 2012, 10:48 PM EDT
U have declare int i=0 in globally. so when u click on next button each and every time i is initialize to 0 and it count should be 1 only. Http request is a stateless protocol .
so try to maintain i value in cookies or session or viewstate
in pageload
declare like
viewstate["i"]=0;
in next button click
int i =convert.ToInt32(viestate["i"]);
i=i+1
viewstate["i"]=i;
praveen maurya103 reputationPosted Apr 25, 2012, 11:17 PM EDT
this is not helpful for me it still same is not moving to 3rd row pls some one help
V S N Raju KanumuriRank #8,839 · 215 reputationPosted Apr 25, 2012, 11:27 PM EDT
SenthilkumarRank #387 · 6.5K reputationPosted Apr 26, 2012, 12:04 AM EDT
It is simple to do that.
I will tell you steps.
1) Loads all the questions into DataSet
2) Initially checks it has more than one record then assign it default as first question
3) You need to always maintain the current page by using the view state or session.
3) Make visible and invisible of the navigation controls and increment/decrement the page for previous and next
4) Show the record from the DataSet using the current page.
praveen maurya103 reputationPosted Apr 26, 2012, 12:44 AM EDT
as i have already told that i am a begineer so i cant understand your shortcuts please define something
praveen maurya103 reputationPosted Apr 26, 2012, 6:06 AM EDT
it realy amazing your code works great
thanks a lot and stay in contact
i ll be with you soon with my upcoming problems
thanks a lot
Poonam Sahu5 reputationPosted Jan 31, 2021, 6:54 AM EST