简体   繁体   中英

SqlDataAdapter Fill gives NULL primary keys

So I have a bit of a head scratcher I am looking for help with. We recently moved an ASP.Net web forms app from .Net 3.5, Win 2003, and SQL 2008 to .Net 4.5, Win 2008, and SQL 2012. The app ran for 4+ years on the old platform without any issues. On the new platform we are experiencing an intermittent problem that causes it to crash. We have added error trapping to isolate where exactly the problem is occurring and what we are seeing does not make any sense.

The line throwing the error is in our DAL, where it queries a table to get the list of primary keys related to a specified parent key:

Select 
    ItemKey
From Items
Where ItemParentKey = @Key

Create Table Items
(
    ItemKey int identity(1,1) not null
    ,ItemParentKey int
    ,constraint PK_Items primary key clustered (ItemKey)
)

On the .net side we are using the fill method of a SQL data adapter to call a stored procedure containing the above select statement, and load the results into a data table. The error occurs when we try to loop through the rows of the datatable and do something with the keys, all of the keys are null, which causes an invalid cast exception. The data table is defined in an xsd file with a single integer column called Key.

Adapter.Fill(data);
foreach(KeyListRow Row in data.KeyList)
{
    Item.Children.Add(Row.Key);  //Invalid cast exception
}

Should note that it is actually the designer generated code which throws the exception while trying to convert to the strongly typed column, this where we are able to catch it.

Putting a try catch around this and adding code to log the details of the query parameter and results shows that the query is returning the correct number of rows for the parameter value, but they are all coming back null… (which should not be possible with the above query and table definition)

Couple other points, this is intermittent, it will work for days or weeks without an issue, and then crash every hour for a week. Manually executing the stored proc and examining the data for the record that is causing the error while the app is down shows the expected values, not what the app is reporting as the results in the logs. Restarting the application pool for the site will clear the problem and bring us back up, until it starts happening again. There seems to be a slight correlation between the traffic levels and how often the app goes down, but nothing we have been able to concretely isolate as of yet.

Any suggestions as to probable cause or further avenues of investigation?


EDIT 2015-03-16

Exception Details:

ExceptionType - ExceptionMessage (drilling through the inner exceptions)

  1. System.Web.HttpUnhandledException - Exception of type 'System.Web.HttpUnhandledException' was thrown.
  2. System.Data.StrongTypingException - The value for column 'Key' in table 'KeyList' is DBNull.
  3. System.InvalidCastException - Specified cast is not valid.

Data being returned:

data.KeyList.Count: 7;
data.KeyList[0].Key: NULL ;
data.KeyList[1].Key: NULL ;
data.KeyList[2].Key: NULL ;
data.KeyList[3].Key: NULL ;
data.KeyList[4].Key: NULL ;
data.KeyList[5].Key: NULL ;
data.KeyList[6].Key: NULL ;

Designer Generated Code:

public partial class dsXXXX : global::System.Data.DataSet
{

    private KeyListDataTable tableKeyList;

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
    [global::System.ComponentModel.Browsable(false)]
    [global::System.ComponentModel.DesignerSerializationVisibility(global::System.ComponentModel.DesignerSerializationVisibility.Content)]
    public KeyListDataTable KeyList {
        get {
            return this.tableKeyList;
        }
    }

    /// <summary>
    ///Represents the strongly named DataTable class.
    ///</summary>
    [global::System.Serializable()]
    [global::System.Xml.Serialization.XmlSchemaProviderAttribute("GetTypedTableSchema")]
    public partial class KeyListDataTable : global::System.Data.TypedTableBase<KeyListRow> {

        private global::System.Data.DataColumn columnKey;

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public KeyListDataTable() {
            this.TableName = "KeyList";
            this.BeginInit();
            this.InitClass();
            this.EndInit();
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        internal KeyListDataTable(global::System.Data.DataTable table) {
            this.TableName = table.TableName;
            if ((table.CaseSensitive != table.DataSet.CaseSensitive)) {
                this.CaseSensitive = table.CaseSensitive;
            }
            if ((table.Locale.ToString() != table.DataSet.Locale.ToString())) {
                this.Locale = table.Locale;
            }
            if ((table.Namespace != table.DataSet.Namespace)) {
                this.Namespace = table.Namespace;
            }
            this.Prefix = table.Prefix;
            this.MinimumCapacity = table.MinimumCapacity;
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        protected KeyListDataTable(global::System.Runtime.Serialization.SerializationInfo info, global::System.Runtime.Serialization.StreamingContext context) : 
                base(info, context) {
            this.InitVars();
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public global::System.Data.DataColumn KeyColumn {
            get {
                return this.columnKey;
            }
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        [global::System.ComponentModel.Browsable(false)]
        public int Count {
            get {
                return this.Rows.Count;
            }
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public KeyListRow this[int index] {
            get {
                return ((KeyListRow)(this.Rows[index]));
            }
        }

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public event KeyListRowChangeEventHandler KeyListRowChanging;

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public event KeyListRowChangeEventHandler KeyListRowChanged;

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public event KeyListRowChangeEventHandler KeyListRowDeleting;

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public event KeyListRowChangeEventHandler KeyListRowDeleted;

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public void AddKeyListRow(KeyListRow row) {
            this.Rows.Add(row);
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public KeyListRow AddKeyListRow(int Key) {
            KeyListRow rowKeyListRow = ((KeyListRow)(this.NewRow()));
            object[] columnValuesArray = new object[] {
                    Key};
            rowKeyListRow.ItemArray = columnValuesArray;
            this.Rows.Add(rowKeyListRow);
            return rowKeyListRow;
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public override global::System.Data.DataTable Clone() {
            KeyListDataTable cln = ((KeyListDataTable)(base.Clone()));
            cln.InitVars();
            return cln;
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        protected override global::System.Data.DataTable CreateInstance() {
            return new KeyListDataTable();
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        internal void InitVars() {
            this.columnKey = base.Columns["Key"];
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        private void InitClass() {
            this.columnKey = new global::System.Data.DataColumn("Key", typeof(int), null, global::System.Data.MappingType.Element);
            base.Columns.Add(this.columnKey);
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public KeyListRow NewKeyListRow() {
            return ((KeyListRow)(this.NewRow()));
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        protected override global::System.Data.DataRow NewRowFromBuilder(global::System.Data.DataRowBuilder builder) {
            return new KeyListRow(builder);
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        protected override global::System.Type GetRowType() {
            return typeof(KeyListRow);
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        protected override void OnRowChanged(global::System.Data.DataRowChangeEventArgs e) {
            base.OnRowChanged(e);
            if ((this.KeyListRowChanged != null)) {
                this.KeyListRowChanged(this, new KeyListRowChangeEvent(((KeyListRow)(e.Row)), e.Action));
            }
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        protected override void OnRowChanging(global::System.Data.DataRowChangeEventArgs e) {
            base.OnRowChanging(e);
            if ((this.KeyListRowChanging != null)) {
                this.KeyListRowChanging(this, new KeyListRowChangeEvent(((KeyListRow)(e.Row)), e.Action));
            }
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        protected override void OnRowDeleted(global::System.Data.DataRowChangeEventArgs e) {
            base.OnRowDeleted(e);
            if ((this.KeyListRowDeleted != null)) {
                this.KeyListRowDeleted(this, new KeyListRowChangeEvent(((KeyListRow)(e.Row)), e.Action));
            }
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        protected override void OnRowDeleting(global::System.Data.DataRowChangeEventArgs e) {
            base.OnRowDeleting(e);
            if ((this.KeyListRowDeleting != null)) {
                this.KeyListRowDeleting(this, new KeyListRowChangeEvent(((KeyListRow)(e.Row)), e.Action));
            }
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public void RemoveKeyListRow(KeyListRow row) {
            this.Rows.Remove(row);
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public static global::System.Xml.Schema.XmlSchemaComplexType GetTypedTableSchema(global::System.Xml.Schema.XmlSchemaSet xs) {
            global::System.Xml.Schema.XmlSchemaComplexType type = new global::System.Xml.Schema.XmlSchemaComplexType();
            global::System.Xml.Schema.XmlSchemaSequence sequence = new global::System.Xml.Schema.XmlSchemaSequence();
            dsXXXX ds = new dsXXXX();
            global::System.Xml.Schema.XmlSchemaAny any1 = new global::System.Xml.Schema.XmlSchemaAny();
            any1.Namespace = "http://www.w3.org/2001/XMLSchema";
            any1.MinOccurs = new decimal(0);
            any1.MaxOccurs = decimal.MaxValue;
            any1.ProcessContents = global::System.Xml.Schema.XmlSchemaContentProcessing.Lax;
            sequence.Items.Add(any1);
            global::System.Xml.Schema.XmlSchemaAny any2 = new global::System.Xml.Schema.XmlSchemaAny();
            any2.Namespace = "urn:schemas-microsoft-com:xml-diffgram-v1";
            any2.MinOccurs = new decimal(1);
            any2.ProcessContents = global::System.Xml.Schema.XmlSchemaContentProcessing.Lax;
            sequence.Items.Add(any2);
            global::System.Xml.Schema.XmlSchemaAttribute attribute1 = new global::System.Xml.Schema.XmlSchemaAttribute();
            attribute1.Name = "namespace";
            attribute1.FixedValue = ds.Namespace;
            type.Attributes.Add(attribute1);
            global::System.Xml.Schema.XmlSchemaAttribute attribute2 = new global::System.Xml.Schema.XmlSchemaAttribute();
            attribute2.Name = "tableTypeName";
            attribute2.FixedValue = "KeyListDataTable";
            type.Attributes.Add(attribute2);
            type.Particle = sequence;
            global::System.Xml.Schema.XmlSchema dsSchema = ds.GetSchemaSerializable();
            if (xs.Contains(dsSchema.TargetNamespace)) {
                global::System.IO.MemoryStream s1 = new global::System.IO.MemoryStream();
                global::System.IO.MemoryStream s2 = new global::System.IO.MemoryStream();
                try {
                    global::System.Xml.Schema.XmlSchema schema = null;
                    dsSchema.Write(s1);
                    for (global::System.Collections.IEnumerator schemas = xs.Schemas(dsSchema.TargetNamespace).GetEnumerator(); schemas.MoveNext(); ) {
                        schema = ((global::System.Xml.Schema.XmlSchema)(schemas.Current));
                        s2.SetLength(0);
                        schema.Write(s2);
                        if ((s1.Length == s2.Length)) {
                            s1.Position = 0;
                            s2.Position = 0;
                            for (; ((s1.Position != s1.Length) 
                                        && (s1.ReadByte() == s2.ReadByte())); ) {
                                ;
                            }
                            if ((s1.Position == s1.Length)) {
                                return type;
                            }
                        }
                    }
                }
                finally {
                    if ((s1 != null)) {
                        s1.Close();
                    }
                    if ((s2 != null)) {
                        s2.Close();
                    }
                }
            }
            xs.Add(dsSchema);
            return type;
        }
    }

    /// <summary>
    ///Represents strongly named DataRow class.
    ///</summary>
    public partial class KeyListRow : global::System.Data.DataRow {

        private KeyListDataTable tableKeyList;

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        internal KeyListRow(global::System.Data.DataRowBuilder rb) : 
                base(rb) {
            this.tableKeyList = ((KeyListDataTable)(this.Table));
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public int Key {
            get {
                try {
                    return ((int)(this[this.tableKeyList.KeyColumn]));
                }
                catch (global::System.InvalidCastException e) {
/*****Exception is thrown here*****/
                    throw new global::System.Data.StrongTypingException("The value for column \'Key\' in table \'KeyList\' is DBNull.", e);
                }
            }
            set {
                this[this.tableKeyList.KeyColumn] = value;
            }
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public bool IsKeyNull() {
            return this.IsNull(this.tableKeyList.KeyColumn);
        }

        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        public void SetKeyNull() {
            this[this.tableKeyList.KeyColumn] = global::System.Convert.DBNull;
        }
    }

}

We never did isolate the exact cause of this issue, but our server team moved some other applications to another server and this one started behaving again. So all indicators are that we were dealing with some form of resource starvation or similar conflict.

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