简体   繁体   中英

“Cannot find Table[0]” - Only in production?

Some code on our production server is suddenly giving us some trouble. Production has not been changed in about a year, and I've confirmed that the database has not changed either.

If I run the same code on my machine (Yay Source Control!), I do not get the error that appears in production and everything works fine. I'll post the code below, but there has to be something else going on.

Correct me if I'm wrong, but if there is no Table[0] , that means my query isn't returning any data. Yet, running the same query directly through SQL Management Studio gives me the data I want.

    var ds = GetData(queryString);
    if (ds.Tables.Count > 0)
    {
        var ddlDataSet = GetAdds();
    }
    private List<tAdd> GetAdds()
    {
        var ds = GetData(queryString);
        var aList = new tAdd[ds.Tables[0].Rows.Count];//THIS IS WHERE ERROR HITS
        //Do other stuff
        ...
    }

    private DataSet GetData(string queryString)
    {
        var connectionString =
              ConfigurationManager.ConnectionStrings["constring"].ConnectionString;

        var ds = new DataSet();

        try
        {
            var connection = new SqlConnection(connectionString);
            var adapter = new SqlDataAdapter(queryString, connection);

            adapter.Fill(ds);
        }
        catch (Exception ex)
        {
            ErrorPanel.Visible = true;
            ErrorPanel.Enabled = true;
            SearchPanel.Enabled = false;

            const string NotificationsEmail = "mailto:emailguy@email.com";
            ErrorAlertLabel.Text =
                "An err happened. " +
                "Please contact the people who do stuff ";
            ErrorAlertLabel.Visible = true;
            ErrorMessageLiteral.Text = "<br />" + "<br />" +
                                       "Message: " + ex.Message + "<br />" +
                                       "StackTrace: " + ex.StackTrace + "<br />" +
                                       "Inner Exception: " + ex.InnerException + "<br />" +
                                       "Full Detals: " + ex + "<br />";
            ErrorMessageLiteral.Visible = true;
        }

        return ds;
     }

I may just try republishing the same version again, but who knows if that will fix it. If anyone needs more info please let me know and thank you in advance.

Actual Error Text: "System.IndexOutOfRangeException: Cannot find table 0."

I think you still have a flaw in your error handling that got exposed.

In GetData , you catch any Exception that occurs and set a bunch of UI elements to Visible and populate them with information, but you don't stop the process from continuing . GetData just continues on after the catch block, and returns an empty DataSet . GetAdds is oblivious to the fact that an error occured, tries to access a table in the DataSet that doesn't exist, and throws another exception that is not handled by your code, but is instead handled by ASP.NET, which throws away all of your error information and just shows a generic error page.

I would not use such detailed error handling within a low-level method, but add more global error handling. Since you don't add any meaningful information to the exception, I would just let it bubble up and handle it at the application level.

If you want to add more information to a low-level exception, throw a new exception with more detail, and assign the original exception to the new exception's InnerException property.

Please check whether you have any pdb files in production , if you dont have them than go to your project right click on properties under Build -> Advanced -> debug info -> pdb build this and please run it production,

If you have these than you can get the exact line where it is breaking.

This might occure,if there is a dead lock on the tables on production. or the permissions are missing.

Got it working today. I finally got a-hold of our Web Admin and asked him to restart the web server - this after I'd confirmed that my code and the DB were not the problem.

As soon as the web server booted back up everything was working fine. There are quite a few applications running on the server, so it may have been something getting tangled up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM