Winform 索引超出范围异常
我的代码有什么问题吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题实际上是从将日期存储在 varchar 类型的列中开始的。只需一台文化设置错误的机器就会损坏数据库表,因此所有尝试读取它的机器都会崩溃。解决实际问题,修复表。
无论如何,您需要改进代码,以便数据库管理员有机会修复损坏。抛出一个提供足够信息的异常。像这样的东西:
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: