0

我的代码有什么问题??

  string birthdate = ((DataRowView)comboBox1.SelectedItem).Row["birthdate"].ToString(); //pulled the data into the database ()

string[] split = birthdate.Split('/'); //split the Date

我想把它们放在一个文本框中,所以我想这样做:

textbox1.Text = split[0]; //correct, gets the 1st word the (Day)
    textbox2.Text = split[1]; //incorrect, outofrange exception (Month)
    textbox3.Text = split[2]; //incorrect, outforange exception (Year)

注意:格式为日/月(字)/年 ==> 1/January/2012

有人可以帮我获取这些值并将它们一一放入文本框中吗?

4

1 回答 1

2

这个问题实际上是从将日期存储在 varchar 类型的列中开始的。只需要一台文化设置错误的机器来损坏数据库表,因此所有试图读取它的机器都会被炸毁。解决实际问题,修复表。

无论如何,您需要改进您的代码,以便 dbase 管理员有机会修复损坏。抛出一个提供足够信息的异常。就像是:

string[] split = birthdate.Split('/');
if (split.Length != 3) {
    throw new Exception("Invalid date string for table entry " + row["primarykey"].ToString());
}
于 2012-01-21T16:32:57.733 回答