在循环中第二次给出异常 [英] Gives exception for the second time in looping
本文介绍了在循环中第二次给出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的代码,通过该代码,我从文本框中的日期和年份分割文本框中的月份,问题是我的循环在第一次执行正常但是第二次我的todate在这两行附近显示数组为1在单词附近的单词1
Below is my code through which i am splitting month from the textbox from date and year in the text box todate the problem is my loop executes fine for the first but for the second time my todate near these two lines shows array as 1 in the below line near words1
string[] text = getdate;
What I have tried:
i tried the above code which works only once in the loop
推荐答案
试试这个
try this
for (int i = 0; i < dt.Rows.Count; i++)
{
string date = dt.Rows[i]["Month"].ToString();
string[] date1 = date.Split('/');
string month = date1[0];
string year = date1[2];
//from date
string getmonth = txtfromdate.Text.Trim();
string[] words = getmonth.Split('/');
string newmonth = words[0].ToString();
//todate
string getyear = txtToDate.Text.Trim();
string[] words1 = getyear.Split('/');
string newyear = words1[2].ToString();
if (month == newmonth && year == newyear)
{
txtfromdate.Text = newmonth.ToString();
txtToDate.Text = newyear.ToString();
Ismatch = true;
break;
}
else
{
Ismatch = false;
}
}
这篇关于在循环中第二次给出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文