Adding NULL to DataTable Row?

J

Joe Keller

Hello,

I'd like to add a "NULL" value to a DataTable row that is expecting an
Int32. The following code works fine when I want to add a value of "32" to
the field..

1) DataSet myDataSet = new DataSet("dsTest");
2) DataTable myDataTable = myDataSet.Tables.Add("tblTest");
3) DataColumn myDataColumn = myDataTable.Columns.Add("colOne");
4) myDataColumn.DataType = System.Type.GetType("System.Int32");
5) myDataColumn.AllowDBNull = true;
6) string[] sTemp = new string[1];
7) sTemp[0] = "32";
8) myDataTable.Rows.Add(sTemp);

However, if I change line 7 to read sTemp[0] = "" (to reflect that the value
is NULL) I get a format exception. How do I add a NULL value to a field in
a DataRow?

Thanks,

Joe
 
P

Paul [Paradise Solutions]

People may correct me if I'm wrong, but wouldn't "" just represent an
empty string rather than NULL? Have you tried using DBNULL? I don't
know ~how~ you use it (not needed to recently), but it may yield better
results.

Paul
 
J

Jon Skeet [C# MVP]

Joe Keller said:
I'd like to add a "NULL" value to a DataTable row that is expecting an
Int32. The following code works fine when I want to add a value of "32" to
the field..

1) DataSet myDataSet = new DataSet("dsTest");
2) DataTable myDataTable = myDataSet.Tables.Add("tblTest");
3) DataColumn myDataColumn = myDataTable.Columns.Add("colOne");
4) myDataColumn.DataType = System.Type.GetType("System.Int32");

typeof(int) is a safer way of doing this.
5) myDataColumn.AllowDBNull = true;
6) string[] sTemp = new string[1];
7) sTemp[0] = "32";
8) myDataTable.Rows.Add(sTemp);

However, if I change line 7 to read sTemp[0] = "" (to reflect that the value
is NULL) I get a format exception. How do I add a NULL value to a field in
a DataRow?

Have you tried using DBNull.Value? Not sure why you're passing in an
array of strings to start with though: I'd use

myDataTable.Rows.Add (new object[] {32});
or
myDataTable.Rows.Add (new object[] {DBNull.Value});
 
J

Joe Keller

Yes - thanks for the help - I've found the problem.

Line 6 should read:
object[] sTemp = new object[1];

and line 7 should read:
sTemp[0] = Convert.DBNull;

Thanks for the suggestion on "TypeOf" also :)

Joe

I need to use an array of objects
Jon Skeet said:
Joe Keller said:
I'd like to add a "NULL" value to a DataTable row that is expecting an
Int32. The following code works fine when I want to add a value of "32"
to
the field..

1) DataSet myDataSet = new DataSet("dsTest");
2) DataTable myDataTable = myDataSet.Tables.Add("tblTest");
3) DataColumn myDataColumn = myDataTable.Columns.Add("colOne");
4) myDataColumn.DataType = System.Type.GetType("System.Int32");

typeof(int) is a safer way of doing this.
5) myDataColumn.AllowDBNull = true;
6) string[] sTemp = new string[1];
7) sTemp[0] = "32";
8) myDataTable.Rows.Add(sTemp);

However, if I change line 7 to read sTemp[0] = "" (to reflect that the
value
is NULL) I get a format exception. How do I add a NULL value to a field
in
a DataRow?

Have you tried using DBNull.Value? Not sure why you're passing in an
array of strings to start with though: I'd use

myDataTable.Rows.Add (new object[] {32});
or
myDataTable.Rows.Add (new object[] {DBNull.Value});
 
J

Jon Skeet [C# MVP]

Joe Keller said:
Yes - thanks for the help - I've found the problem.

Line 6 should read:
object[] sTemp = new object[1];

You still don't need the temporary variable though - just create the
array in the method call, as I showed before.
and line 7 should read:
sTemp[0] = Convert.DBNull;

Thanks for the suggestion on "TypeOf" also :)

I think using Convert.DBNull is less idiomatic than DBNull.Value,
myself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top