c# - No Repeated Primary Key -
protected void btn_insert_click(object sender, eventargs e) { int result = 0; storageprice sp = new storageprice(tb_priceid.text, tb_storagename.text, tb_desc.text, decimal.parse(tb_storeprice.text), int.parse(tb_outletid.text)); result = sp.storagepricinginsert(); if (result > 1) { response.write("<script>alert('storage remove successfullly');</script>"); } else { response.write("<script>alert('storage removal not successfull');</script>"); } response.redirect("storagepricingview.aspx"); }
c# file
public int storagepricinginsert() { int result = 0; string querystr = "insert storagepricing(pricing_id, name, description, pricing, outlet_id)" + "values (@pricing_id, @name, @description, @pricing, @outlet_id)"; try { sqlconnection conn = new sqlconnection(connstr); sqlcommand cmd = new sqlcommand(querystr, conn); cmd.parameters.addwithvalue("@pricing_id", this.pricing_id); cmd.parameters.addwithvalue("@name", this.name); cmd.parameters.addwithvalue("@description", this.description); cmd.parameters.addwithvalue("@pricing", this.pricing); cmd.parameters.addwithvalue("@outlet_id", this.outletid); conn.open(); result += cmd.executenonquery(); // returns no. of rows affected. must >0 conn.close(); return result; } catch (exception ex) { return 0; } }
how insert 1 unique string primary key?
let's put "bt01"
if add , press "bt01"
prompt in use.
thanks lot!
depends. if don't care id value is, let database handle - should automatically assign value depending on how it's configured - use identity column primary key , value assigned you.
if want assign value programmatically, you'll need have method finds highest value integer in database, increment , assign value new record. need place both method , save method singleton make thread safe same value can't added twice.
you @ using guid primary key instead of int.
Comments
Post a Comment