Issue: Index was outside the bounds of the array.
I generate xml file on button click event in windows forms app. First time xml are generate successfully but next time we click button then it Provide Exception (Index was outside the bounds of the array.). Please give me solution for remove this Exception. My Code.
protected void generatetickerxml_update()
{
try
{
if (combo_rss.SelectedItem.ToString() != "-SELECT RSS-")
{
string path = Readxml();
if (File.Exists(path + "\\oohmp\\xml\\" + combo_rss.SelectedItem.ToString() + ".xml"))
{
str = new StringBuilder();
int i;
string rssinterval = Convert.ToString(Convert.ToInt16(combo_rssinterval.SelectedItem.ToString()) * 1000);
string checkagain = Convert.ToString(Convert.ToInt16(combo_checkagain.SelectedItem.ToString()) * 1000);
//string str_flag = combo_rss.SelectedItem.ToString();
//if (str_flag == "bot")
//{
// str_flag = "bottom";
//}
string col = txt_forecolorhexanumber.Text.Trim();
string color = col.Replace("#", "");
string[] forecolor = color.Split(':');
string col1 = txt_bghexanumber.Text.Trim();
string color1 = col1.Replace("#", "");
string[] bgcolor = color1.Split(':');
for (i = 0; i < dataGridView3.Rows.Count; i++)
{
string strt = dataGridView3.Rows[i].Cells["title"].Value.ToString();
if (strt != "")
{
if (str.Length == 0)
{
" Error Line" str.Append(String.Format("<xml>{1}<rss id=\"{7}\" topfont=\"font face='{0}' size={2} color={3}\" bgcolor=\"{6}\" color=\"{3}\" size=\"{2}\" rssinterval=\"{4}\" checkagain=\"{5}\" defaultmessage=\"{9}\" url=\"xml/{8}.xml\" font=\"{0}\" />", combo_font.SelectedItem.ToString().Trim(), Environment.NewLine, combo_size.SelectedItem.ToString().Trim(), forecolor[1].Trim(), rssinterval, checkagain, bgcolor[1].Trim(), combo_rss.SelectedItem.ToString(), combo_rss.SelectedItem.ToString(), txt_defaultmgs.Text.Trim()));
str.Append(String.Format("{0}\t<channel>{0}\t\t<item>{0}\t\t\t<title>{1}</title>{0}\t\t</item>", Environment.NewLine, dataGridView3.Rows[i].Cells["title"].Value.ToString()));
}
else
{
str.Append(String.Format("{0}\t\t<item>{0}\t\t\t<title>{1}</title>{0}\t\t</item>", Environment.NewLine, dataGridView3.Rows[i].Cells["title"].Value.ToString()));
}
}
}
//if (str_flag == "bottom")
//{
// str_flag = "bot";
//}
if (str.Length != 0)
{
str.Append(String.Format("{0}\t</channel>{0}</xml>", Environment.NewLine));
File.WriteAllText(path + "\\oohmp\\xml\\" + combo_rss.SelectedItem.ToString() + ".xml", str.ToString());
File.WriteAllText(path + "\\oohmp\\box\\" + combo_rss.SelectedItem.ToString() + ".xml", str.ToString());
//MessageBox.Show("Ticker text is saved", "Message");
}
Bindticker();
}
else
{
str = new StringBuilder();
int row = dataGridView3.Rows.Count;
string rssinterval = Convert.ToString(Convert.ToInt16(combo_rssinterval.SelectedItem.ToString()) * 1000);
string checkagain = Convert.ToString(Convert.ToInt16(combo_checkagain.SelectedItem.ToString()) * 1000);
string str_flag = combo_rss.SelectedItem.ToString();
//if (str_flag == "bot")
//{
// str_flag = "bottom";
//}
string col = txt_forecolorhexanumber.ToString().Trim();
string color = col.Replace("#", "");
string[] forecolor = color.Split(':');
string col1 = txt_bghexanumber.ToString().Trim();
string color1 = col1.Replace("#", "");
string[] bgcolor = color1.Split(':');
for (int j = 0; j < row; j++)
{
string strt = dataGridView3.Rows[j].Cells["title"].Value.ToString();
if (strt != "")
{
if (str.Length == 0)
{
str.Append(String.Format("<xml>{1}<rss id=\"{7}\" topfont=\"font face='{0}' size={2} color={3}\" bgcolor=\"{6}\" color=\"{3}\" size=\"{2}\" rssinterval=\"{4}\" checkagain=\"{5}\" defaultmessage=\"{9}\" url=\"xml/{8}.xml\" font=\"{0}\" />", combo_font.SelectedItem.ToString().Trim(), Environment.NewLine, combo_size.SelectedItem.ToString(), forecolor[1].Trim(), rssinterval, checkagain, bgcolor[1].Trim(), combo_rss.SelectedItem.ToString(), combo_rss.SelectedItem.ToString(), txt_defaultmgs.Text.Trim()));
str.Append(String.Format("{0}\t<channel>{0}\t\t<item>{0}\t\t\t<title>{1}</title>{0}\t\t</item>", Environment.NewLine, dataGridView3.Rows[j].Cells["title"].Value.ToString()));
}
else
{
str.Append(String.Format("{0}\t\t<item>{0}\t\t\t<title>{1}</title>{0}\t\t</item>", Environment.NewLine, dataGridView3.Rows[j].Cells["title"].Value.ToString()));
}
}
}
//if (str_flag == "bottom")
//{
// str_flag = "bot";
//}
if (str.Length != 0)
{
str.Append(String.Format("{0}\t</channel>{0}</xml>", Environment.NewLine));
File.WriteAllText(path + "\\oohmp\\xml\\" + combo_rss.SelectedItem.ToString() + ".xml", str.ToString());
File.WriteAllText(path + "\\oohmp\\box\\" + combo_rss.SelectedItem.ToString() + ".xml", str.ToString());
//MessageBox.Show("Ticker text is saved", "Message");
}
else
{
MessageBox.Show("Ticker Text Is Blank", "Error");
}
Bindticker();
}
}
else
{
MessageBox.Show("Please Select rss type.", "Message");
}
}
catch (Exception ex)
{
MessageBox.Show("Error Occured.", "Error");
}
}