0

I got a big problem this morning. My SQL Server had a HUGE bug yesterday and I had to reinstall it.

On my client side, when I try to login, and put the password also i always receive this error:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

    public User GetUser(string username, string password)
    {
        // Get the User

        dynamic queryStringLogon = ("SELECT Login, Id, Password, BugLogin, Guid FROM Users WHERE (Login LIKE '" + username + "') ");

        SqlCommand theCommand = new SqlCommand(queryStringLogon);

        SqlDataReader reader = GetData(theCommand);

Line 31:             User user = null; 
Line 32:  
Line 33:             if (reader.HasRows) 
Line 34:             { 
Line 35:                user = new User();

It's like nothing is coming back from my sql database... That's my connection string . I really don't know what's wrong with it .

<connectionStrings>
   <remove name="LocalSqlServer" />
   <add name="LocalSqlServer" 
        connectionString="data source=.\SQLEXPRESS;Initial Catalog=DataUi;Integrated Security=SSPI;User Id=clientportaluser;Password=aspen1aspen1;User Instance=true" 
        providerName="System.Data.SqlClient" />
</connectionStrings>

I also set the clientportaluser with dataread and datawrite access.

 private SqlDataReader GetData(SqlCommand command)
    {
        command.Connection = Connection;

        //using (Connection)
        //{

        try
        {

            if (Connection.State == System.Data.ConnectionState.Closed)
            {
                Connection.Open();
            }

            SqlDataReader reader = command.ExecuteReader();
            return reader;
        }
        catch (Exception e)
        {
            return null;
        }
        finally
        {
            // Connection.Close();
        }

        //}
    }

    private SqlConnection Connection
    {
        get
        {
            if (_sqlConnection == null)
            {
                _sqlConnection = new SqlConnection(_conString);
            }
            return _sqlConnection;
        }
    }

    private string _conString;

    private SqlConnection _sqlConnection;

FIXED : The error was users instance set to false .

Kiwimoisi
  • 4,086
  • 6
  • 33
  • 64
  • where do you declare your reader? Also where do you access the reader in the following lines? The information you have given so far is not conclusive enough to identify the problem. – seanzi Dec 01 '11 at 12:02
  • Edited on my post. But this was always working before my sql crashed on my server so i suppose the problem is when i added again this database . I missed something maybe ? – Kiwimoisi Dec 01 '11 at 12:06
  • I would step through the code and see what the GetData method is returning. If this returns a null and then you try to access the .HasRows property on the reader, you will encounter an null reference exception. – seanzi Dec 01 '11 at 12:07
  • But I think I dont get informations because nothing is returning from my Database =( – Kiwimoisi Dec 01 '11 at 12:08
  • What happens when you step through and debug? What line is it throwing the error on specifically in here? – Doozer Blake Dec 01 '11 at 12:09
  • when you are debugging and you hit the HasRows line, do a quick watch on the reader to ensure it is null. If this is the case you need to investigate the GetData method to see why it is returning null. – seanzi Dec 01 '11 at 12:12
  • Yes my reader is just null .. – Kiwimoisi Dec 01 '11 at 12:15
  • But everything was working perfectly this is why it's like weird ... – Kiwimoisi Dec 01 '11 at 12:18
  • Then the problem is inside GetData(theCommand); which is a part of the code we can't see. – MatthewMartin Dec 01 '11 at 12:39
  • I edited my first post . – Kiwimoisi Dec 01 '11 at 12:47
  • 1
    If you step through the GetData does the following line get hit? can you do a quick watch on e? `catch (Exception e) { return null; }` – seanzi Dec 01 '11 at 15:02
  • I used a tracer to see whats happening .. Error: 15372, Severity: 16, State: 1. 2011-12-01 16:12:08.52 Logon Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed. [CLIENT: ] – Kiwimoisi Dec 02 '11 at 10:19

3 Answers3

2

Why you have both "Integrated Security=SSPI;" and "User Id=clientportaluser;Password=aspen1aspen1;" toghether use one and then try.

1

If you are getting the error onconnect (please post the exception if one is generated) then it's likely that when you restored your database, you didn't add your user account back as a valid user. Please check that your user is entitled to access the database, and that it has sufficient permissions to execute/select whatever you are trying to do.

Look at the Security branch if you are using Sql Server Management Studio.

dash
  • 89,546
  • 4
  • 51
  • 71
  • Now I always get this error .. Error: 15372, Severity: 16, State: 1. 2011-12-01 16:12:08.52 Logon Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed. [CLIENT: ] – Kiwimoisi Dec 02 '11 at 10:17
  • 1
    Remove the User Instance part. See http://stackoverflow.com/questions/4770596/failed-to-generate-a-user-instance-of-sql-server-due-to-failure-in-retrieving-th – dash Dec 02 '11 at 22:19
1

If you fire up SQL Profiler is anything being sent to the database? Can you access the database via Management Studio?

Paul
  • 3,072
  • 6
  • 37
  • 58
  • Error: 15372, Severity: 16, State: 1. 2011-12-01 16:12:08.52 Logon Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed. [CLIENT: ] – Kiwimoisi Dec 02 '11 at 10:18
  • 1
    Is this also in the error log for sql server? If so, it sounds like sql server hasn't re-installed correctly. If profiler doesn't connect either then you can rule out your application – Paul Dec 02 '11 at 16:44