Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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
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 21-Nov-12 2:23am
Edited 21-Nov-12 2:30am
__TR__28.8K
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

E.g. where you have "
Date_Posted='"+ date 
try
date.ToString("dd-MMM-yyyy")
  Permalink  
Comments
Raj.Rautela at 21-Nov-12 12:16pm
   
thanks sir got it now thanks a lot...
Rate this: bad
good
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
  Permalink  
Comments
Raj.Rautela at 21-Nov-12 11:37am
   
m not getting u sir can u pls give same example..
Rate this: bad
good
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
  Permalink  
Comments
Raj.Rautela at 21-Nov-12 12:17pm
   
thanks sir
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 529
1 OriginalGriff 307
2 Damith Weerasinghe 295
3 ravikhoda 260
4 Tadit Dash 180
0 Sergey Alexandrovich Kryukov 10,974
1 OriginalGriff 6,667
2 Peter Leow 4,760
3 Abhinav S 3,813
4 Maciej Los 3,565


Advertise | Privacy | Mobile
Web01 | 2.8.140421.2 | Last Updated 21 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid