Click here to Skip to main content
15,899,632 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

I have a requirement to change the date format from mm/dd/yyyy to dd/mm/yyyy and viceversa. If my column is full of dates (that means no blank values), then it's working.

If i have any blank value in my column, then it's not working and giving and error stating "Index was outside the bounds of the array exception" on this line
C#
if (datfile[RowCount].ToString() != "" )
 
Below is my code:

<pre lang="C#">for (int RowCount = startRec; RowCount &lt;= endRec; RowCount++)
            {
                if (datfile[RowCount].ToString() != &quot;&quot; )
                {
                    if (RowCount == 0)
                    {
                        
                        string[] column = datfile[RowCount].Split(&#39;&#254;&#39;);
                        for (int i = 0; i &lt; column.Length - 1; i++)
                        {
                            if (column[i].ToString() != &quot;&quot;) //if (column[i].ToString() != &quot;&quot; &amp;&amp; column[i].ToString() != &quot;\u0014&quot;)
                            {
                                
                                DataGridViewTextBoxColumn dgvtxtcountry = new DataGridViewTextBoxColumn();
                                dgvtxtcountry.HeaderText = column[i].ToString();
                                dgvtxtcountry.Name = column[i].ToString();
                                dataGridView1.Columns.Add(dgvtxtcountry);
                                cmbList.Add(column[i]);
                                i += 1; 
                            }
                        }
                    }

                    if (RowCount != 0)
                    {
                        dataGridView1.Rows.Add();
                        string[] column = datfile[RowCount].Split(&#39;&#254;&#39;);
                        int index = 0;
                        for (int i = 1; i &lt; column.Length - 1; i++)
                        {
                            if (column[i].ToString() != &quot;\u0014&quot;)
                            {
                                    dataGridView1.Rows[rowindex].Cells[index].Value = column[i].Trim(&#39;&#254;&#39;); 
                                 if (StaticVar.DateFormat != &quot;&quot; &amp;&amp; StaticVar.DateFormat != null)
                                {
                                    if (StaticVar.datecolidx == ((i - 1) / 2))
                                    {
                                        dataGridView1.Rows[rowindex].Cells[index].Value = Convert.ToDateTime(column[i]).ToString(StaticVar.DateFormat);
                                    }
                                }
                                
                                index += 1;
                                i += 1;
                            }
                        }
                        BuildDatString(rowindex);
                        rowindex += 1;
                    }
                }
                recNo += 1;</pre>


What I have tried:

I tried changing the code by !stringIsNullorEmpty. It didn't worked.
Posted
Comments
F-ES Sitecore 16-Feb-16 8:44am    
Use the debugger to find out what line the error is happening on, what array you are trying to access, what the index value is (the value inside the square brackets), find out what length the array is...and then work out why you are accessing an item outside of the array's length.
Member 8010354 16-Feb-16 10:49am    
Yeah i did that. In my file, i do have only 3 records where second record is having blank value in Date column. When i debugg, for the first line, the row count is 1. when it comes to second line, it's showing the rowcount as -26. I didn't get what to do. Can you please help me achieving this?
Sergey Alexandrovich Kryukov 16-Feb-16 11:15am    
This is not C# code; probably you just screwed up HTML formatting. Anyway, use the debugger, before addressing by index N, make sure N is between 0 and Count - 1 (or Length - 1, for arrays), inclusively. As simple as that.
—SA
Member 8010354 16-Feb-16 11:51am    
Sorry i didn't get you. Can you please explain once again in brief.
Sergey Alexandrovich Kryukov 16-Feb-16 13:56pm    
Sure, if you tell me which part of my phrase is unclear. I think that it describes 100% of what you need to do, but if you don't see it, you have to explain what is unclear.
—SA

1 solution

C#
for (int RowCount = startRec; RowCount &lt;= endRec-1; RowCount++)
            {
                if (datfile[RowCount].ToString() != &quot;&quot; )



endRec-1; -- try this.
 
Share this answer
 
Comments
Member 8010354 16-Feb-16 8:23am    
If i do this, i'm getting 1 record less in the grid. Like if i have 10 records in the file, i'm getting only 9 in the grid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900