Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
private int  SaveToDB (string st ,string  brt,string  brot, string  spt)
{
    con1.Open();
    SqlCommand cmd = new SqlCommand("dattime", con1);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Connection = con1 ;
    cmd.Parameters.Add("@st", SqlDbType.DateTime ).Value = label2_time1.Text   ;
    cmd.Parameters.Add("@brt", SqlDbType.DateTime ).Value = label2_brkintime.Text  ;
    cmd.Parameters.Add("@brot", SqlDbType.DateTime ).Value = label3_brkofftm.Text ;
    cmd.Parameters.Add("@spt", SqlDbType.DateTime ).Value = label2_time2.Text ;
    cmd.ExecuteReader();

    con1.Open();
     int  tm = cmd.ExecuteNonQuery();
     con1.Close();
     return tm;

}
Posted

You didn't post your SQL, but make sure that you are setting the parameters on the SQL side as a datetime data type. Also, right now, if you are doing it that way, keep in mind that you are passing the parameters in the 'SaveToDB' method as strings. You may want to convert them to a DateTime object before you pass them, as if one is a string and the other is a datetime, it won't accept it; but it also depends on how they are setting it up in your SPROC.

If you are still having issues, please post your stored procedure. Have a great night. I hope all goes well for you.
 
Share this answer
 
Hi there,

This is simple. Often, strings cannot be directly casted to DateTime objects due to that, the date format can be anything. Therefore, before assigning it to the parameter, convert the string to a datetime object.

Hope this helps, regards
 
Share this answer
 

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