-2

I want to enter guest details(Title,Firstname,midname lastname) in a list<string> ,the guest details can be empty.I'm using LINQ for inserting in the list.I had referred this question for the LINQ code DataGridView write all values from a column to list

All I want to do is enter the text into the list if it has or insert empty string if it doesn't have.Right now if I leave textboxes blank it will throw object reference not set to instance of an object exception

private string SaveGuestDetails()
        {
            string strRet = string.Empty;
            Reservation obj = new Reservation();
            FinalReservationDetails f = new FinalReservationDetails();

            try
            {
                //int i = dgvTextBoxes.Rows.Count;
                List<DataRow> personsList = new List<DataRow>();
                int j = 0;
                for (int i = 0; i < dgvTextBoxes.Rows.Count; i++)
                {
                    f.SubSrNo = j + 1;

                      f.GuestTitle = dgvTextBoxes.Rows
                   .OfType<DataGridViewRow>()
                   .Select(r => r.Cells["txtTitle"].Value.ToString())
                   .ToList();

                f.FirstName = dgvTextBoxes.Rows
                    .OfType<DataGridViewRow>()
                    .Select(r => r.Cells["txtFname"].Value.ToString())
                    .ToList();

                f.MidName = dgvTextBoxes.Rows
                    .OfType<DataGridViewRow>()
                    .Select(r => r.Cells["txtMName"].Value.ToString())
                    .ToList();

                f.LastName = dgvTextBoxes.Rows
                    .OfType<DataGridViewRow>()
                    .Select(r => r.Cells["txtLname"].Value.ToString())
                    .ToList();

                 }


            }
            catch (Exception ex)
            {

            }
            return strRet;
        }
Community
  • 1
  • 1
saurabh64
  • 363
  • 1
  • 4
  • 24
  • @PatrikEckebrecht No man .I want to insert empty cell if its present – saurabh64 Jun 18 '15 at 08:57
  • I stick with [What is a NullReferenceException](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). You have it somewhere in your code and you ask others to find it. If you have a specific question, ask more specific. For example, post your stacktrace and tell, which object is `null`. Then think about why it is so and when you have no clue, ask again. – Patrik Jun 18 '15 at 09:02
  • yep find out what is null. And you can remove the `for loop` your LINQ selects the whole column and stores the list in your object `f` so each iteration is doing the same. – Koryu Jun 18 '15 at 09:11
  • @PatrikEckebrecht my question is not about null reference ,but want to insert empty textboxes in the list – saurabh64 Jun 18 '15 at 09:12
  • 1
    you have to handle cases of Cell.Value is null or DbNull. Best way is to use the **null-coalescing operator**. `f.GuestTitle = dgvTextBoxes.Rows.OfType().Select(r => (r.Cells["txtTitle"].Value as string) ?? string.Empty ).ToList();` – Koryu Jun 18 '15 at 09:30
  • @Koryu that solved my problem thanks but my question didn't need to be downvoted – saurabh64 Jun 18 '15 at 09:44
  • havent downvoted it :) see my answer for detailed information how to convert to string in this case. – Koryu Jun 18 '15 at 09:51
  • @Koryu the comment was itself helpful – saurabh64 Jun 18 '15 at 09:53

1 Answers1

1

Your problem is that your cell values can be string, DBNull.Value or null. null.ToString() throws the object reference not set to instance of an object exception. To avoid that you can first set your object as string to get rid of the DBNull.Value and then use the null-coalescing operator to set null to empty string.

object unknown = System.DBNull.Value;
string converted1 = (unknown as string) ?? string.Empty;
// returns string.Empty

unknown = null;
string converted2 = (unknown as string) ?? string.Empty;
// returns string.Empty

unknown = "text";
string converted3 = (unknown as string) ?? string.Empty;
// returns text

In your case:

f.GuestTitle = dgvTextBoxes.Rows.OfType<DataGridViewRow>().Select
(r => (r.Cells["txtTitle"].Value as string) ?? string.Empty ).ToList();
Koryu
  • 1,371
  • 1
  • 11
  • 21