简体   繁体   中英

Why are my SQL Connections not returning requested columns?

I have an ASP.NET website (running v2.0) and a SQL Server database (2012) and I'm getting a strange error when many people hit the page. The error is:

Column 'product_code' does not belong to table .  // occurs on line 'if (!row.IsNull("product_code") && !row.IsNull("state"))' below

Which seems to indicate there's some sort of connection sharing going on between threads, but I can't seem to figure out why. In the code behind I have:

private Dictionary<string, string> _saleStates = null;
protected Dictionary<string, string> SalesStates {
    get {
        if (_saleStates == null)
        {
            _saleStates = new Dictionary<string, string>();
            string sql = @"SELECT [product_code], [state] FROM [jsb_store_items]";
            using (DataTable tbl = new DataTable())
            {
                using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Shop.DbConnection"].ConnectionString))
                {
                    using (SqlCommand cmd = new SqlCommand(sql, conn))
                    {
                        using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                        { adapter.Fill(tbl); }
                    }
                }
                if (tbl.Rows.Count > 0)
                {
                    foreach (DataRow row in tbl.Rows)
                    {
                        if (!row.IsNull("product_code") && !row.IsNull("state")) // Error thrown here
                        { _saleStates.Add(row["product_code"].ToString().ToLower(), salesStateToText(row["state"].ToString().ToLower())); }
                    }
                }
            }
        }
        return _saleStates;
    }
}

protected string getSalesStates() {
    string output = "";
    int i = 0;
    foreach (KeyValuePair<string, string> entry in SalesStates) {
        output += ((i!=0) ? "," : "") + "\"" + entry.Key + "\":\"" + entry.Value + "\"";
        i++;
    }
    output = "{" + output + "}";
    return output;
}

and in the markup I have:

<script type="text/javascript">var sales_states = <% Response.Write(this.getSalesStates()); %>;</script>

If someone can help me figure out why this is happening I'd really appreciate it.

Since you are just populating a dictionary, try using a SqlDataReader instead of the DataTable and SqlDataAdapter. You can then loop through the resultset. This should be a bit faster and do what you need.

Try adding [dbo]. prefix to tablename, like this:

string sql = @"SELECT [product_code], [state] FROM [dbo].[jsb_store_items]";

Odd things can happen without the dbo prefix, especially if you don't have direct control of creating the objects yourself, such as in a production environment.

I have tested the code you have posted on my machine and it works. Need database details (eg table,fields etc.) also the connection string which you have used to get the data from the database.To see why issue occurs?

So the answer was kinda silly. In production we have two webservers behind a load-balancer and at some point I decided to test the two servers independently by hitting them directly. This allowed me to discover one server was throwing the error 90% of the time and one server next to 0% of the time. I restarted the website and app pools for the server that was throwing it often and it seems to have stopped.

One of the things that clued me in was that I had updated my error logging routines to try and log additional info about the error and I was still seeing the error being thrown without the newer details.

I think the issue is that the dlls being used were old despite the code being updated. This is a web site and not a web application so the code gets compiled on first request. By resetting the site I think it re-compiled everything and things started working as intended.

I am currently rewriting our deployment scripts to ensure the app pools and web site are reset/recycled on code changes to try and prevent this issue in the future.

Thank you for all your responses and I hope this helps someone else at some point.

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