Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In .txt i have data in lines. Each textbox,... gets the data from a one line. Code for opening data is like this:

C#
 ...
f1.textBox41.Text = lista.List[63].ToString();
f1.textBox42.Text = lista.List[64].ToString();
f1.textBox43.Text = lista.List[65].ToString();
f1.textBox59.Text = lista.List[66].ToString();
f1.textBox47.Text = lista.List[67].ToString();
f1.textBox46.Text = lista.List[68].ToString();
f1.textBox49.Text = lista.List[69].ToString();
... // a lot more data...

As you can see data is put into list. I need to figure it out how to check each line and if there is any line in list that there is no data (is empty or "" or,...) then insert space or something (anything). Because now it shows me the error where is no string.
For example: go throw code, if in any line in list is no data then insert something in textbox - is this even possible? Or do i need to write code for every line to check if there is a value?

p.s. Saving works fine, for each textbox app saves one line in .txt. But user can forget,... to fill them all. And in .txt i do get all the lines that i need, they are just empty.
Posted
Comments
[no name] 26-May-13 11:32am    
First thing is that you are doing something terribly, horribly wrong if you think that you need 49+ textboxes on a form to display anything.
Yes you can check if your list contains no data at a particular element, but it is easier to just not insert blank data into your list to begin with.

1 solution

So are you saying you are getting a null ref (or whatever it is) exception on your ToString? is that it?

If so you could do a few things

1) Check if null (Every line)...also i am assuming your List is some other data type then string (cause why ToString a string although you are reading from a file...)
C#
f1.textBox49.Text = lista.List[69] == null ? "" : lista.List[69].ToString();


-Issue with this is that you have to apply this to every line

2) Create a method/Extension method

C#
public static class ExtensionMethod
{
     public static string SafeString(this object value)
     {
          return value == null ? "" : value.ToString();

     }
}


You would have to make sure you include a using to reference the class correctly if it is in a different namespace btw.

Then your usage would be

C#
f1.textBox49.Text = lista.List[69].SafeString();



Based on my understanding of your question i think thats what you are looking for.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900