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 GaradPosted Apr 26, 2012, 4:56 AM
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.
Poonam SahuPosted Jan 31, 2021, 11:54 AM
praveen mauryaPosted Apr 26, 2012, 10:06 AM
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
praveen mauryaPosted Apr 26, 2012, 4:44 AM
as i have already told that i am a begineer so i cant understand your shortcuts please define something
SenthilkumarPosted Apr 26, 2012, 4:04 AM
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.
V S N Raju KanumuriPosted Apr 26, 2012, 3:27 AM
praveen mauryaPosted Apr 26, 2012, 3:17 AM
this is not helpful for me it still same is not moving to 3rd row pls some one help
V S N Raju KanumuriPosted Apr 26, 2012, 2:48 AM
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 mauryaPosted Apr 26, 2012, 1:32 AM
at next button click event
praveen mauryaPosted Apr 26, 2012, 1:27 AM
Kunal VaishyaPosted Apr 26, 2012, 1:03 AM
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();
}
}