All the answers here are ignoring the problem. data
is null
here. Debug your program and make sure that Model.Products[i].Description
is not null.
Your current method to get the first 60 characters is a bit obtuse, but correct. The problem is you're retrieving invalid data before you even start trying to get the 60 characters.
If a null
value is considered valid for you (that is, you expect it to be null
sometimes) then you need to do a null check first then decide what you want to do. Do you want temp
to be null
or an empty string?
Here is sample code performing the check, not ideal code but designed to demonstrate the problem:
string data = Model.Products[i].Description;
string temp;
if (data == null)
{
temp = //? what do you want "temp" to be if data is null?
}
else
{
temp = string.Join(string.Empty,data.Take(60));
}