DataTable () load limit error

I wrote a method below, using reflection to load a few strongly typed data into a .NET application. If I worked, as shown below, everything works - including no exceptions. But if I use the comment part (keeping everything else the same), then I get the Constraint inclusion error described here: enter the link description here .

If I look at what is inside the errors array, it is always the following:

"Column 'AEDelegateName' does not allow DBNull.Value." 

and ItemArray for the error will look something like this:

 [0] = {} [1] = "Some Value" 

This surprises me, since I was expecting only 1 column in a script that selects 1 column, not 2, as mentioned above. I also think this is close to the problem, as one of them seems to be null.

My script does not return null, and I can quickly and visually confirm it, and also say that in the query you use, I like NOT NULL .

 private void GetData(string query, Component tableAdapter) { OracleCommand command = new OracleCommand(); command.Connection = conn; command.CommandText = query; command.CommandType = CommandType.Text; command.CommandTimeout = 3000; OracleDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult); MethodInfo[] methods = tableAdapter.GetType().GetMethods(); MethodInfo getDataMethod = tableAdapter.GetType().GetMethod("GetData"); DataTable table = (DataTable)getDataMethod.Invoke(tableAdapter, null); Type[] paramTypes = new Type[] { table.GetType() }; MethodInfo updateMethod = tableAdapter.GetType().GetMethod("Update", paramTypes); foreach (DataRow row in table.Rows) { row.Delete(); } //try //{ // if (reader.HasRows) // { // table.Load(reader, LoadOption.OverwriteChanges, FillErrorHandler); // } //} //catch (Exception e) //{ // DataRow[] errors = table.GetErrors(); //} while (reader.Read()) { try { List<object> newRow = new List<object>(); for (int i = 0; i < reader.FieldCount; ++i) { object currentValue = reader.GetValue(i); Debug.WriteLine("Value: "+currentValue); newRow.Add(currentValue); } table.Rows.Add(newRow.ToArray()); } catch (ConstraintException e) { DataRow[] errors = table.GetErrors(); } } updateMethod.Invoke(tableAdapter, new object[]{table}); reader.Close(); } 
+6
source share
1 answer

In the documentation for the DataTable.Load Method (IDataReader, LoadOption) , I suspect that you may encounter the behavior described in the excerpt below. Have you checked the number of columns returned from your query and the number of columns in your DataTable? Does the name of the column returned from the query match the name of the column in your DataTable?

Condition: The schemas are compatible, but the loaded result set schema contains fewer columns than the DataTable.

Behavior: If the missing column has a default value or the column data type is null, the Load method allows rows to be added, replacing the default or null value for the missing column. If the default or zero cannot be used, then the Load throws method will throw an exception. If no default value is specified, the load method uses a null value as the default value.

Your code in the while loop probably works because it is not trying to map the schema. It simply fills the values ​​positionally and will be successful until the types are compatible, no restrictions are violated, and the array will not contain more elements than rows with columns.

+1
source

All Articles