Click here to Skip to main content
12,395,645 members (53,708 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server
Sir m using using gridview to manage my database record but m getting an error while updating a record on gridview updatecommand m having a column dateposted and oneditcommand the value in shown in textbox but when i am updating the record error shows..this is the error

"System.Data.SqlClient.SqlException: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated."

Datatype of date_posted column is datetime in sql.

this is my update command
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        string Requirement_Id = Convert.ToString(GridView1.DataKeys[e.RowIndex].Values["Requirement_Id"]);
        DropDownList rbtnreqfor = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlreqfor");
        string reqfor = Convert.ToString(rbtnreqfor.SelectedItem);
        DropDownList PropertyType = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlpropertytype");
        string ptype = Convert.ToString(PropertyType.SelectedItem);
        DropDownList PropertyAge = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlpropertyage");
        string pAge = Convert.ToString(PropertyAge.SelectedItem);

        DropDownList City = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlcity");
        string city = Convert.ToString(City.SelectedItem.Value);
        DropDownList Locality = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddllocality");
        string locality = Convert.ToString(Locality.SelectedItem.Value);
        DropDownList MinPrice = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlminprice");
        string minprice = Convert.ToString(MinPrice.SelectedItem);
        DropDownList MaxPrice = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlmaxprice");
        string maxprice = Convert.ToString(MaxPrice.SelectedItem);
        DropDownList Bedroom = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlbedrooms");
        string bedroom = Convert.ToString(Bedroom.SelectedItem);
        TextBox Area = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtarea");
        string area = Area.Text;
        TextBox Features = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtfeatures");
        string features = Features.Text;
        TextBox Date = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdate");
        //DateTime date = Convert.ToDateTime(Date.Text);
         DateTime date = DateTime.Parse(Date.Text); 
        TextBox Name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname");
        string name = Name.Text;
        TextBox Email = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtemail");
        string email = Email.Text;
        TextBox Mobile = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtmob");
        string mobile = Mobile.Text;
        CheckBox Active = (CheckBox)GridView1.Rows[e.RowIndex].FindControl("chkactive");
        string active = Convert.ToString(Active.Checked);

        SqlConnection con = new SqlConnection(str);
        string strQ = "update tbl_Post_Requirement set Requirement_For = '"+reqfor+"',Property_Type = '"+ptype+"',Property_Age = '"+pAge+"',City_Id = '"+city+"',Locality_Id='"+locality+"',Min_Price = '"+minprice+"',Max_Price = '"+maxprice+"',Bedrooms='"+bedroom+"',Area='"+area+"',Key_Features='"+features+"',Date_Posted='"+ date +"',Name='"+name+"',Email='"+email+"',Mobile='"+mobile+"', IsActive = '" + active + "'  where Requirement_Id='" + Requirement_Id + "'";
        SqlCommand cmd = new SqlCommand(strQ, con);
        lblmessage.Text = "Record Updated Successfully";
        GridView1.EditIndex = -1;
Posted 21-Nov-12 2:23am
Updated 21-Nov-12 2:30am
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

E.g. where you have "
Date_Posted='"+ date 
Raj.Rautela 21-Nov-12 12:16pm
thanks sir got it now thanks a lot...
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You may be falling foul of date formatting - try casting the value during the assignment or making sure that 'date' is in an unambiguous format
Raj.Rautela 21-Nov-12 11:37am
m not getting u sir can u pls give same example..
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You should use the DateTime.Parse or DateTime.TryParse commands to get your string in the correct format
Raj.Rautela 21-Nov-12 12:17pm
thanks sir
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

E.g. where you have "
Date_Posted='"+ date 
Date_Posted='"+  date.ToString("dd-MMM-yyyy")

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 21 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100