c# - insert data from asp.net page to my database with tables which have foreign key -


i'm beginner asp.net programmer , project online shopping classes have problem in

i have 4 tables foreign key between them...

create table [dbo].[orderdetails]  (     [orderid] int not null,     [classid] int not null,     constraint [pk_orderdetails]        primary key clustered ([orderid] asc, [classid] asc) );  create table [dbo].[order]  (     [orderid]    int identity (300, 1) not null,     [customerid] int not null,      constraint [pk_order]         primary key clustered ([orderid] asc) );  create table [dbo].[customer]  (     [customerid] int identity (200, 1) not null,     [firstname]  nvarchar (50) not null,     [lastname]   nvarchar (50) not null,     [phone]      int           not null,      constraint [pk_table_1]         primary key clustered ([customerid] asc) );  create table [dbo].[class]  (     [classid]    int identity (100, 1) not null,     [numofclass] int not null,     [numofstud]  int not null,     [totalprice] int not null,      constraint [pk_class]         primary key clustered ([classid] asc) );  fk_orderdetails_order fk_order_customer fk_orderdetails_class 

i have 3 pages , in first page pass data page , in second page set data db.

first page code

using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.data; using system.data.sqlclient;    public partial class _default : system.web.ui.page {        static int totalprice = 0;         protected void page_load(object sender, eventargs e)        {            int studprice = convert.toint32(numofstud.selectedvalue) * 6;            int classprice = convert.toint32(numofclass.selectedvalue) * 190;             totalprice = studprice + classprice;            lbltotalprice.text = string.format("{0}", totalprice);        }         protected void numofstud_selectedindexchanged(object sender, eventargs e)        {            int studprice = convert.toint32(numofstud.selectedvalue) * 6;            int classprice = convert.toint32(numofclass.selectedvalue) * 190;             totalprice = studprice + classprice;            lbltotalprice.text = string.format("{0}", totalprice);        }         protected void numofclass_selectedindexchanged(object sender, eventargs e)        {                 int studprice = convert.toint32(numofstud.selectedvalue) * 6;                 int classprice = convert.toint32(numofclass.selectedvalue) * 190;                  totalprice = studprice + classprice;                 lbltotalprice.text = string.format("{0}", totalprice);             }              protected void registerbtn_click(object sender, eventargs e)             {                 session["numofclass"] = numofclass.selecteditem.value;                 session["totalprice"] = totalprice;                 session["numofstud"] = numofstud.selecteditem.value;                  response.redirect("account.aspx");             }           } 

second page code:

using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.data; using system.data.sqlclient;     public partial class account : system.web.ui.page {     protected void page_load(object sender, eventargs e)     {     }      protected void buybtn_click(object sender, eventargs e)     {         sqlconnection con = new sqlconnection(system.configuration.configurationmanager.connectionstrings["miztahrirtest2db"].tostring());          sqlcommand cmd = new sqlcommand("insert customer (firstname, lastname, phone) values (@firstname, @lastname, @phone)", con);         cmd.parameters.addwithvalue("firstname", firstnametxt.text);         cmd.parameters.addwithvalue("lastname", lastnametxt.text);         cmd.parameters.addwithvalue("phone", phonetxt.text);          con.open();         cmd.executenonquery();         con.close();          sqlcommand cmd2 = new sqlcommand("insert class (numofstud, numofclass, totalprice) values (@numofstud, @numofclass, @totalprice)", con);         cmd2.parameters.addwithvalue("numofclass", session["numofclass"]);         cmd2.parameters.addwithvalue("numofstud", session["numofstud"]);         cmd2.parameters.addwithvalue("totalprice", session["totalprice"]);          con.open();         cmd2.executenonquery();         con.close();          sqlcommand cmd3 = new sqlcommand("insert order ....          response.redirect("bank.aspx");     } } 

my problem don't know how insert value tables have foreign key , primary key.cmd1 , cmd2 working correctly can't write set order table , orderdetails table have foreign key table...

when inserting parent table should put scope_identity() end of insert command. , should use execute scalar. scope identity give inserted id execute scalar. after can insert child items parent id.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -