Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindQuestion();
                SqlConnection con = new SqlConnection("Data Source=MSPLMC0011\\SQLEXPRESS;Initial Catalog=thursday;User ID=sa;Password=multiverse@1");
            }
        }

        private void BindQuestion()
        {
            SqlConnection con = new SqlConnection("Data Source=MSPLMC0011\\SQLEXPRESS;Initial Catalog=thursday;User ID=sa;Password=multiverse@1");
            SqlDataAdapter da = new SqlDataAdapter("Select * from SecurityQuestion", con);
            con.Open();
            DataSet ds = new DataSet();
            da.Fill(ds);

            Question.DataSource = ds.Tables[0];
            Question.DataBind();
            Question.Items.Insert(0, new ListItem("-Select Question-", ""));
        }

        protected void btn_Submit_Click(object sender, EventArgs e)
        {

            try
            {
                if (ViewState["ans"].ToString() == txtans.Text)
                {

                    lblans.Visible = true;
                    lblans.Text = "Your Password is :" + ViewState["password"].ToString();

                }
                else
                {
                    lblans.Visible = true;
                    lblans.Text = "ans not match...";

                }
            }
            catch (Exception ex)
            {
                lblans.Visible = true;
                lblans.Text = ex.Message;
            }
        }

        protected void Question_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=MSPLMC0011\\SQLEXPRESS;Initial Catalog=thursday;User ID=sa;Password=multiverse@1");
            SqlDataAdapter da = new SqlDataAdapter("Select SecurityQuestion, SecurityID from SecurityQuestion", con);
            con.Open();
            DataSet ds = new DataSet();
            da.Fill(ds);

            Question.DataSource = ds.Tables[0];
            Question.DataBind();
            Question.Items.Insert(0, new ListItem("---Select Question----", ""));
        }
Posted
Updated 19-Aug-14 21:01pm
v2
Comments
Mayank Vashishtha 20-Aug-14 2:51am    
Why don't you use Debugger?
Member 11023602 20-Aug-14 2:54am    
Where i used Debugger
Kumarbs 20-Aug-14 2:59am    
In your code
Thanks7872 20-Aug-14 3:00am    
In visual studio, press f9 at bindquestion/btn_Submit_Click method to set break point. Then run your app. This breakpoint will get hit and then press f10 to step through each line. Check the line at which you got error, you will find any object as null. Resolve it such that it should not be null.
Gupta Poonam 20-Aug-14 3:13am    
Have you set the DataTextField and DataValueField property of your dropdownlist?

At the first code review the error seems to be generated in your btn_Submit_Click method from the next line:
C#
if (ViewState["ans"].ToString() == txtans.Text)

The value from ViewState may be null, so you should correct it with:
C#
if (ViewState["ans"] != null && ViewState["ans"].ToString() == txtans.Text)


PS: You forgot to mention the line of code that generate the error.
 
Share this answer
 
Comments
Member 11023602 20-Aug-14 3:03am    
ok..
and
Raul Iloc 20-Aug-14 7:26am    
Did you fix the problem as I suggested?
make sure your viewstate objects does not have null value. check before assigning the values for viewstate objects.

C#
if(ViewState["ans"]!=null && ViewState["Password"]!=null)
{
// set the values
}



Also this is not a good practice to open the connection at each stage by giving connection string. Follow some coding rules to make your efficient code.
 
Share this answer
 
I think you have missed one step before btn_Submit_Click. In which step you will set your both ViewState. Above solutions are for checking whether your ViewState null or not.
 
Share this answer
 
Check with code and put debugger. There must be some problem with ViewState.
your ViewState would be null or blank..
 
Share this answer
 
Please put Debugger so we Can Understand From Where The Object Reference Be Null..@Vinay it is not necessary that problem is Of ViewState...But He Should Pass The Object..And It Should Be null That Is Checked By Debugger Then Only We Can Catch UP....!!!!
Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900