“字符串未被识别为有效的DateTime。” [英] "String was not recognized as a valid DateTime."

查看:100
本文介绍了“字符串未被识别为有效的DateTime。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hiii ...

Hiii...

SqlConnection con = new SqlConnection(cn.ConnectionStrings);
            GridViewRow row = (GridViewRow)grdLead.Rows[e.RowIndex];
            int userid = Convert.ToInt32(grdLead.DataKeys[e.RowIndex].Value.ToString());
            Label lblLedId = (Label)grdLead.FindControl("lblLedId");
            TextBox txtleadid = (TextBox)row.Cells[0].Controls[0];
            TextBox txtfirstname = (TextBox)row.Cells[1].Controls[0];
            TextBox txtdate = (TextBox)row.Cells[5].Controls[0];
            TextBox txtstatus = (TextBox)row.Cells[6].Controls[0];
            TextBox txtrevenue=(TextBox)row.Cells[3].Controls[0];
            TextBox txtproduct=(TextBox)row.Cells[4].Controls[0];
            TextBox txtowner = (TextBox)row.Cells[7].Controls[0];
            TextBox txtcompany = (TextBox)row.Cells[2].Controls[0];
            GridView1.EditIndex = -1;
            con.Open();
            string QUERY=" UPDATE [WebCrm].[dbo].[tbl_Lead] SET FirstName='" + txtfirstname.Text + "', Date='" +Convert.ToDateTime(txtdate.Text) + "', Status='" + txtstatus.Text + "',PotentialRevenue='" + txtrevenue.Text + "',ProductInterest='" + txtproduct.Text + "', Owner='" + txtowner.Text + "',Company='" + txtcompany.Text + "' where LedId=" + userid + "";
            SqlCommand cmd = new SqlCommand(QUERY, con);
            cmd.ExecuteNonQuery();
            con.Close();

推荐答案

检查中的值是一个合适的约会时间。

使用 CONVERT [ ^ ]或 DateTime.TryParse [ ^ ]确认日期格式正确。



另外,直接传递字段名称由于 SQL注入 [ ^ ]。
Check the value inside TextBox txtdate = (TextBox)row.Cells[5].Controls[0]; is a proper date time.
Use CONVERT[^] on the SQL Server side or DateTime.TryParse[^] on the client side to confirm date format is correct.

In addition, passing field names directly is never a good idea because of SQL injection[^].


这篇关于“字符串未被识别为有效的DateTime。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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