Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a code to import excel data to SQL. There first I m importing excel data into a data table and that data table i am saving into SQL database.I am having one field data type data time. while importing I am taking all values as a string.I am Saving them with the help of one stored procedure. Saving time I am facing error at date time field.


here is the code.

foreach (DataRow xrow in dt.Rows)
               {


                   string siteid = xrow["siteid"].ToString().Trim();
                   string site = xrow["site"].ToString().Trim();



                   DateTime asd = DateTime.Now;

                   asd = Convert.ToDateTime(xrow["asdate"]);//here I got Error



                   string address = xrow["address"].ToString().Trim();
                   string city = xrow["city"].ToString().Trim();
                   string district = xrow["district"].ToString().Trim();
                   string zone = xrow["zone"].ToString().Trim();
                   string rpname = xrow["rpname"].ToString().Trim();
                   string phno = xrow["phno"].ToString().Trim();
                   string lessor = xrow["lessor"].ToString().Trim();
                   string phone = xrow["phone"].ToString().Trim();

                   string fse = xrow["fse"].ToString().Trim();
                   string contact = xrow["contact"].ToString().Trim();
                   string remark1 = xrow["remark1"].ToString().Trim();
                   string remark2 = xrow["remark2"].ToString().Trim();
                   string prodate = xrow["prodate"].ToString().Trim();
                   string restatus = xrow["restatus"].ToString().Trim();
                   string doctype = xrow["doctype"].ToString().Trim();


                   SqlCommand addrow = new SqlCommand("ProductBulkInsertion", con);
                   addrow.CommandType = CommandType.StoredProcedure;
                   //addrow.Parameters.AddWithValue("@company", company);
                   addrow.Parameters.AddWithValue("@siteid", siteid);
                   addrow.Parameters.AddWithValue("@site", site);
                   addrow.Parameters.AddWithValue("@asdate", asd);
                   addrow.Parameters.AddWithValue("@address", address);
                   addrow.Parameters.AddWithValue("@city", city);
                   addrow.Parameters.AddWithValue("@district", district);
                   addrow.Parameters.AddWithValue("@zone", zone);
                   addrow.Parameters.AddWithValue("@rpname", rpname);
                   addrow.Parameters.AddWithValue("@phno", phno);
                   addrow.Parameters.AddWithValue("@lessor", lessor);
                   addrow.Parameters.AddWithValue("@phone", phone);
                   addrow.Parameters.AddWithValue("@fse", fse);
                   addrow.Parameters.AddWithValue("@remark1", remark1);
                   addrow.Parameters.AddWithValue("@remark2", remark2);
                   addrow.Parameters.AddWithValue("@prodate", prodate);
                   addrow.Parameters.AddWithValue("@restatus", restatus);
                   addrow.Parameters.AddWithValue("@doctype", doctype);
                   addrow.ExecuteNonQuery();


error is
string was not recognized as a valid datetime


What I have tried:

I tried tryparse, tryparse extract and all but failed.I searched net but no suitable answer. So I posted Here.
Posted
Updated 1-Dec-18 23:13pm
v6
Comments
Richard MacCutchan 2-Dec-18 3:07am    
What part of that message do you not understand? Clearly the data you are trying to import is not recognised as a valid date and time. And since you have not shown us the actual string it is difficult to make any suggestions.
vijay_bale 2-Dec-18 3:32am    
Thanks for your interest to solve and to help.

1 solution

Ok. My Problem Solved. I changed cell data type to date time in excel itself and I changed date format of all those date cells into single format into Indian format dd/MM/yyyy.
 
Share this answer
 
v2

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