名称'dr'在当前上下文中不存在 [英] the name 'dr' doesnot exist in the current context

查看:92
本文介绍了名称'dr'在当前上下文中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用3层架构设计登录页面



在商务访问层:



public SqlDataReader login()

{

ob.getcon();



SqlDataReader dr = ob.exdr (选择client_pwd来自Register where email ='+ email +');

返回博士;







}



登录页面:



< pre lang =cs> protected void Button1_Click( object sender,EventArgs e)
{
string email = TextBox3.Text;
ba.login();

if (!dr.Read())
{
Label4.Text = 无效用户;
}
else
{
if (dr [ 0 ]。ToString()== TextBox2.Text)
{
Response.Redirect( 〜/ mainPage.aspx);
}
else
{
Label4.Text = 密码错误;

}


}





这里显示的是dr在当前上下文中不存在。如何解决这个问题

解决方案

if(dr [0] .ToString()== TextBox2.Text)

您尚未在此课程中定义 dr



一般来说UI不应该知道数据阅读器。

让BC处理这个并将结果作为数据模型返回给UI。


错误是因为您正在读取在类中声明的变量。

改进您的解决方案,将电子邮件传递给Login()函数。而不是返回DataReader。返回一个表或行。例如。

  public   static  DataTable GetTable (SqlCommand cmd)
{
DataTable tbl = null ;
SqlConnection conn = null ;
尝试
{
conn = // 您的连接;
cmd.Connection = conn;
tbl = new DataTable();
SqlDataReader dr = cmd.ExecuteReader();
tbl.Load(dr);

dr.Close();
dr.Dispose();
}
catch (例外情况)
{
throw ex;
}
最后
{
如果(conn != null
{
conn.Close();
}
}
return tbl;
}

受保护 void Button1_Click( object sender,EventArgs e)
{
string email = TextBox3.Text;
SqlCommand cmd = new SqlCommand();
cmd = 从Register中选择client_pwd,其中email =' + email + '
DataTable tbl = ba.GetTable(cmd)

< span class =code-keyword> if (tbl == null && tbl.Rows.Count == 0
{
Label4.Text = 无效用户;
}
其他
{
// < span class =code-comment> your code



}


Chexk this link

http://msdn.microsoft.com/ en-us / library / f3st0d45(v = vs.100).aspx [ ^ ]

之后将您的bll名称导入您的页面



编写代码为



  protected   void  Button1_Click( object  sender,EventArgs e)
{
字符串 email = TextBox3.Text;
SqlDataReader dr = ba.login();

if (!dr.Read())
{
Label4.Text =& quot;无效的用户和放大器; QUOT ;;
}
else
{
if (dr [ 0 ]。ToString()== TextBox2.Text)
{
Response.Redirect(& quot;〜/ mainPage.aspx& QUOT);
}
else
{
Label4.Text =& quot; Wrong Password& quot ;;

}


} < / pre >



=


i'm designing a login page using 3tier architecture

In business access layer :

public SqlDataReader login()
{
ob.getcon();

SqlDataReader dr = ob.exdr("select client_pwd from Register where email='" + email + "'");
return dr;



}

In login page :

protected void Button1_Click(object sender, EventArgs e)
       {
           string email = TextBox3.Text;
           ba.login();

           if (!dr.Read())
       {
          Label4.Text="Invalid User";
       }
       else
       {
           if (dr[0].ToString() == TextBox2.Text)
           {
               Response.Redirect("~/mainPage.aspx");
           }
           else
           {
              Label4.Text="Wrong Password";

           }


       }



here it is showing that dr doesnot exist in current context.how to solve this

解决方案

if (dr[0].ToString() == TextBox2.Text)
You have not defined dr in this class.

In general the UI should not know about the data reader.
Let the BC handle this and return the result to the UI as a data model.


error is because you are reading a variable which is declared in class.
improve your solution, pass email to Login() function. Instead of returning a DataReader. return a table or row. eg.

public static DataTable GetTable(SqlCommand cmd)
        {
            DataTable tbl = null;
            SqlConnection conn = null;
            try
            {
                conn = //your connection;
                cmd.Connection = conn;
                tbl = new DataTable();
                SqlDataReader dr = cmd.ExecuteReader();
                tbl.Load(dr);

                dr.Close();
                dr.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
            return tbl;
        }

protected void Button1_Click(object sender, EventArgs e)
       {
           string email = TextBox3.Text;
SqlCommand cmd = new SqlCommand();
cmd="select client_pwd from Register where email='" + email + "'"
          DataTable tbl = ba.GetTable(cmd)

           if (tbl == null && tbl.Rows.Count == 0)
       {
          Label4.Text="Invalid User";
       }
       else
       {
          //your code


       }


Chexk this link
http://msdn.microsoft.com/en-us/library/f3st0d45(v=vs.100).aspx[^]
after that import your bll name into yourpage

And write the code as

protected void Button1_Click(object sender, EventArgs e)
       {
           string email = TextBox3.Text;
           SqlDataReader dr=ba.login();

           if (!dr.Read())
       {
          Label4.Text=&quot;Invalid User&quot;;
       }
       else
       {
           if (dr[0].ToString() == TextBox2.Text)
           {
               Response.Redirect(&quot;~/mainPage.aspx&quot;);
           }
           else
           {
              Label4.Text=&quot;Wrong Password&quot;;

           }


       }</pre>


=


这篇关于名称'dr'在当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆