0

I try to return user informations json value at my web page' load method. My code looks like this.

DataSet dsX = ReadSql("select top 14 * from fuyeler where inStatus = 1 and inUye = 1");
    if (dsX.Tables.Count > 0)
    {
        OYUNCU oyuncular = new OYUNCU();

        for (int i = 0; i < dsX.Tables[0].Rows.Count; i++)
        {
            oyuncular.oyuncuID = Convert.ToInt32(dsX.Tables[0].Rows[i]["inID"]);
            oyuncular.oyuncuMail = dsX.Tables[0].Rows[i]["stMail"].ToString();
            oyuncular.oyuncuPass = dsX.Tables[0].Rows[i]["stPass"].ToString();
            oyuncular.oyuncuToken = dsX.Tables[0].Rows[i]["stToken"].ToString();
        }                

        string json = JsonConvert.SerializeObject(oyuncular);


        Response.Clear();
        Response.ContentType = "application/json; charset=utf-8";
        Response.Write(json);
        Response.End();
    }`

At the end it returns only 1 value of course. I have tried Arrays and lists but JsonConvert.SerializeObject(array or list) doesnt return as json value. It returns every values on 1 row. I want 1 row for 1 value. What should i do about it?

For this code below Json result looks like this,

{"oyuncuMail":"x@gmail.com","oyuncuPass":"x2008","oyuncuToken":"290620153513","oyuncuID":14}

mext
  • 31
  • 6

1 Answers1

1

This output is correct and should dispaly last record of the selected rows from your sql query because in your for loop you don't create new object, you assign values to the same object so it will take the values of the last row you should do this if you want to get a list :

    List<OYUNCU> listOyuncular = new List<OYUNCU>();

        for (int i = 0; i < dsX.Tables[0].Rows.Count; i++)
        {
            var oyunclar = new OYUNCU();
            oyuncular.oyuncuID = Convert.ToInt32(dsX.Tables[0].Rows[i]["inID"]);
            oyuncular.oyuncuMail = dsX.Tables[0].Rows[i]["stMail"].ToString();
            oyuncular.oyuncuPass = dsX.Tables[0].Rows[i]["stPass"].ToString();
            oyuncular.oyuncuToken = dsX.Tables[0].Rows[i]["stToken"].ToString();
            listOyuncular.Add(oyunclar);
        }  

  string json = JsonConvert.SerializeObject(listOyuncular);
lyz
  • 548
  • 6
  • 16