How to assing values to 2 dimensional array with data from database in asp.net using c#? -
suppose sql database studentinfo , table name registration
id----------name---------------email---------------------------phoneno 1 munasunghe amilamunasinghe@yahoo.com 0717069425 2 liyanarachchi hareshliya6@gmail.com 0756706352
protected void page_load(object sender, eventargs e) { string query = "select id, name, email, phoneno registration"; sqlcommand cmd1 = new sqlcommand(query); datatable dt1 = getdata(cmd1); int rowcount = dt1.rows.count; /* want assing dt1 datatable data 2 dimensional array*/ }
the function getdata used data database.
private datatable getdata(sqlcommand cmd) { datatable dt = new datatable(); string strconnstring = system.configuration.configurationmanager.connectionstrings["constring"].connectionstring; sqlconnection con = new sqlconnection(strconnstring); sqldataadapter sda = new sqldataadapter(); cmd.commandtype = commandtype.text; cmd.connection = con; try { con.open(); sda.selectcommand = cmd; sda.fill(dt); return dt; } catch { return null; } { con.close(); sda.dispose(); con.dispose(); } }
please give example code me.
object[,] target = new object[dt1.rows.count, dt1.columns.count]; int rowcount = 0; foreach(datarow dr in dt1.rows) foreach(datacolumn dc in dt1.columns) { target[rowcount, dc.ordinal] = dr[dc]; rowcount++; }
Comments
Post a Comment