Home > Back-end >  Setting Null Reference to String "Null"
Setting Null Reference to String "Null"

Time:04-10

I'm setting a textbox equal to the data in a data grid view, allowing the user when clicking a row on the grid to display all the information. However, some of the data is empty and causes a null reference exception. Is it possible to use try-catch and set the null reference equal to a string "null" or return nothing in its place?

private void dataGridViewE_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            employeetxt.Text = dataGridViewE.CurrentRow.Cells[1].Value.ToString();
            lasttxt.Text = dataGridViewE.CurrentRow.Cells[2].Value.ToString();
            firsttxt.Text = dataGridViewE.CurrentRow.Cells[3].Value.ToString();
            titletxt.Text = dataGridViewE.CurrentRow.Cells[4].Value.ToString();
            titleoftxt.Text = dataGridViewE.CurrentRow.Cells[5].Value.ToString();
            birthtxt.Text = dataGridViewE.CurrentRow.Cells[6].Value.ToString();
            hiretxt.Text = dataGridViewE.CurrentRow.Cells[7].Value.ToString();
            addresstxt.Text = dataGridViewE.CurrentRow.Cells[8].Value.ToString();
            citytxt.Text = dataGridViewE.CurrentRow.Cells[9].Value.ToString();
            regiontxt.Text = dataGridViewE.CurrentRow.Cells[10].Value.ToString();
            postaltxt.Text = dataGridViewE.CurrentRow.Cells[11].Value.ToString();
            countrytxt.Text = dataGridViewE.CurrentRow.Cells[12].Value.ToString();
            hometxt.Text = dataGridViewE.CurrentRow.Cells[13].Value.ToString();
            extensiontxt.Text = dataGridViewE.CurrentRow.Cells[14].Value.ToString();
            phototxt.Text = dataGridViewE.CurrentRow.Cells[15].Value.ToString();
            notestxt.Text = dataGridViewE.CurrentRow.Cells[16].Value.ToString();
            reportstxt.Text = dataGridViewE.CurrentRow.Cells[17].Value.ToString();
            photopathtxt.Text = dataGridViewE.CurrentRow.Cells[18].Value.ToString();
        }
        catch (NullReferenceException)
        {

        }
    }

Example when no null reference is present

CodePudding user response:

We use the ?. operator as a null conditional operator in C#.

The dot after the question mark shows the member access. The ?. null-conditional operator applies a member access operation to its operand only if that operand evaluates to non-null; otherwise, it returns null.

Consider the following example:

internal class Program
{
    static void Main(string[] args)
    {
        MyClass noInit = new MyClass();
        noInit = null;

        // Nothing bad happens :)
        Console.WriteLine(noInit?.Name);

        // Throws 'Object reference not set to an instance of an object.'
        Console.WriteLine(noInit.Name);
    }
}

public class MyClass
{
    public string Name { get; set; }
}

Also, you can check out other useful operators here ?? and ??= operators (C# reference)

CodePudding user response:

You can use the ? operator, so something like this:

dataGridViewE.CurrentRow.Cells[16]?.Value?.ToString();

Or you can create a function:

private string GetStringValue(int position) {
   if (dataGridViewE.CurrentRow.Cells[position].Value != null)
       return dataGridViewE.CurrentRow.Cells[position].Value.ToString();
   //Customize your default result
   return string.Empty;
}

employeetxt.Text = GetStringValue(1);
  • Related