0

Iam trying to get a JSON array into a MSSQL table using linq. I almost got it to work but with the current code, it inserts the first row for the duration of the for at the list part. the table looks like this after the insert. This is my code:

List<insertmultiapiwhitelist> thelist = new List<insertmultiapiwhitelist>();
        string json = "[{'naam':'Default Web Site','webid':1,'verbruik':0,'datum':'2016-12-05 00:00:00'}]";
//json above has 150 items, purged to keep it clean.
        dynamic jsondata = JsonConvert.DeserializeObject(json);

        foreach (var jsonitem in jsondata)
        {
            thelist.Add(new insertmultiapiwhitelist()
            {
                naam = jsondata[0].naam,
                webid = jsondata[1].webid,
                verbruik = jsondata[2].verbruik,
                datum = jsondata[3].datum
            });
        }

        foreach(var listgebruik in thelist)
        {
            webfarm_data dbtoevoegingen = new webfarm_data();
            dbtoevoegingen.naam = listgebruik.naam;
            dbtoevoegingen.webid = listgebruik.webid;
            dbtoevoegingen.verbruik = listgebruik.verbruik;
            dbtoevoegingen.datum = DateTime.Parse(listgebruik.datum);
            DeDB.webfarm_data.Add(dbtoevoegingen);
        }

        var updateantwoord = new insertmultiapiwhitelist { antwoord = "Niets ontvangen" };
        try
        {
            DeDB.SaveChanges();
            updateantwoord = new insertmultiapiwhitelist { antwoord = "Insert voltooid" };
        }
        catch (Exception e)
        {
            var foutmelding = e.ToString();
            updateantwoord = new insertmultiapiwhitelist { antwoord = foutmelding };
        }
        return View(updateantwoord);

2 Answers 2

1

I think you first foreach should look like this:

 foreach (var jsonitem in jsondata)
    {
        thelist.Add(new insertmultiapiwhitelist()
        {
            naam = jsonitem.naam,
            webid = jsonitem.webid,
            verbruik = jsonitem.verbruik,
            datum = jsonitem.datum
        });
    }
Sign up to request clarification or add additional context in comments.

1 Comment

That was it! I choose the wrong definition for the foreach.
0

So if I understand your question correctly you mean that it inserts the first row instead of each individual one? I think your mistake is here:

    foreach (var jsonitem in jsondata)
    {
        thelist.Add(new insertmultiapiwhitelist()
        {
            naam = jsondata[0].naam,
            webid = jsondata[1].webid,
            verbruik = jsondata[2].verbruik,
            datum = jsondata[3].datum
        });
    }

Shouldn't it be this:

    foreach (var jsonitem in jsondata)
    {
        thelist.Add(new insertmultiapiwhitelist()
        {
            naam = jsonitem.naam,
            webid = jsonitem.webid,
            verbruik = jsonitem.verbruik,
            datum = jsonitem.datum
        });
    }

You are basically saying "For each item in my list, insert the 0th element's name and its 1th element webid" which is exactly the same each loop. You should use jsonitem instead of jsondata ;-)

3 Comments

Darn it! 1 second too late after @msokrates answer
Yea I see my mistake now. I"derped" alot looking where It went wrong. I also removed the [] part from the jsonitem.x. Thanks!
Debugging is your best friend for solving these problems ;-) In case you're not familiar with debugging, you should absolutely look it up! It will save you tons of time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.