检查用户是否存在 [英] Check if user exist

查看:105
本文介绍了检查用户是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,现在正在尝试检查用户是否存在于数据库中,是的,后面的代码有效.当我使用类时,出现问题,出现以下错误:对象引用未设置为对象的实例."
它在代码中突出显示了这一行:"reader = dbCmd.ExecuteReader();"

以下是我的班级的代码

Hi guys now am trying to check if the user exist in a Database and yes the code Behind works. The problems comes when Im using a Class, I get the following error: "Object reference not set to an instance of an object."
And it highlights this line in the code: "reader = dbCmd.ExecuteReader();"

Below is the code for my Class

public class Class1
{
    OleDbConnection dbConn;
    DataSet ds;
    OleDbDataAdapter dbAdapter;
    OleDbCommand dbCmd;
    DataTable dataTable;
    OleDbDataReader reader;
    public void dbConnection()
    {
        string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source= H:/SCHOOL OF ICT/ITP 2000/Semester 2/Database System/App_Data/dbICTWorxScheduling.mdb ";
        try
        {
            // set up and open connection
            dbConn = new OleDbConnection(connString);
            dbConn.Open();
        }
        catch (OleDbException exp)
        {
            exp.ToString();
        }
       
    }
	public Class1()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public DataTable CognosGrid()
    {
       
        dbConnection();
        string sql = "SELECT * FROM ICTWorxEducationMembers";
        dbAdapter = new OleDbDataAdapter();
        dbCmd = new OleDbCommand(sql, dbConn);
        dbAdapter.SelectCommand = dbCmd;
        reader = dbCmd.ExecuteReader();
        dataTable = new DataTable();
        reader.Close();
        dbConn.Close();
        dbAdapter.Fill(dataTable);
        
        return dataTable;
        
    }
}



这是按钮后面的代码



This is the code behind the button

protected void btnTest_Click(object sender, EventArgs e)
    {
       

        bool found = false;

        DataTable dataTable = myClass.CognosGrid();
         reader = dbCmd.ExecuteReader();

        while (reader.Read())
        {
   if (txtusername.Text == reader.GetString(0) &&     
        txtpassword.Text == reader.GetString(1))
            {
         MessageBox.Show("Welldone", " Found");

                Response.Redirect("Default.aspx");
                found = true;
            }

        }
        if (!found)
        {

            MessageBox.Show("The account does not exist. Please Register first", "Not Found");

        }

    }
}



编辑TR:已添加< pre>



Edit TR : Added <pre> block to clean up first code snippet.

推荐答案

是的,错误行对象引用未设置为对象的实例"提示对象是在使用它们之前未实例化.您可以在这里尝试两件事.可以将方法"CognosGrid()"设置为静态,以便可以直接调用它:

1.公共静态数据表CognosGrid()
{
//您的代码
}



或在该方法所在的类中创建一个对象,然后使用该对象调用该特定方法:




yes, the error line "Object reference not set to an instance of an object" suggests that objects are not instantiated, before they are being used. You can try two things here. Either make your method "CognosGrid()" static so that you can call it directly :

1. public static DataTable CognosGrid()
{
//your code
}



or make an object of the class in which the method resides and then use that object to call that particular method :




protected void btnTest_Click(object sender, EventArgs e)
    {

        bool found = false;

          Class1 objClass= new Class1();                 //<---
        DataTable dataTable = objClass.CognosGrid();     //<---
         reader = dbCmd.ExecuteReader();
        while (reader.Read())
        {
   if (txtusername.Text == reader.GetString(0) &&
        txtpassword.Text == reader.GetString(1))
            {
         MessageBox.Show("Welldone", " Found");
                Response.Redirect("Default.aspx");
                found = true;
            }
        }
        if (!found)
        {
            MessageBox.Show("The account does not exist. Please Register first", "Not Found");
        }
    }



}


希望这可以解决您的问题.如果有用,请提供投票.


阿努拉格
@cheers @



}


Hope, this would solve your problem. Please provide vote if this was useful.


Anurag
@cheers@


嘿,伙计,请检查此代码.您的问题已解决.仍然,如果您发现任何问题,请告诉我.如果我的回答是正确的,也不要忘记投票;)

hey buddy , check this code . ur problem is solved. still if u find any prob let me know. and dont forget to vote if my answer is corret ;)

OleDbConnection dbConn;
       DataSet ds;
       OleDbDataAdapter dbAdapter;
       OleDbCommand dbCmd;
       DataTable dataTable;
       OleDbDataReader reader;
       public void dbConnection()
   {
       string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source= H:/SCHOOL OF ICT/ITP 2000/Semester 2/Database System/App_Data/dbICTWorxScheduling.mdb ";
       try
       {
           // set up and open connection
           dbConn = new OleDbConnection(connString);
           dbConn.Open();
       }
       catch (OleDbException exp)
       {
           exp.ToString();
       }

   }

       public DataTable CognosGrid()
   {

       dbConnection();
       dataTable = new DataTable();

       string sql = "SELECT * FROM ICTWorxEducationMembers";
       dbAdapter = new OleDbDataAdapter();
       dbCmd = new OleDbCommand(sql, dbConn);
       dbAdapter.SelectCommand = dbCmd;

       dbAdapter.Fill(dataTable);

       dbConn.Close();
       return dataTable;

   }





protected void btnTest_Click(object sender, EventArgs e)
    {


        bool found = false;

        DataTable dataTable = myClass.CognosGrid();

        foreach (DataRow dr in dataTable.Rows)
        {
            if (txtusername.Text == dr["ColumnName"].ToString() && txtpassword.Text == dr["ColumnPassword"].ToString())
            {
                MessageBox.Show("Welldone", " Found");
                found = true;
                Response.Redirect("Default.aspx");
                
            }

        }

        if (!found)
            MessageBox.Show("The account does not exist. Please Register first", "Not Found");
        
    }


请确保在单击测试"按钮之前,已创建dbCmd对象,并且该对象不是null,因此请在!ISPOSTBACK
Make sure that before clicking on your Test button your dbCmd object is created and it is not null , so write db connectivity code in !ISPOSTBACK


中写入数据库连接代码.


这篇关于检查用户是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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