0

I am using gridview's default update method in which it allows me to update row in gridview itself by converting cells into textboxes.

I want to check validations that if a particular textbox (cell) remains empty or blank then it should not update its value.

For that i have written following code:

string.IsNullOrEmpty(e.NewValues[0].ToString())

But it gives an error like object reference not set to an instance of an object. May be it can not convert null value of e.Newvalues[0] to string.

All answers are appreciated in advance.

2
  • 1
    If e.NewValues[0] is null, then yes, you are correct - calling ToString() on it will fail. What data type is e.NewValues[0]? Commented Aug 5, 2013 at 7:53
  • if(e.NewValues[0].Equals(null)) Commented Aug 5, 2013 at 7:55

6 Answers 6

3

You could do this:

e.NewValues[0] == null || e.NewValues[0].ToString() == string.Empty

If e.NewValues[0] is already a string, you could just do this:

string.IsNullOrEmpty(e.NewValues[0])

Update as of C# 6, you could also use:

string.IsNullOrEmpty(e.NewValues[0]?.ToString())

Or even:

$"{e.NewValues[0]}" == string.Empty
Sign up to request clarification or add additional context in comments.

Comments

2

Another way:

String.IsNullOrEmpty(Convert.ToString(e.NewValues[0]));

A bit of (probably unneeded) explanation:

Convert.ToString() will return null for a (string)null, and an empty string for an (object)null (or any other null).

Either case will give the expected result, because we're checking with String.IsNullOrEmpty().

In any case, its behaviour is the same as someValue.ToString() except it handles the cases where someValue is null.

3 Comments

Did you try that? It will return an empty string.
@LasseV.Karlsen So? That isn't a problem, since string.IsNullOrEmpty will treat null and "" the same way. This is a correct answer.
Ugh, better get more coffee, sorry, my mistake.
1

Another (wasteful) way to do it is with a singleton with an overridden ToString and ?? (overkill but it lets me use ?? :P)

(e.NewValues[0] ?? Empty._).ToString();

The code for the singleton is here:

public sealed class Empty
{
    private static readonly Lazy<Empty> lazy =
        new Lazy<Empty>(() => new Empty());
    public override string ToString()
    {
        return "";
    }
    public static object _ { get { return lazy.Value; } }
    private Empty()
    {
    }
}

Comments

1

You can use this piece of code

 (e.NewValues[0] == null) ? string.Empty : e.NewValues[0].ToString()

The above code will will return the string equivalent if not null, otherwise it will return empty string.

Otherwise you can use following code. This will handle the null case.

string.IsNullOrEmpty(Convert.ToString( e.NewValues[0] )

1 Comment

Your first line of code seems to return either a string or a bool so I don't think will compile (or do what is wanted). Did you mean to have the IsNullOrEmpty call wrapping the whole thing rather than just in the last expression?
0

You'll need to check that e.NewValues[0] isn't null prior to doing a .ToString() on it.

Comments

0
protected void grd_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
     GridViewRow row = grd.Rows[e.RowIndex];
     for (int i = 0; i <= row.Cells.Count; i++)
     {
         String str = ((TextBox)(row.Cells[i].Controls[0])).Text;
         if (!string.IsNullOrEmpty(str))
         { 
          //Your Code goes here ::
         }
     }
}

Comments

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.