What is the correct syntax to read a date field from a grid

Olukayode Oluwole
Olukayode Oluwole used Ask the Experts™
on
I have a c# application reading 2 date fields from a grid

with the script below

[public DataTable GetResultsTable()                  //  This handles Insertion of Grid items  into database
        {
            DataTable d =  (DataTable)(dgvDetailsTable.DataSource);

                foreach (DataGridViewRow row in dgvDetailsTable.Rows)
                {
                    //if (Convert.ToString(row.Cells["dgvtxtCompName"].Value).Length > 0)
                    //{
                    (string gc2, DateTime gc3, DateTime gc4, string gc5, string gc6) = (
                              GridVariables.GridColumn02 = gc2 = Convert.ToString(row.Cells["dgvtxtCompName"].Value),
                              GridVariables.GridColumn03 = gc3 = Convert.ToDateTime(row.Cells["dgvstartdate"].Value),
                              GridVariables.GridColumn04 = gc4 = Convert.ToDateTime(row.Cells["dgvenddate"].Value),
                              GridVariables.GridColumn05 = gc5 = Convert.ToString(row.Cells["dgvtxtyearsexp"].Value),
                              GridVariables.GridColumn06 = gc6 = Convert.ToString(row.Cells["dgvtxtPosition"].Value)
                               );
     
                    GlobalConfig.Connection.CreateJobHistoryForm((DataTable)dgvDetailsTable.DataSource);

                }
                MessageBox.Show("Records saved to Database", "NEW RECORDS CREATION !!!", MessageBoxButtons.OK);
                WireupList2();
                return d;
       
        }[/code]

The 2 date fields  startdate  and enddate contain    1-11-2019  and 30-11-2019

I have used the debugger to confirm that the fields actually contain these  values

However  i think the script expects a different syntax for the 2 fields being read.

The error screen is shown below

Error screen while reading a date field from a grid
What would be the correct syntax to ensure that the date field as read is acceptable

Any help will be appreciated

Olukay
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Eduard GherguArchitect - Coder - Mentor

Commented:
Hi,

Could you dump the string values from the cells using Debug.Write and see if they're ok?
Olukayode OluwoleSystems Analyst

Author

Commented:
I am sorry i don't understand what you want me to do.

I have debugged and can see that the date values are correct

What does it mean to dump the values and then write . How  do i do that

Grateful for an example

Thanks

Olukay
Eduard GherguArchitect - Coder - Mentor

Commented:
HI,

Add the following lines in the beginning of the foreach loop body:
 
Debug.WriteLine(row.Cells["dgvstartdate"].Value);
Debug.WriteLine(row.Cells["dgvenddate"].Value);

Execute the code and paste here what is shown in the VS Debug pane.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Olukayode OluwoleSystems Analyst

Author

Commented:
Find what was pasted  after the first debug line
After the first debug line
then after the second debug line
After the second debug line
and finally error  going into the original script
Next Click after the Debug Lines
I hope the above helpto resolve the issue

Olukay
Architect - Coder - Mentor
Commented:
Ok, thank you.

Try to use DateTime.ParseExact method, where you have the posibility to specify the format of the string:
https://docs.microsoft.com/en-us/dotnet/api/system.datetime.parseexact?view=netframework-4.8

The format options:
https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings?redirectedfrom=MSDN
Olukayode OluwoleSystems Analyst

Author

Commented:
Thanks
Eduard GherguArchitect - Coder - Mentor

Commented:
Hi!
My pleasure! If you need more help, please, let me know!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial