Winform 索引超出范围异常

发布于 2024-12-28 00:18:42 字数 571 浏览 1 评论 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

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

What's wrong with my code??

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

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

I want to put them in a textbox so I thought of doing this:

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)

NOTE: The format is Day/Month(words)/Year ==> 1/January/2012

Can somebody help me get that values and put them one by one in a textbox?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落成白 2025-01-04 00:18:42

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

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

string[] split = birthdate.Split('/');
if (split.Length != 3) {
    throw new Exception("Invalid date string for table entry " + row["primarykey"].ToString());
}

This problem really got started by storing dates in a column of type varchar. It takes only one machine with the culture set wrong to damage the database table so all machines that try to read it will bomb. Solve the real problem, fix the table.

Anyhoo, you'll want to improve your code so the dbase admin will have a fighting chance to repair the damage. Throw an exception that gives sufficient information. Something like:

string[] split = birthdate.Split('/');
if (split.Length != 3) {
    throw new Exception("Invalid date string for table entry " + row["primarykey"].ToString());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文