2014-03-05 206 views
0

没有设置为一个对象的实例的错误,即对象的引用,我试图来比较安全性问题,并使用电子邮件ID是存在于数据库中使用下面 代码错误:未将对象引用设置为对象的实例?

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); 
     } 
    } 

但错误是在这里回答

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

使用此代码我以电子邮件ID作为查询字符串,并将其发送给NewPassword.aspx页面,在这里我可以设置新密码时应并更新此使用电子邮件ID为改变特定列。

和NewPassword.aspx代码有一个按钮,在一个按钮上单击新密码应该在我的数据库的注册表和登录表中更新。

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(); 

    } 
} 

请尽快提供给我解决方案。

感谢和问候

Vasundara雷迪

+0

可能重复[什么是NullReferenceException,我该如何解决它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – nvoigt

回答

4

这些事情之一是null

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

你确定你不是这个意思吗?

ds.Tables[0] 
ds.Tables[1] 
2

使用表的索引为int,所以不是

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

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

因为你DataSet不包含返回null名称"0"表。

您的表的名称是"data",因为您已使用DataApter.Fill的过载,该过载采用应填写的表的名称。

所以这个作品也:

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

然而,我敢肯定,你不想DataTable.ToString,但它的内容是行(S):中

DataTable data = ds.Tables["data"]; 
string firstQuestion = data.Rows[0].Field<string>("Security_Question"); 
string firstAnswer = data.Rows[0].Field<string>("Answer"); 
+0

如何比较数据库与文本框value.its像我有安全问题和答案应该被比较,然后移动到NewPassword.aspx,然后用户可以更新他的新密码 – user3384492

+0

@ user3384492:我编辑了我的答案,告诉你如何阅读行字段。如果表格包含多个通过“DataTable.Rows”,则可以循环它们。 –

+0

现在没有错误但没有重定向到NewPassword.aspx – user3384492

相关问题