-2

Hey i already asked this but its not solved still, nothing worked , here my complete code:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Insertss
{
class Program
{
    static void Main()
    {
        ReadWrite("projektangebote.txt", "inserts.txt");
    }

    static void ReadWrite(string readName, string writeName)
    {
        StreamReader streamReader;
        StreamWriter streamWriter;
        string str;
        List<string> values = new List<string>();
        streamReader = File.OpenText(readName);
        str = streamReader.ReadLine();
        streamWriter = new StreamWriter(writeName);

        while (str != null)                                         
        {
            values.Add(str);                                   
            str = streamReader.ReadLine();               
        }

        foreach (string a in values)
        {
            int temp = 1;
            String[] temparray = a.Split(';');
            streamWriter.WriteLine("Insert into table Firma values({0},'{1}','{2}')", temp, temparray[1], temparray[4]);
            temp++;
        }
        streamReader.Close();
    }
    }
}

Ok i insert:

PR_Arbeitstitel;PR_Bereich;PR_Firma_Name;PR_Firma_Organisation;PR_Massnahme;PR_Standort;PR_ProjektTeamDaten_ProjektOrt

Überarbeitung der SAV Seite;b.i.b.;;;;PB;

But and the 2nd row ( Überarbeitung der SAV Seite;b.i.b.;;;;PB; ) i get a IndexOutOfRangeException because: Debugged

Anyone knows how i can solve this problem ? Either insert just some char or a space ..

  • 4
    Your string is `JAVA-Apllication;b.i.b;Novabig....` so it has 3 entries see debuger – Kamil Budziewski Dec 17 '13 at 12:17
  • @DGibbs yes i asked it but nothing worked of it, i should have there posted my whole code i guess – user3107760 Dec 17 '13 at 12:17
  • @wudzik yes thats the problem if you read – user3107760 Dec 17 '13 at 12:18
  • 2
    @user3107760 he says that you are showing different string in debugger (which is obviously throws exception), not one you are mentioning in question – Sergey Berezovskiy Dec 17 '13 at 12:18
  • @user3107760 your string presented in debugger does not match string in question – Kamil Budziewski Dec 17 '13 at 12:19
  • @SergeyBerezovskiy not it ignores ;; those where nothing is in there and i get IndexOutOfRangeException because ignoring ;;; those doesnt create my temparray[4] – user3107760 Dec 17 '13 at 12:20
  • 2
    Überarbeitung der SAV Seite;b.i.b.;;;;PB; <- this is not the row that throws the exception. Search for a row with Novabit Informationssysteme GmbH. It is always a good idea to check the array length before accessing an array created with split and dynamic input. – user1567896 Dec 17 '13 at 12:21
  • @wudzik youre right, my file has more than 570rows each of it projekt data, but somehow a few of them dont contain 7elements in this case only 3, i didnt know that , thx for all your help – user3107760 Dec 17 '13 at 12:45

1 Answers1

1

JAVA-Apllication;b.i.b;Novabig....

This is string you are splitting -> see debugger variable a, also you can see that temparray contains parts of string I presented.

it is possible you are reading wrong file (check names and directories). You can check manually which lines are read in debugger and see if it matches expected value.

Split string is working here fine, for string a it should give array of 3, because string a has only two ;'s

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
  • I want to split: Überarbeitung der SAV Seite;b.i.b.;;;;PB; i want it to return an array of 7 elements – user3107760 Dec 17 '13 at 12:31
  • @user3107760 it does not matter what you want to split. It matters only what you are actually splitting – Sergey Berezovskiy Dec 17 '13 at 12:32
  • @user3107760 as I suggested try to inspect which file you are opening and If file contains data you are expecting. Without content of that file I can not suggest any more. For now I see you are splitting wrong string – Kamil Budziewski Dec 17 '13 at 12:32
  • @wudzik oh now i see what you mean, my file looks like this : http://s7.directupload.net/images/131217/62lb4eer.png – user3107760 Dec 17 '13 at 12:36