How to display/ fire data (sum column values) from SQL Server database in C# WinForm textBoxes -
i'm trying display sum columns sql server database winforms textboxes i'm facing difficulties , don't know further.
this how table in database looks like:

my c# code:
if (combobox1.selectedtext != null) { string cs = (@"data ) sqlconnection con = new sqlconnection(cs); sqlcommand sqlcmd = new sqlcommand(); sqlcmd.connection = con; sqlcmd.commandtext = ("[dbo].[spsalesanalyse]"); sqlcmd.commandtype = commandtype.storedprocedure; cbanalyse.selectedvalue.tostring()); sqldatareader myreader; try { con.open(); sqldataadapter adapter = new sqldataadapter(sqlcmd); // save results in dt. call adapter statement , fill in dt datatable dt = new datatable(); adapter.fill(dt); //fill textboxes data in dt textbox1.text = dt.rows[0].field<string>("north"); textbox2.text = dt.rows[1].field<string>("east"); textbox3.text = dt.rows[2].field<string>("west"); .... } catch(exception ex) { messagebox.show(ex.message); } { con.close(); } } else messagebox.show("error"); my expectation:

the problem: if debugg till line adapter.fill(dt), see records but not getting textboxes. empty when debugg reach textboxes level
hope did express myself well
looks trying 1 textbox value @ time. use group by clause in sql fetch data.
select country, region, sum([sales1]) [first sales], sum([sales2]) [sec sales], sum([sales3]) [third sales] [dbo].[tblsales] group country, region a single query return results. can take results in dataset , set values of corresponding textboxes fetching values dataset.
//use query string show above sqldataadapter adapter = new sqldataadapter(querystring, connection); dataset dataset = new dataset(); adapter.fill(dataset); now have records inside dataset, can fill textboxes.
datarow datarow = dataset.tables[0].select("country = 'italy' , region = 'north'").firstordefault(); if (datarow != null) { textbox1.text = datarow["first sales"].tostring(); }
Comments
Post a Comment