Friday, 15 August 2014

Edit SQL Server Database (INSERT, UPDATE, etc) in ASP.NET (VB) -



Edit SQL Server Database (INSERT, UPDATE, etc) in ASP.NET (VB) -

i learning write asp.net website using visual studio express 2013 web, , plan develop online shopping website. have started new web site, added sql server database (ecommerce.mdf) website (in vs), created 2 tables , inserted row of info using next query:

create table product (product_id char(4) primary key, product_name varchar(50), product_price money, product_stock int); insert product values ('p001', 'omega seamaster planet ocean 600m', 68000, 7); create table cart (customer_id char(4), product_id char(4), cart_quantity int);

then have added new web form product.aspx website , gridview info product table (it shows selectcommand="select * [product]" in source) database. works fine want create button, namely add cart, can insert into function add together new rows cart table. seek utilize next codes:

add onclick event in button html code

<asp:button id="button1" runat="server" text="add cart" onclick="func1" />

add script of func1 event before <html xmlns="http://www.w3.org/1999/xhtml">

<%@ import namespace="system.data.sqlclient" %> <script runat="server"> sub func1() dim cn new sqlconnection("i dont know should type here!") cn.open() dim cmd = new sqlcommand("insert cart values ('c001', 'p001', 1);") cmd.executenonquery() cn.close() end sub </script>

i not sure connectionstring parameter passed sqlconnection() because have tried lot of examples net none of them works me.

from connectionstrings under webconfig file, other connectionstring name of defaultconnection, writes connectionstring="data source=(localdb)\v11.0;attachdbfilename=|datadirectory|\ecommerce.mdf;integrated security=true".

moreover, message an exception of type 'system.data.sqlclient.sqlexception' occurred in system.data.dll not handled in user code when click button in debugging mode if utilize onclick event instead of onclientclick event.

so question is, should type connectionstring parameter , else should modify func1 work expected? appreciate other methods.

thank you.

you can seek this.

dim connectionstring string = configurationmanager.connectionstrings("defaultconnection").connectionstring dim cn new sqlconnection(connectionstring) cn.open() dim cmd = new sqlcommand("insert cart values ('c001', 'p001', 1);", cn) cmd.executenonquery() cn.close()

sql asp.net vb.net

No comments:

Post a Comment