更新时间:2023-02-18 20:55:39
看看那一行:TextBox6.Text = DateTime.TryParseExact(dateString,format,CultureInfo.InvariantCulture,DateTimeStyles.None, out dateTime);
TryParse / TryParseExact返回一个布尔值:布尔返回值表示解析是成功还是失败。
您应该像以下一样使用它:if (!DateTime.TryParseExact(TextBox6.Text,format,CultureInfo.InvariantCulture,DateTimeStyles.None, out dateTime))
{
// 显示一条消息给用户OR
// 使用默认值或
// 做你认为有用的事情l当TextBox6中的数据不是日期
};
此外,在代码中使用更远的dateTime变量而不是TextBox6。文本!块引用>Hello ,
I want to particular date data i write some code and also iam using sql server 2008
the column type rdate=datetime some i am given the date in one textbox the textbox
date data can be display the gridview but here is the some error so please help me
i am given the source codestring dateString = TextBox6.Text; // <-- Valid string format = "ddd dd MMM h:mm tt yyyy"; DateTime dateTime; TextBox6.Text = DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime); SqlConnection con = new SqlConnection("Data Source=NSYS1\\SQLEXPRESS;Initial Catalog=agfl;connect timeout=30;Integrated Security=True"); con.Open(); string strQuery = "select * from stf where rdate =@some"; SqlCommand cmd = new SqlCommand(strQuery, con); cmd.CommandType = CommandType.Text; TextBox6.Text = TextBox6.Text.Trim(); cmd.Parameters.AddWithValue("@some", TextBox6.Text); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); gvuser.DataSource = dt; gvuser.DataBind(); con.Close();Look at that line:TextBox6.Text = DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);
TryParse / TryParseExact return a Boolean: the boolean return value indicates whether the parsing succeeded or failed.
You ought to use it like:if (!DateTime.TryParseExact(TextBox6.Text, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) { //show a message to the user OR //use a default value OR //do what ever you think is useful when the data in TextBox6 are not a date };
Also, use the dateTime variable farther down in your code instead of TextBox6.Text!