This issue occurs when inserting a row from the DataTable into the corresponding table contained in the Advanced SQL Engine. The DataTable contains columns that have data types that are provider specific types, and the corresponding columns in the underlying SQL table are defined as NOT NULL and with a DEFAULT value. When a row from the DataTable is inserted into the SQL table the provider will return a TdException that indicates that a Null value cannot be inserted.
This issue occurs with all Teradata provider releases.
When the following conditions exist a TdException will be thrown when DbDataAdapter.Update has been called by an application:
In the information that is sent to Teradata, the provider will indicate that the parameter is NOT NULL. However, a null value is sent to Teradata. There is a conflict between the value and the information about the parameter. This causes Teradata to send an error indicating that the value cannot be null.
The following is an example of code that duplicates this problem:
C# |
Copy Code |
---|---|
public static void Example1() { // The definition of the table used in this example is // CREATE TABLE ProblemTable( // C1 INTEGER, // C2 DATE NOT NULL DEFAULT DATE'2008-10-10', // C3 DECIMAL(20, 4)) // Setting up the log on string TdConnection cn = new TdConnection(); TdConnectionStringBuilder tsb = new TdConnectionStringBuilder(); tsb.DataSource = "teradata"; tsb.UserId = "user"; tsb.Password = "password"; // Open a connection to the Advanced SQL Engine cn.ConnectionString = tsb.ConnectionString; cn.Open(); // Setting up the SELECT statement that will be used to fill the DataTable TdCommand cmd = cn.CreateCommand(); cmd.CommandText = "select c1, c2, c3 from ProblemTable"; // Setting the data adapter and command builder TdDataAdapter da = new TdDataAdapter(cmd); da.ReturnProviderSpecificType = true; TdCommandBuilder cb = new TdCommandBuilder(da); DataTable dt = new DataTable("ProblemTable"); // Filling the DataTable da.Fill(dt); // Adding a row to the DataTable // Column C2 will not be set. It will default to the TdDate.Null value. DataRow dr = dt.NewRow(); dr["C1"] = 200; dr["C3"] = new TdDecimal(321.32); dt.Rows.Add(dr); try { // A TdException will get thrown when Update is called da.Update(dt); } catch (TdException e) { // This exception was expected to be thrown Console.WriteLine(e.Message) } } |
There are several work arounds for this issue:
da.Fill(dt)
dt.Columns[1].DefaultValue = new TdDate(2008, 10, 10);
System.Data.DataTable
TdDataReader.GetSchemaTable
TdParameter
TdCommandBuilder
TdDataAdapter