Link to home
Start Free TrialLog in
Avatar of Olukayode Oluwole
Olukayode OluwoleFlag for Canada

asked on

Why is a date field being written from a Grid to a database NOT seen as a date field

I am trying to write data from a grid  to a postgresql database .

The datagrid with the data is shown below:

User generated image
I got an error  saying that a field is not recognised as valid date field
But i know the field is declared  as a date field. See the error screen below

User generated image
Note that the string GridColumn03  and gridColumn04    are showing as string and
i tried the Convert  statement

My full  Method is shown below

public void CreateJobHistoryForm(DataTable dataSource)
        {
            // New datatable implementation  24-05-2019
            //Place  to process tuples into the database
            //  Save Button Click
            using (NpgsqlConnection conn = new NpgsqlConnection(pgrstring))
            {
                // public.speducation_insert
                using (var command = new NpgsqlCommand("public.spjobhistory_writemasterdetail", conn))
                //using (var command = new NpgsqlCommand("public.speducation_insert", conn))
                {

                    conn.Open();
                    command.CommandType = CommandType.StoredProcedure;

                    //command.Parameters.Add(value: new NpgsqlParameter("staffnox", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Input, Value = GridVariables.GridColumn01 });
                    //command.Parameters.Add(new NpgsqlParameter("companycodex", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Input, Value = GridVariables.GridColumn07 });
                    //command.Parameters.Add(new NpgsqlParameter("empidx", NpgsqlTypes.NpgsqlDbType.Integer) { Direction = ParameterDirection.Input, Value = ((int)GridVariables.GridColumn08) });
                    //command.Parameters.Add(new NpgsqlParameter("empeduidx", NpgsqlTypes.NpgsqlDbType.Integer) { Direction = ParameterDirection.InputOutput, Value = ((int)GridVariables.GridColumn09) });

                    NpgsqlParameter staff_no = new NpgsqlParameter("staffnox", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Input };
                    command.Parameters.Add(staff_no);
                    NpgsqlParameter companyname = new NpgsqlParameter("companynamex", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Input };
                    command.Parameters.Add(companyname);
                    NpgsqlParameter startdate = new NpgsqlParameter("startdatex", NpgsqlTypes.NpgsqlDbType.Date) { Direction = ParameterDirection.Input };
                    command.Parameters.Add(startdate);
                    NpgsqlParameter enddate = new NpgsqlParameter("enddatex", NpgsqlTypes.NpgsqlDbType.Date) { Direction = ParameterDirection.Input };
                    command.Parameters.Add(enddate);
                    NpgsqlParameter yearsexp = new NpgsqlParameter("yearsexpx", NpgsqlTypes.NpgsqlDbType.Integer) { Direction = ParameterDirection.Input };
                    command.Parameters.Add(yearsexp);
                    NpgsqlParameter designationid = new NpgsqlParameter("designationidx", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Input };
                    command.Parameters.Add(designationid);
                    NpgsqlParameter companycode = new NpgsqlParameter("companycodex", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Input };
                    command.Parameters.Add(companycode);
                    NpgsqlParameter empid = new NpgsqlParameter("empidx", NpgsqlTypes.NpgsqlDbType.Integer) { Direction = ParameterDirection.Input };
                    command.Parameters.Add(empid);
                    NpgsqlParameter empjobid = new NpgsqlParameter("empjobidx", NpgsqlTypes.NpgsqlDbType.Integer) { Direction = ParameterDirection.Input };
                    command.Parameters.Add(empjobid);

                    staff_no.Value = GridVariables.GridColumn01;
                    companycode.Value = GridVariables.GridColumn07;
                    empid.Value = GridVariables.GridColumn08;
                    empjobid.Value = GridVariables.GridColumn09;

                    string staffnop = "";
                    string companynamep = "";
                    string startdatep = "";
                    //  Use paratmeters above to test that record is not duplicated twice in the same recordset ( check duplicates )

                    foreach (DataRow row in dataSource.Rows)
                    {
                        if ((staffnop != GridVariables.GridColumn01) && (companynamep != GridVariables.GridColumn02) && (startdatep != GridVariables.GridColumn03))
                        {
                            companyname.Value = GridVariables.GridColumn02;
                            startdate.Value = Convert.ToDateTime(GridVariables.GridColumn03);
                            enddate.Value = Convert.ToDateTime(GridVariables.GridColumn04);
                            yearsexp.Value = Convert.ToInt32(GridVariables.GridColumn05);
                            designationid.Value = GridVariables.GridColumn06;

                            int newvar = 0;
                            //  int newid = Convert.ToInt32(command.ExecuteNonQuery());
                            int newid = Convert.ToInt32(command.ExecuteScalar());
                            newvar = newid;
                            staffnop = GridVariables.GridColumn01;
                            companynamep = GridVariables.GridColumn02;
                            startdatep = GridVariables.GridColumn03;
                        }
                    }
                    return;
                }
            }
        }

Open in new window


How can i resolve this error

Thanks

Olukay
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Olukayode Oluwole

ASKER

Thanks Gustav
You are welcome!