Click here to Skip to main content
15,891,926 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
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
C#
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);
        con.Open();
        cmd.ExecuteNonQuery();
        lblmessage.Text = "Record Updated Successfully";
        GridView1.EditIndex = -1;
        Filldata();
    }
Posted
Updated 21-Nov-12 2:30am
v3

E.g. where you have "
Date_Posted='"+ date 
try
date.ToString("dd-MMM-yyyy")
 
Share this answer
 
Comments
Raj.Rautela 21-Nov-12 12:16pm    
thanks sir got it now thanks a lot...
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
 
Share this answer
 
Comments
Raj.Rautela 21-Nov-12 11:37am    
m not getting u sir can u pls give same example..
You should use the DateTime.Parse or DateTime.TryParse commands to get your string in the correct format
 
Share this answer
 
Comments
Raj.Rautela 21-Nov-12 12:17pm    
thanks sir
E.g. where you have "
Date_Posted='"+ date 
try
Date_Posted='"+  date.ToString("dd-MMM-yyyy")
 
Share this answer
 
v2

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