0

I have a table where your data is changed every week, and in some cells it is null, occasionally it is bringing me an error because apparently it can only read values ​​that are not null, I would like to know how I can read it anyway, even if it doesn't bring me any data.

the null cells is E1 for example

i want to read the E2 null cell for example.

what i have did so far in my project

 {
        UserCredential credential;

        using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
        {
            string creadPath = "token.json";
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(creadPath, true)).Result;
            label1.Text = "Conected with sheet";


        }
        var service = new SheetsService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName
        });
        {
            var LimiteOP = $"{sheet}!B3:E3";
            SpreadsheetsResource.ValuesResource.GetRequest request = service.Spreadsheets.Values.Get(SpreadSheetId, LimiteOP);
            var response = request.Execute();

            IList<IList<Object>> values = response.Values;

            if (values.Count > 0)
            {
                foreach (var row in values)
                {

                    Console.WriteLine(row[0]+" "+ row[1]+" " + row[2]);
                }
             }
              else
             {
                Console.WriteLine("Nothing");
             }

Update (from comments): "the consolewrite give the exception"

EDIT

to solve the problem that was the way as a simple process, transform the object to a string and then use a Split to remove the null spaces

  String[] str = row[0].ToString().Split(null);
  Console.WriteLine(str[0]);
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Felipe Cruz
  • 140
  • 1
  • 11
  • What line throws the exception? Can you post the entire exception stack? I'm guessing either the `response.Values` or `values.Count` – Stack Undefined May 02 '20 at 23:00
  • the consolewrite give the exception – Felipe Cruz May 02 '20 at 23:12
  • Then the error is in your display code, not in how to retrieve data from Google –  May 02 '20 at 23:34
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  May 02 '20 at 23:34
  • If the exception is thrown in the `Console.WriteLine()`, then you aren't getting back all three `row` elements - not even the `null` or empty string from the googlesheet. – Stack Undefined May 02 '20 at 23:59
  • I already checked the code and made some changes but I still can't read the table line that contains the null values – Felipe Cruz May 03 '20 at 00:02
  • So for `{sheet}!B3:E3` in your example table, how many items does the `row` list have in the `foreach` loop (3 or 4)? – Stack Undefined May 03 '20 at 00:10
  • 1
    assuming that u might be having null reference exception, to sort this you would need to check that thing which could be null, in your case that could be row[i], so first of all you need to learn how to debug your code "https://learn.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019". put your debug point on foreach :p – Syed Muhammad Munis Ali May 03 '20 at 00:11
  • I can read the Column if I put a point in the ones that are empty, like for example in column B, the fields that are empty I put a point but in column B before it, what is causing an error, no matter how much information there is in the column the program can read them. unless they have something inside – Felipe Cruz May 03 '20 at 00:19

1 Answers1

0

This is what I did in my solution:

  row.ElementAtOrDefault(1)?.ToString() ?? ""

The "" at the end is the default value