Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey guys, I have this function:

public DateTime dates(string date, string hour, string min) {
            string dateString = date + " " + hour + ":" + min;
            string[] format = new string[] { "d/M/yyyy h:mm", "d/M/yyyy h:mm:ss", "d-M-yyyy h:mm", "dd-MM-yyyy hh:mm" };
            string finalFormat = "d/M/yyyy h:mm";

            //Set datestring and the format
            DateTime dateTime;
            DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);
            dateTime.ToString().Trim().Replace('-', '/');

            return dateTime;
        }


Which i call here:

protected void submit_Click(object sender, EventArgs e)
        { 
            DateTime eventStart = dates(eventStartDate.Text, eventStartHour.Text, eventStartMinute.Text);
            DateTime departure = dates(departureDate.Text, departureHour.Text, departureMinute.Text);
            DateTime arrival = dates(arrivalDate.Text, arrivalHour.Text, arrivalMinute.Text);
            Console.WriteLine(arrival);
            DateTime flightDeparture = dates(flightDepartureStartDate.Text, flightDepartureStartHour.Text, flightDepartureStartMinute.Text);
            DateTime flightReturn = dates(flightReturnStartDate.Text, flightReturnStartHour.Text, flightReturnStartMinute.Text);

            byte[] file = (byte[])Session["File"];
            string filename = System.IO.Path.GetFileName(AsyncFileUpload1.FileName);
            string ConStr = ConfigurationManager.ConnectionStrings["FORgestIT"].ToString();

            SqlConnection SQLConn = new SqlConnection(ConStr); //The SQL Connection              

            SqlCommand SQLCmd = new SqlCommand();
            SQLCmd.Connection = SQLConn;
            SQLCmd.CommandType = CommandType.Text;

            //SQLCmd.CommandText = "insert into Event values(@Name,@Project,@Objectives,@City,@Country,@Event_Start,@Departure,@Arrival,@Registration,@National_Transportation,@Accomodation,@AC_NumberNights,@AC_PreferHotel,@AC_PreferHotelURL,@Flight,@FL_Departure,@FL_Depart_Prefer,@FL_Depart_URL,@FL_Ret_Date,@FL_Return,@FL_Ret_Prefer,@FL_Ret_URL,@Notes,@File,@Status)";

            // ACCORDING TO OG's SOLUTION OP CHANGED THE PREVIOUS LINE TO THIS
            SQLCmd.CommandText = "INSERT INTO Event (Name, Project, Objectives, City, Country, Event_Start, Departure, Arrival, Registration, National_Transportation, Accomodation, AC_NumberNights, AC_PreferHotelURL, Flight, FL_departure, FL_Depart_Prefer, FL_Depart_URL, FL_Return, FL_Ret_Prefer, FL_RET_URL, Notes, File, Status) VALUES (@Name,@Project,@Objectives,@City,@Country,@Event_Start,@Departure,@Arrival,@Registration,@National_Transportation,@Accomodation,@AC_NumberNights,@AC_PreferHotel,@AC_PreferHotelURL,@Flight,@FL_Departure,@FL_Depart_Prefer,@FL_Depart_URL,@FL_Return,@FL_Ret_Prefer,@FL_Ret_URL,@Notes,@File,@Status)";

            SQLCmd.Parameters.Clear();
            SQLCmd.Parameters.AddWithValue("@Name", name.Text);
            SQLCmd.Parameters.AddWithValue("@Project", project.Text);
            SQLCmd.Parameters.AddWithValue("@Objectives", objectives.Text);
            SQLCmd.Parameters.AddWithValue("@City", venueCity.Text);
            SQLCmd.Parameters.AddWithValue("@Country", venueCountry.Text);
            SQLCmd.Parameters.AddWithValue("@Event_Start", eventStart);
            SQLCmd.Parameters.AddWithValue("@Departure", departure);
            SQLCmd.Parameters.AddWithValue("@Arrival", arrival);
            SQLCmd.Parameters.AddWithValue("@Registration", registrationInformation.Text);
            SQLCmd.Parameters.AddWithValue("@National_Transportation", rdlYesNo.SelectedValue);
            SQLCmd.Parameters.AddWithValue("@Accomodation", accomodation.SelectedValue);
            SQLCmd.Parameters.AddWithValue("@AC_NumberNights", numberOfNights.Text);
            SQLCmd.Parameters.AddWithValue("@AC_PreferHotel", preferredHotel.Text);
            SQLCmd.Parameters.AddWithValue("@AC_PreferHotelURL", preferredHotelURL.Text);
            SQLCmd.Parameters.AddWithValue("@Flight", flight.SelectedValue);
            SQLCmd.Parameters.AddWithValue("@FL_Departure", flightDeparture);
            SQLCmd.Parameters.AddWithValue("@FL_Depart_Prefer", flightDeparturePreferred.Text);
            SQLCmd.Parameters.AddWithValue("@FL_Depart_URL", flightDeparturePreferredURL.Text);
            SQLCmd.Parameters.AddWithValue("@FL_Return", flightReturn);
            SQLCmd.Parameters.AddWithValue("@FL_Ret_Prefer", flightReturnPreferred.Text);
            SQLCmd.Parameters.AddWithValue("@FL_Ret_URL", flightReturnPreferredURL.Text);
            SQLCmd.Parameters.AddWithValue("@Notes", notes.Text);
            SQLCmd.Parameters.AddWithValue("@File", file);
            SQLCmd.Parameters.AddWithValue("@Status", "Pending");

            if (SQLConn.State == ConnectionState.Closed)
            {
                SQLConn.Open();
            }

            SQLCmd.ExecuteNonQuery();
            SQLConn.Close();

            string url = "Default.aspx";
            ClientScript.RegisterStartupScript(this.GetType(), "callfunction", "alert('Your request form was saved correctly!');window.location.href = '" + url + "';", true);
        }


The problem here is I'm trying to save the DateTime variables into the DB but it's not accepting their format... they have an output like {14-Jan-15 02:10:00} and DB ask for something like dd/MM/yyyy hh:mm:ss what should I do?
Posted
Updated 27-Jan-15 0:56am
v2
Comments
Kornfeld Eliyahu Peter 27-Jan-15 5:33am    
Try this:
SQLCmd.Parameters.Add("@Event_Start", SqlDbType.DateTime).Value = eventStart;
varmartins 27-Jan-15 6:28am    
Thanks for the suggestion but it didn't work the format didn't change
OriginalGriff 27-Jan-15 7:10am    
That's possibly because you have missed a name:
... Accomodation, AC_NumberNights, AC_PreferHotelURL, Flight,...
... @Accomodation,@AC_NumberNights,@AC_PreferHotel,@AC_PreferHotelURL,@Flight,...

Since you are passing your dates via parameters - correctly, you always should - the problem isn't with the date format, because the DateTime value is being passed as a DateTime, not as a formatted string. Instead, it is passed directly as a number of milliseconds since a nominated point in time, and can be inserted directly into an SQL DATETIME field.

The problem is more likely to be that you don't list the columns into which you are INSERTing your data:
C#
SQLCmd.CommandText = "insert into Event values(@Name,@Project,...
Which means that @Name will go into teh first columns SQL finds, @Project into the second, and so forth. If your SQL table definition includes any other columns (such as an ID column as is normal) or the columns get reordered for any reason, it will try to insert the values into the wrong columns, and that means it will try to auto convert them.
Try listing the column names:
C#
SQLCmd.CommandText = "INSERT INTO Event ([Name], Project, ...) VALUES (@Name,@Project,...
And see if the problem goes away.
 
Share this answer
 
Comments
varmartins 27-Jan-15 6:49am    
Did this didn't work :s
varmartins 27-Jan-15 6:52am    
Here's what I did:

SQLCmd.CommandText = "INSERT INTO Event (Name, Project, Objectives, City, Country, Event_Start, Departure, Arrival, Registration, National_Transportation, Accomodation, AC_NumberNights, AC_PreferHotelURL, Flight, FL_departure, FL_Depart_Prefer, FL_Depart_URL, FL_Return, FL_Ret_Prefer, FL_RET_URL, Notes, File, Status) VALUES (@Name,@Project,@Objectives,@City,@Country,@Event_Start,@Departure,@Arrival,@Registration,@National_Transportation,@Accomodation,@AC_NumberNights,@AC_PreferHotel,@AC_PreferHotelURL,@Flight,@FL_Departure,@FL_Depart_Prefer,@FL_Depart_URL,@FL_Return,@FL_Ret_Prefer,@FL_Ret_URL,@Notes,@File,@Status)";
varmartins 27-Jan-15 6:54am    
This is the error:

An exception of type 'System.Data.SqlTypes.SqlTypeException' occurred in System.Data.dll but was not handled in user code

Additional information: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
Kornfeld Eliyahu Peter 27-Jan-15 6:57am    
It's not anymore a time format error...:-)
What the value of that field?
Kornfeld Eliyahu Peter 27-Jan-15 6:59am    
I see a problem in your field list here:
AC_NumberNights, AC_PreferHotelURL, Flight,
@AC_NumberNights,@AC_PreferHotel,@AC_PreferHotelURL,@Flight,
As an aside to Solution 1 and in addition to the comment about trying
SQLCmd.Parameters.Add("@Event_Start", SqlDbType.DateTime).Value = eventStart;
I always advise using culture-agnostic formats when using dates - i.e. an unambiguous format.

Consider the following sql
SQL
create table demo
(
  demoDate datetime
  )
insert into demo VALUES('10-01-2015')
In the UK that looks like I'm inserting "10th January 2015" into the database, but the SQL Server instance is actually hosted in the USA so when I select that data I actually get back
October, 01 2015 00:00:00+0000

But if I use
insert into demo VALUES('10-JAN-2015')
I now get back what I expected
January, 10 2015 00:00:00+0000
 
Share this answer
 
Really sorry about all that work guys my bad! I read DB a thousand times and just noticed now it said Hotal insteas of Hotel... really sorry guys :s
 
Share this answer
 
Comments
CHill60 27-Jan-15 10:14am    
I wouldn't have expected the exception you reported for a simple spelling mistake
Problem is your parameter mapping is incorrect. You have 23 fields but you are passing 24 fields in parameters. AC_PreferHotelURL fields is incorrectly mapped to @AC_PreferHotel and this has caused all further parameters to be in invalid order. Correct command would be like following.

SQLCmd.CommandText = "INSERT INTO Event (Name, Project, Objectives, City, Country, Event_Start, Departure, Arrival, Registration, National_Transportation, Accomodation, AC_NumberNights, AC_PreferHotelURL, Flight, FL_departure, FL_Depart_Prefer, FL_Depart_URL, FL_Return, FL_Ret_Prefer, FL_RET_URL, Notes, File, Status) VALUES (@Name,@Project,@Objectives,@City,@Country,@Event_Start,@Departure,@Arrival,@Registration,@National_Transportation,@Accomodation,@AC_NumberNights,@AC_PreferHotelURL,@Flight,@FL_Departure,@FL_Depart_Prefer,@FL_Depart_URL,@FL_Return,@FL_Ret_Prefer,@FL_Ret_URL,@Notes,@File,@Status)"
 
Share this answer
 
Comments
varmartins 27-Jan-15 9:36am    
fixed this, still complains about format

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