ullas K

ullas K

  • NA
  • 55
  • 0

Datetime from datagridview insertion error

Aug 9 2012 8:33 AM
I have a datagridview with columns,Book no,Book Name,Issue Date,Return Date,remarks.

i have set the issuedate and returndate format as "d"(ie,dd/MM/yyyy) and issue date cell value as datetime.now.

when i am inserting the values it is giving a conversion error.My sqlserver issuedate and returndate columns are of datetime datatypes

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
The statement has been terminated.

when i try to insert issuedate as follows it is working

issuedt= DateTime.Now.ToString("MM/dd/yyyy");

but when i am trying to fetch the value from datagridview cell for insert it is giving the above error

my code is ;


{
            DataGridViewRow dgr;
            LibraryLib.Library obj = new LibraryLib.Library();

            int  i;
            string bookno, remarks ;
            DateTime issuedt, returndt;
            for (i = 0; i < (dataGridView1.Rows.Count - 1); i++)
            {
                dgr = dataGridView1.Rows[i];
                bookno = dgr.Cells[1].Value.ToString();
                issuedt = DateTime.Parse(dgr.Cells[3].Value.ToString());
                //issuedt= DateTime.Now.ToString("MM/dd/yyyy"); this is working


                returndt = DateTime.Parse(dgr.Cells[4].Value.ToString());
                remarks = dgr.Cells[5].Value.ToString();


                    obj.idu("insert into bookissue values(" + t1.Text.ToString() + ",'" + bookno + "','" + issuedt + "','" + returndt + "','" + remarks + "')");

                           
            }

pls help me get out of this situation as i am new to c#.net
thanks in advance

Answers (2)