c# - Selecting a value from one table to insert the id of the row to another one in asp.net -


i'm trying simple insert implementation in project learning , show future employers. know should put in layer , call etc don't have enough time , need learn how easy way anyways.

i have "@username" stored in session["user"], need insert table orders amount , product id, thing product name in products table.

i have product names in drop down list user selects value, types amount , clicks on buy , stores order in database.

what correct way query sql command? thinking sqlcommand trick i'm still not quite sure of how put it.

sample database:

create table orders ( _orderid int not null identity (1,1) primary key, _date datetime null, _quantity bigint null, _prodid int foreign key references products (_productid), _userid int foreign key references users (_userid) ) go   create table products ( _productid int not null identity(1,1) primary key, _prodname nchar (200) null, _stockunits int, _suppid int foreign key references suppliers (_suppid) ) go  create table users ( _userid int not null identity(1,1) primary key, _useremail varchar (35) null, _username varchar (30) not null, _userpass varchar (30) not null, _name varchar (100), _phone varchar (20) null, _address varchar (150) null, _authority int not null, _special bit null ) go   protected void btnbuy_click(object sender, eventargs e)         {            //obviously incomplete.             string usrquery = session["nomusu"].tostring();             sqlconnection oconnection = new sqlconnection("server=.\\sqlexpress;attachdbfilename=l:\\apps\\vs projects\\carnisoftix\\carnidb.mdf;database=carnidb;trusted_connection=yes;");             sqlcommand ocom = new sqlcommand("insert orders _date, _quantity values " + " (" + datetime.now.tostring("yyyy-mm-dd hh:mm:ss") + ", "+ txtamount.text          } 

pd: should make stored procedure simple task?

the key problem in statement of yours:

i have product names in drop down list already

when build dropdown list can add id of products , use value in dropdown such have this:

<select>     <option value="1">apple</option>     <option value="2">banana</option>     <option value="3">orange</option> </select> 

then pass/get value in code-behind, similar how value of amount txtamount. in manner don't have query products table name _productid can insert orders table.


Comments