c# - StringBuilder appending first row of a datatable repeatedly -


i trying build html table datatable contains 24 months of customer's payment history. however, first row of data returned (most recent payment) , repeated 24 times. select statement returns 24 distinct rows when run in query builder. foreach loop writing on itself? don't think overloading stringbuilder 24 rows. tried adding appendline(); doesn't change anything.

        //get account history          string strconnstring7 = webconfigurationmanager.connectionstrings["billing_webconnectionstring"].connectionstring;         sqlconnection con7 = new sqlconnection(strconnstring7);         sqlcommand cmd7 = new sqlcommand("select * dbo.web_history acct_nbr ='" + acctnbr + "'order billing_date desc", con7);         cmd7.parameters.add("conn_nbr", sqldbtype.varchar).value = session["acctnum"];         cmd7.connection = con7;         sqldataadapter da7 = new sqldataadapter(cmd7);         datatable dthistory = new datatable();         da7.fill(dthistory);           if (dthistory != null && dthistory.rows.count > 0)         {             string billing_date = string.format("{0:d}", dthistory.rows[0]["billing_date"]);             string w_cons = dthistory.rows[0]["w_cons"].tostring();             string w_curr_chrg = string.format("{0:0.00}", dthistory.rows[0]["w_curr_chrg"]);             string s_curr_chrg = string.format("{0:0.00}", dthistory.rows[0]["s_curr_chrg"]);             string adjs = string.format("{0:0.00}", dthistory.rows[0]["adjs"]);             string pay = string.format("{0:0.00}", dthistory.rows[0]["pay"]);             string return_pay = string.format("{0:0.00}", dthistory.rows[0]["return_pay"]);              system.text.stringbuilder sb = new system.text.stringbuilder();              int counter = 0;             foreach (datarow row in dthistory.rows)                {                  sb.append("<tr><td>" + billing_date + "</td><td>" + w_cons + "</td><td>" + w_curr_chrg + "</td><td>" + s_curr_chrg + "</td><td>" + adjs + "</td><td>" + pay + "</td><td>" + return_pay + "</td></tr>");                  }               history.text = sb.tostring();             //counter.text = counter.tostring();             con7.close();         } 

you iterate on every row, set billing_date, w_cons, w_curr_chrg, s_curr_chrg, adjs, pay, , return_pay once:

        string billing_date = string.format("{0:d}", dthistory.rows[0]["billing_date"]);         string w_cons = dthistory.rows[0]["w_cons"].tostring();         string w_curr_chrg = string.format("{0:0.00}", dthistory.rows[0]["w_curr_chrg"]);         string s_curr_chrg = string.format("{0:0.00}", dthistory.rows[0]["s_curr_chrg"]);         string adjs = string.format("{0:0.00}", dthistory.rows[0]["adjs"]);         string pay = string.format("{0:0.00}", dthistory.rows[0]["pay"]);         string return_pay = string.format("{0:0.00}", dthistory.rows[0]["return_pay"]); 

when loop through rows, you're appending them every time, you're not updating them -- value never changes set to.

        foreach (datarow row in dthistory.rows)            {             sb.append("<tr><td>" + billing_date + "</td><td>" + w_cons + "</td><td>" + w_curr_chrg + "</td><td>" + s_curr_chrg + "</td><td>" + adjs + "</td><td>" + pay + "</td><td>" + return_pay + "</td></tr>");         } 

you need update values in loop.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -