日期时间到gridview更新命令上的字符串转换 [英] Datetime to string conversion on gridview update command

查看:103
本文介绍了日期时间到gridview更新命令上的字符串转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生m使用gridview来管理我的数据库记录,但是在更新gridview updatecommand上的记录时出现错误,其中m列的日期已过帐,并且oneditcommand的值显示在文本框中,但当我更新记录时显示错误.是错误

"System.Data.SqlClient.SqlException:将varchar数据类型转换为日期时间数据类型会导致超出范围的值.
该语句已终止."

date_posted列的数据类型在sql中为datetime.


这是我的更新命令

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();
    }

推荐答案

您有"
Date_Posted=''"+ date 

尝试

date.ToString("dd-MMM-yyyy")


的地方,您可能会犯规日期格式-尝试在赋值期间强制转换值,或确保"date"位于明确的格式
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


您应该使用DateTime.Parse或DateTime.TryParse命令以正确的格式获取字符串
You should use the DateTime.Parse or DateTime.TryParse commands to get your string in the correct format


这篇关于日期时间到gridview更新命令上的字符串转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆