-5

System.IndexOutOfRangeException: Index was outside the bounds of the array. At WinServiceProject.WinService.OnStart(String[] args) in C:\Documents and Settings\Administrator\Desktop\WinServiceProject\WinService.cs:line 515

{ string[] stringArray = row["Version"].ToString().Split('.');

        Line 515 -----> switch (stringArray[0] + "." + stringArray[1] + ".")
                        {
                            case "11.0.":
                                sqlServerVersion = "SQL Server 2012";
                                file.WriteLine("SQL Server 2012");
                                break;

                            case "10.50.":
                                sqlServerVersion = "SQL Server 2008 R2";
                                file.WriteLine("SQL Server 2008 R2");
                                break;

                            case "10.0.":
                                sqlServerVersion = "SQL Server 2008";
                                file.WriteLine("SQL Server 2008");
                                break;

                            case "9.00.":
                                sqlServerVersion = "SQL Server 2005";
                                file.WriteLine("SQL Server 2005");
                                break;
                        }
                    }`

If Someone can help me please... thanks.

  • What is return `row["Version"].ToString()`? – Hamlet Hakobyan Apr 08 '14 at 14:48
  • return the SQL Server Internal Version... its that the reason that I made the cases cuz this show something like 11.0.2100.60. – Manuel Fdz Apr 08 '14 at 14:58
  • you are so funny... this is a Windows Service... try to debug... the only way to debug its with NotePad... If people only will post to say any thing that dont help me... please dont post – Manuel Fdz Apr 08 '14 at 15:09
  • 1
    @ManuelFdz You can use your debugger on a windows service you created. It's just a slightly different process than the normal F5 debugging. What on earth are you talking about? – tnw Apr 08 '14 at 15:35

1 Answers1

0

Your stringArray contains less than two elements.That is your problem, you need to make sure it contains at least two elements before switch statement.BTW, if you just want to append a dot to the end, you don't need String.Split, just use Insert method:

string str  = row["Version"].ToString();
str = str.Insert(str.Length, ".");

switch(str)
{
   ...
}

Or simply use string concatenation: string str = row["Version"].ToString() + ".";

Selman Genç
  • 100,147
  • 13
  • 119
  • 184