I've been stuggling with this for a couple hours and CANNOT figure out it! It's starting to drive me nuts. I'm kind of new to C# so I might not be doing this right!!
I have an Event class with TimeStart, TimeEnd, and Date, all as DateTime objects.
In my Event table in my DB, TimeStart, TimeEnd, and Date are all DateTime database types.
I have a DAL that controls all interaction with the database. I'm using the InsertEvent function to insert an event with information collected from a form. I'm using parameters with my SQL statement. I'm creating an instance of the Event class called myEvent
and filling those properties with values from the form and passing my instantiated myEvent object as an argument to InsertEvent.
I keep getting "Conversion failed when converting datetime from character string." from Line 373: cmd.ExecuteNonQuery(); (in my Event.cs file in app_code). I've checked and made sure the DateTime in SQL and DateTime on my computer match, they are
in the same format. I've also removed the contents of the calendar controls on my form and filled the 3 properties of myEvent (date, timestart, and timeend) with DateTime.Now. I also tried putting # around my parameters like #@TimeStart# and it still does
not work. What's going on here!!!!
Should I just store everything in the database and a string and just convert it to datetime when I need to use it or should I keep every date as a DateTime and keep dealing with this stuff?!?!
Here's my code:
### this is in my DAL ###
public static int InsertEvent(Event myEvent, SqlConnection conn)
{
if (String.IsNullOrEmpty(myEvent.OwnerFirst
throw new ArgumentException("First Name cannot be null or empty.");
if (String.IsNullOrEmpty(myEvent.OwnerLast)
throw new ArgumentException("Last Name cannot be null or empty.");
if (myEvent.Affiliation == null) { myEvent.Affiliation = String.Empty; }
//insert more stuff here
string sql = "INSERT INTO Event " +
"(AV, Date, Affiliation, OwnerLast, OwnerFirst, OwnerEmail, RoomID, " +
"SpecialReq, TimeEnd, TimeStart, OrgID, MasterCal, Approved) " +
"VALUES (@AV, @Affiliation, @Date, @OwnerLast, @OwnerFirst, @OwnerEmail, @RoomID, " +
"@SpecialReq, @TimeEnd, @TimeStart, @OrgID, @MasterCal, @Approved); " +
"SELECT @eventID = SCOPE_IDENTITY()";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add(new SqlParameter("@AV", myEvent.AV));
cmd.Parameters.Add(new SqlParameter("@Date", DateTime.Now)); //would be myEvent.Date
cmd.Parameters.Add(new SqlParameter("@Affiliation", myEvent.Affiliation));
cmd.Parameters.Add(new SqlParameter("@OwnerLast", myEvent.OwnerLast));
cmd.Parameters.Add(new SqlParameter("@OwnerFirst", myEvent.OwnerFirst));
cmd.Parameters.Add(new SqlParameter("@OwnerEmail", myEvent.OwnerEmail));
cmd.Parameters.Add(new SqlParameter("@RoomID", myEvent.RoomID));
cmd.Parameters.Add(new SqlParameter("@SpecialReq", myEvent.SpecialReq));
cmd.Parameters.Add(new SqlParameter("@TimeEnd", DateTime.Now)); //would be myEvent.TimeEnd;
cmd.Parameters.Add(new SqlParameter("@TimeStart", DateTime.Now)); //would be myEvent.TimeStart;
cmd.Parameters.Add(new SqlParameter("@OrgID", myEvent.OrgID));
cmd.Parameters.Add(new SqlParameter("@MasterCal", myEvent.MasterCal));
cmd.Parameters.Add(new SqlParameter("@Approved", myEvent.Approved));
SqlParameter p = cmd.Parameters.Add("@eventID", SqlDbType.Int);
p.Direction = ParameterDirection.Output;
int newEventID = 0;
cmd.ExecuteNonQuery(); <--- this is where I get the error, line 372
newEventID = (int)p.Value;
return newEventID;
}
### this is the code in my form to create an instance of event and populate with form data ###
newEvent.AV = av;
newEvent.Date = calDate.SelectedDate;
newEvent.Affiliation = ddlAffiliation.Text.ToString();
newEvent.OwnerLast = txtLName.Text.ToString();
newEvent.OwnerFirst = txtFName.Text.ToString();
newEvent.OwnerEmail = txtEmail.Text.ToString();
newEvent.SpecialReq = mySR;
newEvent.TimeStart = startTime.SelectedTime;
newEvent.TimeEnd = endTime.SelectedTime;
newEvent.OrgID = Convert.ToInt32(ddlOrg.SelectedValue);
newEvent.MasterCal = masterCal;
newEvent.Approved = 0;
newEvent.RoomID = ddlRoomReq.SelectedIndex.ToString();
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.