I want to insert the (Day,Date,Time) from a Calendar and TimePicker a in my DB but I got this error

"SqlDateTime overflow"

Date access code:

 public void InsertExam(SqlCommand cmd)
    {
        con.Open();
        cmd.Connection = con;
        cmd.CommandText = "InsertExam";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.ExecuteNonQuery();
        con.Close();

    }

Business

 public void InsertExam_record()
        {

            cmd.Parameters.AddWithValue("@EWeekday", EWeekday);
            cmd.Parameters.AddWithValue("@BegainTime", BegainTime);
            cmd.Parameters.AddWithValue("@EndTime", EndTime);
            cmd.Parameters.AddWithValue("@Duration", Duration);
            cmd.Parameters.AddWithValue("@DateAD", DateAD);
            cmd.Parameters.AddWithValue("@CourseID", CourseID);
            cmd.Parameters.AddWithValue("@SemsterID", SemsterID);

            dal.InsertExam(cmd);  
        }

CS

protected void Page_Load(object sender, EventArgs e)

    {
        if (!this.IsPostBack)
        {
            DateTime dt = DateTime.Parse("08:30 AM");
            MKB.TimePicker.TimeSelector.AmPmSpec am_pm;
            if (dt.ToString("tt") == "AM")
            {
                am_pm = MKB.TimePicker.TimeSelector.AmPmSpec.AM;
            }
            else
            {
                am_pm = MKB.TimePicker.TimeSelector.AmPmSpec.PM;
            }
            TimeSelector1.SetTime(dt.Hour, dt.Minute, am_pm);
        }
        CourseGridView.DataBind();
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        TextBox1.Text = Calendar1.SelectedDate.ToString("ddd", new System.Globalization.CultureInfo("ar-SA"));
        TextBox2.Text= Calendar1.SelectedDate.ToString("yyyy/MM/dd", new System.Globalization.CultureInfo("ar-SA"));
        TextBox3.Text = Calendar1.SelectedDate.ToString("yyyy/MM/dd");
    }
    protected void Submit(object sender, EventArgs e)
    {
        DateTime start = DateTime.Parse(string.Format("{0}:{1}", TimeSelector1.Hour, TimeSelector1.Minute));
        DateTime end = DateTime.Parse(string.Format("{0}:{1}", TimeSelector2.Hour, TimeSelector2.Minute));

        TimeSpan du = end - start;
        double r = du.TotalHours;

        EndTime.Text = r.ToString();

        b.EWeekday = TextBox1.Text;
       b.BegainTime = Convert.ToDateTime(start);
      b.EndTime = Convert.ToDateTime(end);
        b.Duration = Convert.ToInt32(r);
        b.DateAD = Convert.ToDateTime(TextBox3.Text);
        b.CourseID = Convert.ToInt32(CourseGridView.SelectedValue);
        b.SemsterID = Convert.ToInt32(SemsterList.SelectedValue);

        b.InsertExam_record();



    }

and this is my Stored Procedure

[dbo].[InsertExam]
    -- Add the parameters for the stored procedure here

    @EWeekday NVARCHAR (50),
    @BegainTime Time,
    @EndTime Time,
    @Duration INT,
    @DateAD Date,
    @CourseID INT,
    @SemsterID INT
AS
BEGIN
INSERT INTO Exam
                         ( EWeekday, BegainTime, EndTime, Duration, DateAD, CourseID, SemsterID)
VALUES                   (@EWeekday,@BegainTime,@EndTime,@Duration,@DateAD,@CourseID,@SemsterID)

END
  • What are the datatypes of fields in table Exam? – VK_217 Mar 11 '16 at 17:41
  • these are the datatypes EWeekday NVARCHAR (50), BegainTime Time, EndTime Time, Duration INT, DateAD Date, CourseID INT, SemsterID INT – nouf Mar 11 '16 at 18:58
  • I tried to mock this up using a C# console application and it worked for me. I can't really test the source of your data that is being inserted in the database though. When you receive the SqlDateTime overflow error/exception, what are the exact values being passed to EWeekday, BegainTime, EndTime, Duration, DateAD, CourseID, SemsterID? Maybe add System.Diagnostics.Debug.WriteLine("EWeekday:{0};BegainTime:{1};EndTime:{2};Duration:{3};DateAD:{4};CourseID:{5};SemesterID:{6}", b.EWeekday, b.BegainTime, b.EndTime, b.Duration, b.DateAD, b.CourseID, b.SemsterID); to get these values. – Shayne Ephraim Mar 11 '16 at 21:00
  • These are the values postimg.org/image/fd3ms44yn and l using C# asp.net Web application – nouf Mar 12 '16 at 18:25

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.