到日期时间的转换错误 [英] covertion error to datetime
本文介绍了到日期时间的转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里我必须将txtbox'的值转换为日期时间,并且还希望在表格中进行转换..
它正在收到错误。
错误是
字符串未被识别为有效的DateTime。在System.DateTimeParse.Parse(String s,DateTimeFormatInfo dtfi,DateTimeStyles样式)at System.Convert.ToDateTime(String value)at fc.Registration.submit_Click1(Object sender,EventArgs e)''
这是我的代码
here i have to convert txtbox''s value into date time and also want to stred in table..
bt it''s getting error .
error is
String was not recognized as a valid DateTime. at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.Convert.ToDateTime(String value) at fc.Registration.submit_Click1(Object sender, EventArgs e)''
here is my code
protected void submit_Click1(object sender, EventArgs e)
{
try
{
SqlConnection sc = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\My projects5-6 sem\friend's corner\back up\fc 20-3\fc\App_Data\fc.mdf;Integrated Security=True;User Instance=True");
sc.Open();
string str;
str = bdt_txt.Text;
DateTime dt;
dt = DateTime.Now;
string role = "user";
string cmdstr1 = "INSERT INTO login VALUES('" + unm_txt.Text + "','" + pwd_txt.Text + "','" + DropDownList9.SelectedItem.Value + "','" + ans_txt.Text + "','" + eml_txt.Text + "','" + dt + "','" + role + "')";
SqlCommand cmd1 = new SqlCommand(cmdstr1, sc);
dt = Convert.ToDateTime(str);
cmd1.ExecuteNonQuery();
string cmdstr = "INSERT INTO register VALUES('" + fnm_txt.Text + "','" + lnm_txt.Text + "','" + eml_txt + "','" + unm_txt.Text + "','" + pwd_txt.Text + "','" + dt + "','" + phn_txt.Text + "','" + RadioButtonList1.SelectedItem.Value + "')";
SqlCommand cmd = new SqlCommand(cmdstr, sc);
cmd.ExecuteNonQuery();
string cmdstr2 = "INSERT INTO profile VALUES('" + unm_txt.Text + "','" + null + "','" + null + "','" + null + "','" + null + "','" + null + "','" + null + "')";
SqlCommand cmd2 = new SqlCommand(cmdstr2, sc);
cmd2.ExecuteNonQuery();
// string cmdstr3 = "INSERT INTO birthday VALUES('" + unm_txt.Text + "','" + dt.ToString("d") + "','" + null + "','" + null + "','" + null + "','" + null + "')";
sc.Close();
Response.Redirect("Rsuccessful.aspx");
}
catch (Exception ex)
{
Response.Write(ex.Message);
Label3.Visible = true;
Label3.Text = ex.ToString();
}
}
推荐答案
尝试使用DateTime.TryParse而不是Convert。虽然它应该工作,但尝试一个特定的文化,也许尝试自己指定一种高级格式,并尝试不变的文化。与样式相同。
try DateTime.TryParse instead of Convert. though it should work anyway, try a specific culture, maybe try to specifiy an advanced format yourself and try invariant culture, too. same with styles.
// Parse a date and time with no styles.
dateString = "03/01/2009 10:00 AM";
culture = CultureInfo.CreateSpecificCulture("en-US");
styles = DateTimeStyles.None;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
Console.WriteLine("Unable to convert {0} to a date and time.",
dateString);
请修剪字符串您获取日期的文本框,然后将其转换为日期时间。
please trim the string of text box where your getting the date, and then convert it to date time.
您好,
字符串日期应为MM / DD / YYYY格式。
Hi,
string date should be MM/DD/YYYY format.
protected void Page_Load(object sender, EventArgs e)
{
string strDate = "03/20/2013";
DateTime odt;
odt = DateTime.Now;
odt = Convert.ToDateTime(a);
Response.Write(odt.ToString());
}
这篇关于到日期时间的转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文