Click here to Skip to main content
15,906,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi frnds,

Please can you help me this error.

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
The statement has been terminated.

This code is in Gridview Update Button Click. I have a Update Image Button In Gridview.

In Database I gave Issuedate and ExpiryDate DATATYPE as DateTime.

C#
protected void btnUpdate_Click(object sender, EventArgs e)
   {
       con.Open();

SqlCommand cmd = new SqlCommand("UPDATE AddUsers SET FirstName='" + txtfname.Text + "',LastName='" + txtlname.Text + "',EmpNo='" + txtEmpNo.Text + "',Sponsor='" + txtSponsor.Text + "',ManagerName='" + txtManagerName.Text + "', Dept='" + txtDepartment.Text + "',Location='" + txtLocation.Text + "',OffTelNo='" + txtOffTelNo.Text + "',MobileNo='" + txtMobileNo.Text + "',IssuedDate='" + txtIssueDate.Text + "',ExpiryDate='" + txtExpiryDate.Text + "' Where Id=@Id", con);

C#
cmd.Parameters.AddWithValue("@FirstName", txtfname.Text);
        cmd.Parameters.AddWithValue("@LastName", txtlname.Text);
        cmd.Parameters.AddWithValue("@EmpNo", txtEmpNo.Text);
        cmd.Parameters.AddWithValue("@Sponsor", txtSponsor.Text);
        cmd.Parameters.AddWithValue("@ManagerName", txtManagerName.Text);

        cmd.Parameters.AddWithValue("@Dept", txtDepartment.Text);

        cmd.Parameters.AddWithValue("@Location", txtLocation.Text);

        cmd.Parameters.AddWithValue("@OffTelNo", txtOffTelNo.Text);

        cmd.Parameters.AddWithValue("@MobileNo", txtMobileNo.Text);

        cmd.Parameters.AddWithValue("@IssuedDate", txtIssueDate.Text);

        cmd.Parameters.AddWithValue("@ExpiryDate", txtExpiryDate.Text);
        cmd.Parameters.AddWithValue("@Id", Convert.ToInt32(lblID.Text));

 cmd.ExecuteNonQuery();
        con.Close();

 ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Updated successfully');", true);
        BindGV();
}



This is the error am getting when update button click.

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
The statement has been terminated.

Please help. THANKS.
Posted
Comments
Karwa_Vivek 1-Jul-13 3:20am    
Are you Using DateTimePicker Control To Input the Date or you Using a textBox
Software Engineer 892 1-Jul-13 4:22am    
am using Calendar Extender.

1 solution

C#
DateTime txtMyDate = DateTime.ParseExact(txtDate.Text, , "dd/MM/yyyy",CultureInfo.InvariantCulture) 

you have to Parse the TextBox Values to DateTime First
Try this May this Solves your Query
 
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