Click here to Skip to main content
15,909,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I select Date in Datagrid view Column for update into Database Here is shown an Error: The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.
------------------------------------------------------
Here is my Code :
C#
private void DataGridDataInsert()
        {
            DataGridViewRow dgr;
           
            int  i;
            string Pname,amount,inerID ;
            DateTime insertdt;
            //double amount;
           
            for (i = 0; i < (dtProductTray.Rows.Count - 1); i++)
            {
                dgr = dtProductTray.Rows[i];

//Shown Error : in below line:
                insertdt= Convert.ToDateTime(dgr.Cells[2].Value);//The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.
                amount = dgr.Cells[3].Value.ToString();
                inerID =dgr.Cells[0].Value.ToString();
                
                DB_Manager.UpdateData("update datatable set insert_date='" +Convert.ToDateTime( insertdt.ToString("MM/dd/yyyy")) + "',amount=" + amount.ToString() + " where iner_id=" + inerID + " ");
                           }
        }

Plz help
Posted
Updated 5-Oct-13 7:24am
v3
Comments
Azee 5-Oct-13 13:26pm    
What value does this part return,
dgr.Cells[2].Value?
Shah Noman 5-Oct-13 13:45pm    
dgr.Cells[2].Value.toString();
or, dgr.Cells[2].Value.toString("MM/dd/yyyy")

but not work and shown above error, plz help
Azee 5-Oct-13 14:08pm    
I mean what is the exact datetime string that dgr.Cells[2].Value returning?
Shah Noman 5-Oct-13 22:23pm    
when I select 01/12/2012 in datagridview but it returns 01/01/0001 12:00:00 AM and shown above error, plz help
Shah Noman 5-Oct-13 23:32pm    
Ok, it was cell index problem, it would be Cells[1] itstead of Cells[2]. so it is solved

1 solution

Debug and see what is the value of dgr.Cells[2].Value.
Quote:
Ok, it was cell index problem, it would be Cells[1] itstead of Cells[2]. so it is solved
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900