.net - list not adding all values in C# -


i tried below code create json code ,code working fine.

i load value db,but last value got output .remaining value not added..

datatable dt = new datatable(); var objecttoserialize = new rootobject(); sqlconnection connection = new sqlconnection(configurationmanager.connectionstrings["dbconnection"].tostring()); sqlcommand command = new sqlcommand("select * iflicksmenu", connection); sqldataadapter da = new sqldataadapter(command); da.fill(dt);  foreach (datarow dr in dt.rows) {     foreach (datacolumn col in dt.columns)     {          objecttoserialize.contacts = new list<cont>          {             new cont             {                  sno = dr["cinema"].tostring(),                  name = dr["gallery"].tostring(),                  address =  dr["star"].tostring(),                  gender =  dr["video"].tostring(),                  em = dr["em"].tostring(),                 phone=new phone { mobile=dr["phonone"].tostring() }              },         };               }      javascriptserializer serializer = new javascriptserializer();     response.write(serializer.serialize(new { item = objecttoserialize.contacts })); } 

output is

{     "item":[          {         "phone":{             "mobile":"99528748474"                 },         "sno":"actress",         "name":"actress",         "em":"scd",         "address":"actress",         "gender":"trailer"         }     ] } 

you recreate contacts each item when want create in on beginning of loop , keep adding list:

var items = new list<cont>(); foreach (datarow dr in dt.rows) {      items.add(new cont{          sno = dr["cinema"].tostring(),          name = dr["gallery"].tostring(),          address =  dr["star"].tostring(),          gender =  dr["video"].tostring(),          em = dr["em"].tostring(),         phone = new phone{ mobile=dr["phonone"].tostring() }     }); } objecttoserialize.contacts = items; 

also need remove internal loop

foreach (datacolumn col in dt.columns) 

as multiply number of rows number of columns


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -