Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
DataTable dt = new DataTable();

                        dt.Columns.Add("L_DateTime");
                        dt.Columns.Add("L_Direction");
                        dt.Columns.Add("L_CardID");
                        dt.Columns.Add("L_GateID");

                        int i2 = 0;
                        while ((inputLine = rdr.ReadLine()) != null) //Read while                          
                                                 //the line is not null
                        {
                            string[] arr;
                            arr = inputLine.Split(','); //splitting the line which
   
                           //was read by the stream reader object

                            row = dt.NewRow();
                            row["L_DateTime"] = Convert.ToDateTime(arr[0]);
                            row["L_Direction"] = arr[1];
                            row["L_CardID"] = arr[2];
                            row["L_GateID"] = Convert.ToInt32(arr[3]);

                            dt.Rows.Add(row);
                            i2++;
                        }

                        sqlbulkCopy.WriteToServer(dt);
                        sqlbulkCopy.Close();



Greeting everyone...i have problem here...
let me explain a little bit what this code is all about...

im using sqlbulk to read data from text file, and store it in database. As far as im aware in text file, the data all treated as string(perhaps)!..

earlier in my database i have created rows which name are exactly as in this code..

first in my sql dbase, im treated all data as string..and it is working fine..
but since it involving datetime...i change it to suitable type...
and i strting to get error...said :STRING WAS NOT RECOGNIZED AS A VALID DATETIME!!!
i dont understand...i have already converted it to datetime...any suggestion would be nice....regards to all coders!!
Posted
Updated 10-Dec-11 3:43am
v2
Comments
Abhinav S 10-Dec-11 9:43am    
Pre tags added.

I believe the format of L_DateTime you are retrieving is not valid for DateTime. Use DateTime.TryParse method instead of directly converting the values or, save it as string format by using .ToString().

Regards,
Eduard
 
Share this answer
 
Comments
beh7606 10-Dec-11 11:34am    
thanks Eduard...u save me!!
[no name] 11-Dec-11 6:48am    
u are welcome! feel free to ask :)
Abhinav S 10-Dec-11 12:07pm    
My 5. I recommended TryParse as well.
[no name] 11-Dec-11 6:48am    
thanks!
Instead of Convert.ToDateTime use DateTime.TryParse.
In this case, if your input is not a valid date time, you will not get this error.

To know more about the DateTime.TryParse method, go through here[^].
 
Share this answer
 
Comments
beh7606 10-Dec-11 10:08am    
thank you for your post Abhinav, do i need to do a declare a method or it is already built in??
Abhinav S 10-Dec-11 10:15am    
You are welcome.DateTime.TryParse is a built in method - just follow the link I have provided to learn more about it.
Mark this question as answered if this answer helped you.
beh7606 10-Dec-11 10:19am    
can u pls share with me logically where should i place DaTime.TryParse in my code?? huhu...
Abhinav S 10-Dec-11 10:30am    
Instead of row["L_DateTime"] = Convert.ToDateTime(arr[0]);
Try something like
DateTIme.TryParse(arr[0],Culture.currentculture, dt);
if (dt != null) row["L_DateTime"] = dt;
beh7606 10-Dec-11 11:35am    
thanks Abhinav...its done..!!

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