.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> Windows Application >> Post New QuestionBookmark and Share Subscribe to Forum

Datagridview datetime value insertion error

Posted By: Ullas     Posted Date: August 09, 2012    Points:5   Category :Windows Application
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

Responses
Author: Narayanan             
Posted Date: August 10, 2012     Points: 20   

Hi,
try this code...
DateTime dte = DateTime.Parse(dateTimePicker1.Value.ToString());
DateTime dateOnly = dte.Date;
string format = "dd/MM/yyyy";
MessageBox.Show(dateOnly.ToString(format));


Regards, Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend