Import csv into a gridview using asp.net -
in asp.net app there 2 options import csv file gridview.
one streamreader
this:
string rowvalue; string[] cellvalue; system.io.streamreader streamreader = new streamreader(txtpath.text); // reading header rowvalue = streamreader.readline(); cellvalue = rowvalue.split(','); (int = 0; <= cellvalue.count() - 1; i++) { datagridviewtextboxcolumn column = new datagridviewtextboxcolumn(); column.name = cellvalue[i]; column.headertext = cellvalue[i]; datagridview1.columns.add(column); } // reading content while (streamreader.peek() != -1) { rowvalue = streamreader.readline(); cellvalue = rowvalue.split(','); datagridview1.rows.add(cellvalue); } streamreader.close();
the other using oledb:
string cmdstring = string.format("select * {0}", system.io.path.getfilename(target + "\\" + fileupload1.filename)); oledbdataadapter dataadapter = new oledbdataadapter(cmdstring, connstring); dataset dataset = new dataset(); dataadapter.fill(dataset); gridview1.datasource = dataset.tables[0]; gridview1.databind();
what's difference between these two? there advantage using 1 on other?
using streamreader :-
for example, data in csv file this.
copy below data in notepad , save "test.csv" , try
id,name,address 1,user1,india 2,user2,"chennai,india"
in first row string array {1,user1,india}
but, problem second {2,user2,"chennai,india"}
using oledb:-
i feel, best use oledb. because, no need worry string manipulation. , access data using sql conditions.
Comments
Post a Comment