Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Code:
C#
public string Login_Handler(string Username, string password, out string Details)
   {
       Database dblogin = null;
       DbConnection dbconn = null;
       DbCommand lgncmd;
       string Sp_login = string.Empty;
       string Str_Details = string.Empty;
       try
       {
           if (dbconn.State == ConnectionState.Closed)
           {
               dblogin = DatabaseFactory.CreateDatabase();
               dbconn = dblogin.CreateConnection();
               dbconn.Open();
           }

           lgncmd = dbconn.CreateCommand();
           Sp_login = "exec usp_login";
           lgncmd.CommandType = CommandType.StoredProcedure;
           lgncmd = dblogin.GetStoredProcCommand(Sp_login);

           Username = (string.IsNullOrEmpty(Username) ? string.Empty : Username.Trim());
           password = (string.IsNullOrEmpty(password) ? string.Empty : password.Trim());

           dblogin.AddInParameter(lgncmd, "@uname", DbType.String, Username.Trim());
           dblogin.AddInParameter(lgncmd, "@pass", DbType.String, password.Trim());

           dblogin.AddOutParameter(lgncmd, "@id", DbType.Int16, 10);

           dblogin.ExecuteNonQuery(lgncmd);

           Details = Convert.ToString(lgncmd.Parameters["@id"].Value);
       }
       catch (Exception ex)
       {

           throw ex;
       }
       finally
       {
           if (dbconn.State == ConnectionState.Open)
           {
               dbconn.Close();
               dbconn.Dispose();
               dblogin = null;
           }
       }
       return Str_Details;
   }


Error:
object reference not set to an instance of an object

error in the line DbConnection and Database. I also used the proper dll for those commands but still it is showing error.
Posted
Updated 7-Feb-14 1:22am
v2
Comments
Ajay_Babu 7-Feb-14 7:18am    
which line it shows error?
riodejenris14 7-Feb-14 7:20am    
In the if condition Mr.Ajay, and it is moving to catch block with the message object reference not set to an instance of an object

1 solution

If you are "always getting struck" by the same error, then it's probably time you stopped doing that...
Look at your code:
C#
Database dblogin = null;
DbConnection dbconn = null;
DbCommand lgncmd;
string Sp_login = string.Empty;
string Str_Details = string.Empty;
try
{
    if (dbconn.State == ConnectionState.Closed)
You create a variable to reference a DbConnection (dbconn), and you set it to null.
The next time you use it, you try to access a property. So the system looks at it, and sees it is null - i.e. has no instance - and rightly throws an exception.

You need to create an instance of your DbConnection class, and assign it to dbconn before you can use it. Probably, along the lines of this:
C#
DbConnection dbconn = new DbConnection();


If you think of cars instead of variables, a parking space is a place to store an instance of a car: but you can't drive away while the space is empty, because it doesn't have a car in it to drive! You have to go to a parking space that contains a car (preferably yours) before you can drive off.
 
Share this answer
 
Comments
Sumit_Pathak 7-Feb-14 7:29am    
+5 for real time examples..
riodejenris14 7-Feb-14 7:30am    
No Mr.Original Griff I tried it before, it also showing the error as "Cannot create an instance of the abstract class or interface 'Microsoft.Practices.EnterpriseLibrary.Data.Database'". That why I tried with null.
OriginalGriff 7-Feb-14 7:35am    
You can't create an instance of *any* abstract class (or of an Interface) - you need to find teh appropriate derived class and create an instance of that.

Everything in C# is based around instances - if you want to use a class, you need to have an instance to work from (except for static members, but a database wouldn't be static as you might well need more than one at a time).

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