2

Anyone can guide me a way to check and replace null values in strList with String message "NO DATA" or allow them in dtList. Because when I met a null value in this List,it show an error like "There is no row at position..."

private DataTable getSomething1(String text){
    DataTable dtb = new DataTable();
    ...
    ...
    return dtb;
}

...

protected void buttonCheck_Click(object sender, ImageClickEventArgs e){
    List<DataTable> strList = new List<DataTable>(){
        getSomething(txtInput.Text),
        getSomething2(txtInput.Text),
        getSomething3(txtInput.Text),
        ...                
    };
}

I used C# in Webform. Plz, help me to fix this code or guide me a way to solve it. Thanks in advance.

@tech, @ riffnl: Finally I get all of your ideas and it really work now. Anw, thanks all pro here for advices. So, this is my solution ^^

private String ConvertNullToEmptyString(DataTable element)
    {
        if (element.Rows.Count > 0) //just add this condition here
        {
            if (!DBNull.Value.Equals(element.Rows[0]["FullName"]))
                return (string)element.Rows[0]["FullName"] + " ";
            else
                return String.Empty;
        }
        return "NO DATA";
    }

protected void ....(){
     List<DataTable> strList = new List<DataTable>(){
       GetItem1(txtName.Text),   //private DataTable GetItem1(String t)
       GetItem2(txtName.Text),   //...           
     };

    txtGrD_D.Text = ConvertNullToEmptyString(strList[0]);
    txtGrM_D.Text = ConvertNullToEmptyString(strList[1]);
}
1
  • If row 0 is null, then you can't parse it into string, so either check for null there or check for the row count Commented May 16, 2012 at 6:54

4 Answers 4

8

You can use C#'s null coalescing operator ??:

string s = null;
s = s ?? "NO DATA";
// s now equals "NO DATA";
Sign up to request clarification or add additional context in comments.

Comments

1

Since you're using a datatable I assume you're not talking about NULL (or nill), but DBNull.. Change your function ConvertNullToEmptyString like this:

private String ConvertNullToEmptyString(DataTable element){ 
  if (element.Rows[0]["Fullname"] == DBNull.Value || element.Rows[0]["Fullname"] == null) {
    return "NO DATA";
  } else {
    return element.Rows[0]["Fullname"].ToString();
  }
}

as requested: new sample;

// a list of datatables each containing 1 row, wasn't that the point of datatables
    // anyway -- I think you don't have any rows, so let's try this:
    private String ConvertNullToEmptyString(DataTable element)
    {
        if (element.Rows.Count == 0)
        {
            return "NO DATA";
        }
        if (element.Rows[0]["Fullname"] == DBNull.Value || element.Rows[0]["Fullname"] == null)
        {
            return "NO DATA";
        }
        else
        {
            return element.Rows[0]["Fullname"].ToString();
        }
    } 
    protected void Test()
    {
        List<DataTable> strList = new List<DataTable>(){ 
           GetItem("test1"),   //private DataTable GetItem1(String t) 
           GetItem("test2")   //...            
         };
        txtGrD_D.Text = ConvertNullToEmptyString(strList[0]);
    }

5 Comments

if that's is the correct answer mark the checkmark green in front of the answer to mark it as correct - it'll improve your acceptrating.
i understand your solution. its sound logically. i change a little bit to suite my situation. but i dont know why it still be... "There is no row at position 0.". hix
can you point out my error. I've already edited my post on above like your idea
Yah. I get your ideas and do it in my way. Your idea help me find the solution in this way much. Thanks pro
I am not sure but Instead of: if (element.Rows.Count == 0) try using: if (element.Rows.Any())
0

Try it this way by using String.IsNullOrEmpty. I double a cell in dataTable can be null.

        //creating an example table with one row (no data inisde):
        DataTable table = new DataTable();
        table.Columns.Add("FullName", typeof(string));
        table.Rows.Add("");
        string str = ConvertNullToEmptyString(table);

    //method to check for string:
    private String ConvertNullToEmptyString(DataTable element)
    {
        String s = element.Rows[0]["FullName"].ToString();
        return (string.IsNullOrEmpty(s) ? "NO DATA" : s);
    }

1 Comment

indeed it can't be null it can be DBNull though
0

If the error is "There is no row at position...", then I guess the probelm is not that a string is null, but it is, instead, that you are trying to access a row that does not exist in the datatable.

In particular, in the line :

String s = element.Rows[0]["FullName"].ToString();

You try to access the first row of the datatable. What if the datatable is empty ? You get an error! So you should check if the datatable contains rows with something like this:

if (element.Rows.Count >0)

Answer to you comment: yes we can. modify your:

foreach (DataTable element in strList)

into something like:

foreach (DataTable element in strList)     
    {          
    if (element.Rows.Count>0)
        {
        if(strList.Any(item => item == null))          
            {                
          ConvertNullToEmptyStringelement);                                                 
            }
        txtItem1.Text = mytext ;          
        .....
        }
    else
        {
        txtItem1.Text = "No Data";          
        }      
    } 

This is as far as I can go without modifying too much your program structure.

5 Comments

yup. this row does not exist in the datatable. it's empty so i wanna allow them to be a string message = "NO DATA" and i dont know how to do it. can we do it, pro ?
yes: look into the answer, I've edited it with a possible solution
can you point out my error. I've already edited my post on above like you said
probably the error is in the line: txtGrM_D.Text = strList[1].Rows[0]["FullName"].ToString(); Here you are referencing a datatable (strList[1]) where you have not checked if there is at least one row.
uk. i got it. i've already find a way to checked it before replace it. thanks pro

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.