I m trying to make a online exam page in website,
my code is in .cs is:
public static SqlConnection sqlconn;
protected string PostBackStr;
int startid = 1;
int endid = 4;
int totalnoofquestions = 4;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings["cnnstring"].ToString());
PostBackStr = Page.ClientScript.GetPostBackEventReference(this, "time");
if (IsPostBack)
{
string eventArg = Request["__EVENTARGUMENT"];
if (eventArg == "time")
{
getNextQuestion();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Visible = false;
txtName.Visible = false;
Button1.Visible = false;
Panel1.Visible = true;
lblName.Text = "Name : " + txtName.Text;
int score = Convert.ToInt32(txtScore.Text);
lblScore.Text = "Score : " + Convert.ToString(score);
Session["counter"] = "1";
Random rnd = new Random();
int i = rnd.Next(startid ,endid+1 );
getQuestion(i);
ArrayList al = new ArrayList();
al.Add(i.ToString());
Session["ids"] = al;
}
protected void Button2_Click(object sender, EventArgs e)
{
getNextQuestion();
}
protected void Finish_Click(object sender, EventArgs e)
{
if (Session["Answer"].ToString() == RblOption.SelectedIndex.ToString())
{
int score = Convert.ToInt32(txtScore.Text) + 1;
txtScore.Text = score.ToString();
lblScore.Text = "Score : " + Convert.ToString(score);
}
lblResult.Text = "Thank you for test our application. Your Score is : " + txtScore.Text;
lblResult.Visible = true;
Panel2.Visible = false;
}
public void getQuestion(int no)
{
string str = "select * from Questions where id=" + no + "";
SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
DataSet ds2 = new DataSet();
da2.Fill(ds2, "Question");
if (ds2.Tables[0].Rows.Count > 0)
{
DataRow dtr;
int i = 0;
while (i < ds2.Tables[0].Rows.Count)
{
dtr = ds2.Tables[0].Rows[i];
Session["Answer"] = Convert.ToString(Convert.ToInt32(dtr["Correct"].ToString()) - 1);
lblQuestion.Text = "Q." + Session["counter"].ToString() + " " + dtr["Question"].ToString();
RblOption.ClearSelection();
RblOption.Items.Clear();
RblOption.Items.Add(dtr["Option1"].ToString());
RblOption.Items.Add(dtr["Option2"].ToString());
RblOption.Items.Add(dtr["Option3"].ToString());
RblOption.Items.Add(dtr["Option4"].ToString());
i++;
}
}
}
public void getNextQuestion()
{
if (Convert.ToInt32(Session["counter"].ToString()) < totalnoofquestions)
{
if (RblOption.SelectedIndex >= 0)
{
if (Session["Answer"].ToString() == RblOption.SelectedIndex.ToString())
{
int score = Convert.ToInt32(txtScore.Text) + 1;
txtScore.Text = score.ToString();
lblScore.Text = "Score : " + Convert.ToString(score);
}
}
Random rnd = new Random();
int i = rnd.Next(startid ,endid );
ArrayList al = (ArrayList)Session["ids"];
if (!al.Contains(i.ToString()))
{
al.Add(i.ToString());
}
else
{
while (al.Contains(i.ToString()))
{
i = rnd.Next(startid, endid + 1);
if (al.Count == totalnoofquestions - 1 && !al.Contains(i.ToString()))
{
Button2.Visible = false;
Finish.Visible = true;
break;
}
else if (al.Count > endid + 1)
{
break;
}
}
if (!al.Contains(i.ToString()))
{
al.Add(i.ToString());
}
}
if (al.Count == totalnoofquestions)
{
Button2.Visible = false;
Finish.Visible = true;
}
Session["ids"] = al;
Session["counter"] = Convert.ToString(Convert.ToInt32(Session["counter"].ToString()) + 1);
getQuestion(i);
}
else
{
Panel2.Visible = false;
}
}
public void ConnectionOpen()
{
try
{
if (sqlconn.State == ConnectionState.Closed) { sqlconn.Open(); }
}
catch (SqlException ex)
{ }
catch (SystemException sex)
{ }
}
public void ConnectionClose()
{
try
{
if (sqlconn.State != ConnectionState.Closed) { sqlconn.Close(); }
}
catch (SqlException ex)
{ }
catch (SystemException exs)
{ }
}
}
but it gives the error
Server Error in '/Online Exam' Application.
Fill: SelectCommand.Connection property has not been initialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Fill: SelectCommand.Connection property has not been initialized.
Source Error:
Line 75: SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
Line 76: DataSet ds2 = new DataSet();
Line 77: da2.Fill(ds2, "Question");
Line 78: if (ds2.Tables[0].Rows.Count > 0)
Line 79: {
Source File: c:\Documents and Settings\DW\Desktop\New Folder\Online Exam\Default.aspx.cs Line: 77
Stack Trace:
[InvalidOperationException: Fill: SelectCommand.Connection property has not been initialized.]
System.Data.Common.DbDataAdapter.GetConnection3(DbDataAdapter adapter, IDbCommand command, String method) +4995530
System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +76
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +319
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +92
_Default.getQuestion(Int32 no) in c:\Documents and Settings\DW\Desktop\New Folder\Online Exam\Default.aspx.cs:77
_Default.Button1_Click(Object sender, EventArgs e) in c:\Documents and Settings\DW\Desktop\New Folder\Online Exam\Default.aspx.cs:50
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
how to solve this problem....