Assign Null value to the Integer Column in the DataTable
Asked Answered
U

11

23

I have a datatable with One ColumnName "CustomerID" with Integer DataType. Dynamically I want to add rows to the DataTable. For that, I had created one DataRow object like:

  DataTable dt = new DataTable();
  DataRow DR = dt.NewRow();
  DR["CustomerID"] = Convert.ToInt32(TextBox1.Text);

But if the TextBox contains empty string, it throws the error. In that case, I want to assign Null value to the CustomerID. How to do this?

Ulrich answered 15/6, 2011 at 12:49 Comment(2)
@Marc why not you recommend him something instead using DataTable?Wilek
@Marc: Yes. Iam using DataTable.Ulrich
L
31

A null/empty string is in the wrong format; you would need to detect that scenario and compensate:

    DR["CustomerID"] = string.IsNullOrWhiteSpace(text)
        ? DBNull.Value : (object)Convert.ToInt32(text);
Latoyia answered 15/6, 2011 at 12:56 Comment(1)
If we have to construct that DataTable manually like MyDataTable.Columns.Add("CustomerID", typeof(int)), should we have that CustomerID field be defined as a Nullable type like int??Growth
G
5
DR["CustomerID"] = !string.IsNullOrEmpty(TextBox1.Text)
                   ? Convert.ToInt32(TextBox1.Text)
                   : DBNull.Value;

But you should check also that the value is a valid integer:

int value;
if(int.TryParse(TextBox1.Text, out value))
{
    DR["CustomerID"] = value;
}
else
{
    DR["CustomerID"] = DBNull.Value;
}
Guzel answered 15/6, 2011 at 12:53 Comment(0)
L
2

you could do it like that:

DR["CustomerID"] = string.IsNullOrEmpty(TextBox1.Text) ?
    null : Convert.ToInt32(TextBox1.Text);
Liquidize answered 15/6, 2011 at 12:53 Comment(0)
P
2

First of all, of course, the field needs to be set as nullable in the DB.

And then, set it to DBNull.Value

Photomontage answered 15/6, 2011 at 12:54 Comment(0)
N
2

You need to check first

if (TextBox1.Text.Length > 0)
{
   DR["CustomerID"] = Convert.ToInt32(TextBox1.Text); 
}
else
{
  DR["CustomerID"] = null;  
}
Ninette answered 15/6, 2011 at 12:55 Comment(0)
P
2
Int32 Temp = 0;
if !(Int32.TryParse(TextBox1.Text,Temp))
    DR["CustomerID"] = DBNull.Value
else
    DR["CustomerID"] = Temp
Philia answered 15/6, 2011 at 12:56 Comment(0)
L
1

You can use DBNull.

DR["CustomerID"] = (TextBox.Text.Length == 0) ? Convert.ToInt32(TextBox1.Text) : DBNull.Value;
Laktasic answered 15/6, 2011 at 12:53 Comment(0)
R
1
DataTable dt = new DataTable();
DataRow DR = dt.NewRow();

if (String.IsNullOrEmpty(TextBox1.Text))
    DR["CustomerID"] = DBNull.Value;
else
    DR["CustomerID"] = Convert.ToInt32(TextBox1.Text);
Rosaleerosaleen answered 15/6, 2011 at 12:54 Comment(0)
G
1

If you declare the Integer variable as int? it is automatically boxed by the C# compiler and you are able to assign null to that variable. For example:

int? custID = null;

I hope that helps

Golf answered 15/6, 2011 at 12:54 Comment(0)
M
0
 if (TextBox1.Text.Trim() == String.Empty)
    {
        DR["CustomerID"] = null;
    }
    else
    {
        DR["CustomerID"] = Convert.ToInt32(TextBox1.Text);
    }
Mercurate answered 15/6, 2011 at 12:53 Comment(1)
null is not accepted, you must use DBNull.valueBadly
G
0

When null is not allowed to be inserted into DR["CustomerID"], you could use (int?)null instead like this:

DR["CustomerID"] = string.IsNullOrEmpty(TextBox1.Text) ?
(int?) null : Convert.ToInt32(TextBox1.Text);
Glottology answered 23/10, 2017 at 15:53 Comment(1)
null is not an accepted value, you must use DBNull.valueBadly

© 2022 - 2024 — McMap. All rights reserved.