我在程序中遇到此错误 [英] I got this Error in program

查看:80
本文介绍了我在程序中遇到此错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void txtLogin_Click(object sender, EventArgs e) {
        string ConnectionString = "User ID=SYSDBA;Password=masterkey;" + "Database=localhost:C:\\Program Files\\Attend HRM\\Data\\ITAS.FDB; " + "DataSource=localhost;Charset=NONE;";
        FbConnection addDetailsConnection = new FbConnection(ConnectionString);
        addDetailsConnection.Open();
        string SQLCommandText = "Select EMP_ID,EMP_ENO from EMP_EMP where EMP_ID= '" + txtEmpId.Text + "'";
        FbDataReader dr = null;       
        //SqlDataReader dr = cmd.ExecuteReader();
        if (!dr.Read() || dr["EMP_ENO"].ToString() != txtPassword.Text)
            Response.Write("Invalid UserName&Password");
        else
            Response.Write("Valid-UserId=" + dr["PKUserId"]);

        addDetailsConnection.Close();
    }
}



我在这行出现错误.



I got an error this line.

if (!dr.Read() || dr["EMP_ENO"].ToString() != txtPassword.Text)



对象引用未设置为对象的实例.



Object reference not set to an instance of an object.

推荐答案



首先,我想发表评论.如我所见,您正在使用firebird db,因此请从问题中删除SQL2008标记.
其次,将数据读取器设置为null,然后使用它.这显然是对象引用未设置为对象的实例"异常...

解决方案...

使用FbCommand ExecuteReader方法,然后使用阅读器读取返回的记录...
Hi,

First, I want to comment something. You are using firebird db as I can see, so remove SQL2008 tag from question.
Second, You set your data reader to null and then you are using it... This is obviously "Object reference not set to an instance of an object" exception...

Solution...

Use FbCommand ExecuteReader method and then use reader to read returned records...
string ConnectionString = "Your conn string";
FbConnection addDetailsConnection = new FbConnection(ConnectionString);
addDetailsConnection.Open();
FbCommand fbCommand = new FbCommand("Your sql command string here", addDetailsConnection);
FbDataReader fbDataReader = fbCommand.ExecuteReader();

// Add your code that uses data reader...

// And add cleanup code...
fbDataReader.Close();
addDetailsConnection.Close();


添加方括号以使其成为方法调用:
Add the brackets that make it a method call:
FbDataReader dr = addDetailsConnection.Open;

成为

FbDataReader dr = addDetailsConnection.Open();


请尝试以下操作:

please try by the following :

protected void txtLogin_Click(object sender, EventArgs e)
{
string ConnectionString = "User ID=SYSDBA;Password=masterkey;" + "Database=localhost:C:\\Program Files\\Attend HRM\\Data\\ITAS.FDB; " + "DataSource=localhost;Charset=NONE;";
SqlConnection cn = new SqlConnection(ConnectionString );
cn.Open();
string SQLCommandText = "Select EMP_ID,NAME 

需要添加密码字段

from UserProfile where UsaeName= '" + txtEmpId.Text + "'";
 DataTable dt= new DataTable();
 sda = new SqlDataAdapter(SQLCommandText , cn);
  sda.Fill(dt);
if(dt.Rows.Count >0){
    if (dt.Rows[0]["PassWord"].ToString() != txtPassWord.Text)
Response.Write("Invalid UserName&Password");
else

Response.Write("Valid-UserId=" + dt.Rows[0]["EMP_ID"]);
}
con.Close();
}
}


希望有帮助,
Theingi.


Hope Be helpful,
Theingi.


这篇关于我在程序中遇到此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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