c# - how to use foreach loop to edit some rows while populating datatable from sql? -



c# - how to use foreach loop to edit some rows while populating datatable from sql? -

i trying populate data table database table. after populated using asp:gridview show data.

the problem trying edit rows after populated database before binding data.

i new c# cant working. code not update anything.

private void bindgridviewfiledata() { string connectionstringname = webconfigurationmanager.connectionstrings["localconnection"].connectionstring; seek { using (sqlconnection sqlconn = new sqlconnection(connectionstringname)) { using (sqlcommand sqlcmd = new sqlcommand()) { sqlcmd.commandtext = "select * visualisation"; sqlcmd.connection = sqlconn; sqlconn.open(); sqldatareader objdatareader = sqlcmd.executereader(); datatable objdatatable = new datatable(); objdatatable.load(objdatareader); foreach (datarow row in objdatatable.rows) { if (row["linenumber"] == "1") { row["batch_no"] = "new value"; } } gvsubjectdetails.datasource = objdatatable; gvsubjectdetails.databind(); sqlconn.close(); } } } grab { } }

the linenumber check needs int check; note doing object check, supports reference equality - never match, if value string of "1", because value database become different string instance interned literal "1" code. so, first check needs be:

if((int)row["linenumber"] == 1)

you inquire in comments how string; in case, quite similarly:

if((string)row["batch_no"] == "some value")

this works because there overloaded == operator string==string, value based equality test, not reference-based equality test.

note: also use:

if(equals(row["batch_no"], "some value"))

but find before version clearer.

c#

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -