c# - How to get values from Class -
i building c# wpf app. app contains listview multiple column. added column , binding in listview xaml
<listview x:name="receiptlist" horizontalalignment="left" height="209.987" verticalalignment="top" width="645" margin="111,135.748,0,0" isenabled="false" > <listview.view> <gridview> <gridviewcolumn header="receiptid" width="60" displaymemberbinding="{binding receiptid}"/> <gridviewcolumn header="itemno" width="50" displaymemberbinding="{binding itemno}"/> <gridviewcolumn header="itemname" width="240" displaymemberbinding="{binding itemname}"/> <gridviewcolumn header="packingsize" width="100" displaymemberbinding="{binding packingsize}"/> <gridviewcolumn header="quantity" width="50" displaymemberbinding="{binding quantity}"/> <gridviewcolumn header="price" width="50" displaymemberbinding="{binding price}"/> <gridviewcolumn header="expirydate" width="100" displaymemberbinding="{binding expirydate}"/> </gridview> </listview.view> </listview>
now of button add rows in listview:
private void additems_click(object sender, routedeventargs e) { additems additems = new additems(hhk,2,"abc","36x63",3,opop,"22-11-2014"); receiptlist.items.add(additems); }
additems class:
class additems { public int receiptid { get; set; } public int itemno { get; set; } public string itemname { get; set; } public string packingsize { get; set; } public int quantity { get; set; } public float price { get; set; } public string expirydate { get; set; } public additems() { } public additems(int oid, int itno, string itname, string itsize, int qua, float pri, string edate) { this.receiptid = oid; this.itemno = itno; this.itemname = itname; this.packingsize = itsize; this.quantity = qua; this.price = pri; this.expirydate = edate; } }
now want add method counts how many items added in additems , want retrieve items additems of loop.
this not idea. i'll explain why:
- user starts program , gets new order id based on current maximum order id in database
- user b starts program , gets same order id nothing has been updated far.
- both user , b save orders - , 2 different orders same id.
please, , users favour correctly start: either have database assign order id upon insert or create number-series table updates new order id whenever order id queried.
example:
table numberseries contain following fields:
seriesname minid maxid currentvalue orders 1 999999 817
the following stored procedure next possible order id safely!
create procedure spgetnextno @seriesname begin declare @newno table (value int) update numberseries set currentvalue = currentvalue + 1 output inserted.currentvalue @newno seriesname = @seriesname , currentvalue < maxid select top 1 value @newno end
you can use c# follows:
using (sqlcommand cmd = new sqlcommand("spgetnextno", conn)) { cmd.commandtype = commandtype.storedprocedure; cmd.parameters.addwithvalue("@seriesname", "orders"); object res = cmd.executescalar(); if (res == null || res == dbnull.value) throw new invalidoperationexception("number series used up"); return (int)res; }
edit checked out habo's approach comments , following works, too:
create procedure spgetnextno @seriesname begin declare @newno int update numberseries set @newno = currentvalue += 1 seriesname = @seriesname , currentvalue < maxid select @newno end
edit asked in comments whether possible without using stored procedures. have not tried c#, i've entered following using test table in sql management studio , worked.
exec('declare @var int; update ttest set @var = test += 1; select @var;')
this yields correct results in case. try execute above statement using sqlcommand
this:
using (sqlcommand cmd = new sqlcommand("exec('declare @var int; update ttest set @var = test += 1; select @var;')", conn) { object o = cmd.executescalar(); ... }
of course use number series table suggested above , adjust query needed.
that worth trying. otoh, why don't want use stored procedures?
Comments
Post a Comment