0

There is an error i.e. Object reference not set to an instance of an object, i am trying to compare security question and answer using email id which are present in database using below code

    protected void Button1_Click(object sender, EventArgs e)

{

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\keishna\Documents\Visual Studio 2010\Projects\MasterDemo\App_Data\Earth_Movers.mdf;Integrated Security=True;User Instance=True");
        con.Open();
        DataSet ds = new DataSet();
        SqlCommand cmd = new SqlCommand("SELECT Security_Question,Answer FROM Registration Where email='" + TextBox3.Text + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds,"data");
        //string Ans = (cmd.ExecuteReader()).ToString();
        string sq = (ds.Tables["0"]).ToString();
        string Ans = (ds.Tables["1"]).ToString();
        if (sq == TextBox3.Text && Ans == TextBox2.Text)
        {
            Response.Redirect("NewPassword.aspx?email="+TextBox3.Text);
        }
    }

but error is here

**string sq = (ds.Tables["0"]).ToString();
            string Ans = (ds.Tables["1"]).ToString();**

using this code i am taking email id as query string and sending it to NewPassword.aspx page where i can set new passsword and update this using email id for changing particular column.

and NewPassword.aspx code has a button,on a button click new password should get updated in registration table and login table of my database.

NewPassword.aspx code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Webpages_NewPassword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    { 

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\krishna\Documents\Visual Studio 2010\Projects\MasterDemo\App_Data\Earth_Movers.mdf;Integrated Security=True;User Instance=True");
        con.Open();
        if (Request.QueryString["email"] != null)
        {
            string Email= Request.QueryString["email"];
            SqlCommand cmd = new SqlCommand("update Registration set Password where email='" + Email + "'", con);
        }
        con.Close();

    }
}

So please provide me solution as soon as possible.

Thanks and Regards

Vasundara Reddy

  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – nvoigt Mar 05 '14 at 17:09

2 Answers2

5

One of these things is null.

ds
ds.Tables
ds.Tables["0"]
ds.Tables["1"]

Are you sure you don't mean this instead?

ds.Tables[0]
ds.Tables[1]
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
3

Use the index of the table as int, so instead of

string sq = (ds.Tables["0"]).ToString();

this

string sq = (ds.Tables[0]).ToString();

Since your DataSet does not contain a table with the name "0" it returns null.

The name of your table is "data", because you have used the overload of DataApter.Fill that takes the name of the table it should fill.

So this works also:

string sq = (ds.Tables["data"]).ToString();

However, i'm sure that you don't want DataTable.ToString but the content of it's row(s):

DataTable data = ds.Tables["data"];
string firstQuestion = data.Rows[0].Field<string>("Security_Question");
string firstAnswer = data.Rows[0].Field<string>("Answer");
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • how to compare data of database with textbox value.its like i have security question and answer which should be compared and then moved to NewPassword.aspx and then user can update his new password – user3384492 Mar 05 '14 at 17:13
  • @user3384492: i have edited my answer to show you how you read the rows fields. You can loop them if the table contains multiple via `DataTable.Rows`. – Tim Schmelter Mar 05 '14 at 17:15
  • Now there is no error but not redirected to NewPassword.aspx – user3384492 Mar 05 '14 at 17:25