Reputation: 2402
I know this question has been asked many times, but I have the following piece of code that I am trying to use to default null values. Can someone please help me. I tried this code but instead of giving me "NO DATA" for the null values, it doesnt display anything. Not sure where I am going wrong.
More Detail: This code does not replace null values with "NO DATA" string. What is wrong here? What do I need to change in order for it to display "NO DATA"?
protected override void Execute(NativeActivityContext context)
{
DataSet dataset = GetDataSet.Get(context);
foreach (DataTable dt in dataset.Tables)
{
foreach (DataRow row in dataset.Tables[0].Rows)
{
if (row["USER_COMMENT"] is System.DBNull)
{
ConvertNullToEmptyString(dt);
Console.WriteLine("In if");
}
else
{
Console.WriteLine("out if");
}
}
}
TransformResult.Set(context, dataset);
}
private static string ConvertNullToEmptyString(DataTable element)
{
if (element.Rows[0]["USER_COMMENT"] == DBNull.Value || element.Rows[0]["USER_COMMENT"] == null)
{
return "NO DATA";
}
else
{
return element.Rows[0]["USER_COMMENT"].ToString();
}
}
Upvotes: 3
Views: 7720
Reputation: 1502
No need of extra function there. You just have to insert the "No DATA" in the loop like below
foreach (DataRow row in dataset.Tables[0].Rows)
{
if (row["USER_COMMENT"] is System.DBNull)
{
row["USER_COMMENT"] = "NO DATA";
Console.WriteLine("In if");
}
else
{
Console.WriteLine("out if");
}
}
Upvotes: 2
Reputation: 843
If you don't need a second function to do it, try something like this:
protected override void Execute(NativeActivityContext context) {
DataSet dataset = GetDataSet.Get(context);
foreach(DataTable dt in dataset.Tables) {
foreach(DataRow row in dt.Rows) {
row["USER_COMMENT"] = String.IsNullOrEmpty(row["USER_COMMENT"].ToString()) ? "NO DATA" : row["USER_COMMENT"];
}
}
TransformResult.Set(context, dataset);
}
But, with a second function to convert, it would look something like this:
protected override void Execute(NativeActivityContext context) {
DataSet dataset = GetDataSet.Get(context);
foreach(DataTable dt in dataset.Tables) {
foreach(DataRow row in dt.Rows) {
row["USER_COMMENT"] = ConvertNullToEmptyString(row["USER_COMMENT"]);
}
}
TransformResult.Set(context, dataset);
}
private static object ConvertNullToEmptyString(object element) {
if(String.IsNullOrEmpty(element.ToString())) {
return "NO DATA";
} else {
return element;
}
}
Upvotes: 1
Reputation: 6778
A couple things that might help:
You probably want to change this:
foreach (DataTable dt in dataset.Tables)
{
foreach (DataRow row in dataset.Tables[0].Rows)
{
...
to this:
foreach (DataTable dt in dataset.Tables)
{
foreach (DataRow row in dt.Rows)
{
...
Or else you will only be querying 1 table in your loop.
Also, I'd use String.IsNullOrEmpty() to interrogate the data.
Upvotes: 2
Reputation: 2082
Did you try
private static string ConvertNullToEmptyString(DataTable element) { if (string.IsNullOrEmpty(element.Rows[0]["USER_COMMENT"])) { return "NO DATA"; } else { return element.Rows[0]["USER_COMMENT"].ToString(); } }
Upvotes: 0
Reputation: 5804
Use empty string instead of null
if(row["USER_COMMENT"] == string.Empty)
Upvotes: 0
Reputation: 1231
Here is what I would do
String stringtocompare;
if(String.isnullorwhitespace(stringtocompare)){
stringtocompare = "No VALUE";
}
Upvotes: 3