0

I have recently placed a web application on a remote server for testing. This application uses a range of SQL Server (Express) databases to hold card and user information, of which all are linked into my master database. The database is referred to in my web.config file.

I have made .bak files from my databases and restored them on my server, with the connection string now showing:

<add key="ConnectionString" value="server=localhost; database=DatabaseMaster; uid=...; pwd=..."/>

On my local computer the application is fine and throws no exceptions. However, upon connecting to my application via the web server, and trying to retrieve data from my cards table a NullReferenceException error is thrown. I have checked my code via breakpoints on the following code:

    private void FillGrid()
    {
        DataSet ds = new DataSet();
        //get data from session
        ds = (DataSet)SessionNavigator.GetDataFromCurrentPage
             (PageParams.Customer.DataCards);

        if (ds != null)
        {
            DataView dv = ds.Tables["Cards"].DefaultView;
            gridCtrl.RowsCount = dv.Count;
            gridCtrl.BindGrid(dv);
        }
    }

My checks have found that the DataSet ds is indeed not null, and in fact it seems that the application is not recognising the table "Cards", there being no reference to an instance of the object in the table.

Confusingly enough, other tables (such as for users) have no problems whatsoever on the server. Also, manipulating data related to the card objects (such as making transactions and changing points values) are reflected in the SQL Server Management Studio.

I am inexperienced with SQL Server so I may be wrong but I don't think it has anything to do with the database itself.

So SO, are there any glaringly obvious steps that I may have missed when setting up the application that are causing these issues? If so, are there any reference materials that you can recommend?

Edit: After Searching through the PageParams Enumerable and looking closely at the ds Dataset, I have found that ds is not null but has a value of {System.Data.DataSet} containing System.Data.DataTableCollection with a list of size 0.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
Patrick Developer
  • 369
  • 2
  • 7
  • 21
  • Well what does the code in GetDataFromCurrentPage look like? – Jon Skeet Dec 13 '13 at 11:54
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Dec 13 '13 at 12:07
  • This has nothing to do with SQL Server. Where does the exception occur? What do you see in the call stack? What is PageParams? Are you sure it or PageParams.Customer aren't null? – Panagiotis Kanavos Dec 13 '13 at 12:12
  • I have been looking at PageParams.Customer to check and it seems that it is null, I'm just trying to figure out why it is null on the server but fine on my local computer – Patrick Developer Dec 13 '13 at 12:16
  • How very odd. I have spent a while looking at the PageParams, which contains my enumerable values for a customer (Including DataCards), but when debugging the session navigator only searches as far as PageParams, when it should be PageParams.Customer.DataCards. – Patrick Developer Dec 13 '13 at 15:36

2 Answers2

2

Found the Answer! Searching through my output when debugging the FillGrid method I found this little line:

System.Data.SqlClient.SqlException: SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online

After that it was just a simple case of allowing Ad Hoc Distributed Queries via sp_configure in the SQL Server Management Studio, resolving the issue completely. This explains why my local copy of the application was working fine but the server was having issues retrieving the relevant DataCards.

Patrick Developer
  • 369
  • 2
  • 7
  • 21
  • The Output window in Visual Studio shows all exceptions: handled and unhandled. Apparently NullReferenceException crashed your application, but the root cause (SqlException) did not. Have you got a nonspecific exception handler that suppresses SqlException instead of reporting it and stopping execution? – groverboy May 15 '14 at 19:57
0

It might just be that the name of the table isn't read from the source, and that it's just called "Table1" in your DataSet.

Have you tried to look at the actual content of the dataset?

FoxHound
  • 380
  • 3
  • 17
  • The content on the DataSet on my local computer is a list of Count = 1, which contains Cards. On the server it is null – Patrick Developer Dec 13 '13 at 12:25
  • I meant with the dataset visualizer. When debugging break on a point where your dataset variable is filled, hover over the variable and press the magnifier icon. You should be able to see what data is stored in the dataset, visible as tables with records. The tables contain the names that you need to use – FoxHound Dec 13 '13 at 12:28
  • Disregard my previous suggestion. It seems that the issue lies with filling the DataSet. – FoxHound Dec 13 '13 at 12:37