Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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.
C#
if (!dr.Read() || dr["EMP_ENO"].ToString() != txtPassword.Text)


Object reference not set to an instance of an object.
Posted
Updated 3-Mar-12 4:04am
v5
Comments
StM0n 3-Mar-12 3:54am    
Sorry... but which error do you get...

and is the gap after else in your original code?
Sergey Alexandrovich Kryukov 3-Mar-12 21:06pm    
Not a question. Again.
--SA

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...
C#
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();
 
Share this answer
 
v2
Add the brackets that make it a method call:
C#
FbDataReader dr = addDetailsConnection.Open;
Becomes
C#
FbDataReader dr = addDetailsConnection.Open();
 
Share this answer
 
Comments
2011999 3-Mar-12 3:25am    
again same Error
OriginalGriff 3-Mar-12 3:45am    
And what type does Open return?
I don't think it will be a data reader of any type...
please try by the following :

C#
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 
need to add PassWord fields
C#
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();
}
}

Hope Be helpful,
Theingi.
 
Share this answer
 
Comments
2011999 3-Mar-12 5:05am    
sda = new SqlDataAdapter(SQLCommandText, ConnectionString);

Compiler Error Message: CS0103: The name 'sda' does not exist in the current context
2011999 3-Mar-12 5:13am    
hear i have firebird i am using Fbdataadapter
2011999 3-Mar-12 5:13am    
not using sqlData adapter
Apparently, dr["EMP_ENO"] returns null, and the attempt to de-reference null in ToString throws this exception. You can see it under debugger.

You should always use the debugger on a slightest concern on your run-time behavior. And certainly before asking questions like that. You could find and fix the problem much faster than posting this question and waiting for the answer.

—SA
 
Share this answer
 
C#
if (!dr.Read() || dr["EMP_ENO"].ToString() != txtPassword.Text)


a little bit too early... first check

C#
if (!dr.Read())


then move to

C#
if  (dr["EMP_ENO"].ToString() != txtPassword.Text)
   Response.Write("Invalid UserName&Password");
 
Share this answer
 
Comments
2011999 3-Mar-12 8:34am    
Error 1 'FirebirdSql.Data.FirebirdClient.FbConnection' does not contain a definition for 'ExecuteReader' and no extension method 'ExecuteReader' accepting a first argument of type 'FirebirdSql.Data.FirebirdClient.FbConnection' could be found (are you missing a using directive or an assembly reference?) E:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\WebSites\Subbu\SelectEdit\DBdata2\EmpLeave.aspx.cs 25 49 E:\...\DBdata2\
2011999 3-Mar-12 8:35am    
how to resolve it
StM0n 3-Mar-12 9:00am    
Sorry, but is the code in your question correctly copied and pasted...?

And, do you have changed the .Net-Version recently?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900