Friday, 15 July 2011

ado.net - how to use row index as dataset index using c#.net -



ado.net - how to use row index as dataset index using c#.net -

i newbie c# working on project trying loop through info table containing distinct values , database has song id like:1,2,3,4,6,8,9,10 dataset takes values 0,1,2,3,4,5,6,7 respectively...

string sql = "select title, song_id up_song song_type='mp3 tracks' "; adpt = new sqldataadapter(sql, cn); ds = new dataset(); adpt.fill(ds, "title"); var maxvalue = ds.tables["title"].asenumerable().max(x => x.field<int>("song_id")); var minvalue = ds.tables["title"].asenumerable().min(x => x.field<int>("song_id")); (i =maxvalue; >= minvalue; --i) { seek { hyperlink[i] = new hyperlink(); hyperlink[i].id = "hyperlink" + i; hyperlink[i].text = ds.tables["title"].rows[i].itemarray[0].tostring(); hyperlink[i].navigateurl = "downloadpage.aspx"; hyperlink[i].forecolor = system.drawing.color.white; panel1.controls.add(hyperlink[i]); panel1.controls.add(new literalcontrol("<br>")); httpcookie coo = new httpcookie("song"); coo["sogtit"] = ds.tables["title"].rows[i].itemarray[0].tostring(); response.cookies.add(coo); } grab (exception ex) { response.write(ex.message); } }

you using loop variable acess rows in datatable here:

coo["sogtit"] = ds.tables["title"].rows[i].itemarray[0].tostring();

but variable initialized min , max id values of song_id.

i don't know why need these values @ all, why don't loop datarows:

foreach(datarow row in ds.tables["title"].rows) { // ... int songid = row.field<int>("song_id") hyperlink hl = new hyperlink(); // don't need array of hyperlinks neither hl.id = "hyperlink" + songid; string title = row.field<string>("title); hl.text = title; coo["sogtit"] = title; panel1.controls.add(hl); // ... }

update

i want access latest upload song utilize loop , index min , max values. mean want access latest uploaded minimum 6 song

you utilize linq lastly 6 uploaded songs:

var last6uploaded = ds.tables["title"].asenumerable() .orderbydescending(r => r.field<int>("song_id")) .take(6); foreach(datarow row in last6uploaded) { // ... }

note should utilize datetime field instead of primary-key.

c# ado.net

No comments:

Post a Comment