---
title: "SQL Server Database Crud operation in ASP.NET (VB)"  
description: "SQL Server Database Crud operation in ASP.NET (VB)"  
author: "Anonymous User"  
published: 2014-11-02  
updated: 2014-11-03  
canonical: https://www.mindstick.com/forum/2491/sql-server-database-crud-operation-in-asp-dot-net-vb  
category: "asp.net"  
tags: ["vb.net", "sql server", "sql"]  
reading_time: 2 minutes  

---

# SQL Server Database Crud operation in ASP.NET (VB)

I am currently learning to write ASP.NET website using [Visual Studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) Express 2013 for Web, and I plan to develop an [online shopping](https://www.mindstick.com/articles/310828/the-online-shopping-statistics-you-need-to-know) website. I have started a New [Web Site](https://www.mindstick.com/articles/12285/the-finest-web-site-design-trends-you-may-expect-in-2017), added a SQL [Server Database](https://www.mindstick.com/forum/156824/what-is-normalization-in-sql-server-database) (eCommerce.mdf) into the WebSite (in VS), created [two tables](https://www.mindstick.com/forum/159646/how-to-join-two-tables-in-oracle-to-get-single-line-results) and inserted a row of data using the following query:

\

```
CREATE TABLE product (product_id char(4) PRIMARY KEY, product_name varchar(50), product_price money, product_stock int);INSERT INTO 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 I have added a new web form Product.aspx into the website and a GridView to get data from the product table (it shows SelectCommand="SELECT * FROM [product]" in the source) from my database. It [works fine](https://answers.mindstick.com/qa/115732/my-software-works-fine-on-windows-10-but-crashes-on-windows-11-why) but I want to make a Button, namely Add to cart, that can do the INSERT INTO function to add new rows to the cart table. I try to use the following codes:

\
Add an OnClick event in the Button html code

```
 <asp:Button ID="Button1" runat="server" Text="Add to cart" OnClick="func1" />
```

Add the 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 As New SqlConnection("I dont know what I should type here!")    cn.Open()    Dim cmd = New SqlCommand("INSERT INTO cart VALUES ('C001', 'P001', 1);")    cmd.ExecuteNonQuery()    cn.Close()End Sub</script>
```

I am not sure about the ConnectionString parameter to be passed to [SqlConnection](https://www.mindstick.com/forum/1209/sqlconnection-sqlcommand-sqldatareader-idisposable)() because I have tried a lot of examples from the Internet but none of them [works for me](https://answers.mindstick.com/qa/110735/how-do-i-create-a-study-schedule-that-works-for-me).

From connectionStrings under the Webconfig file, other than the ConnectionString with name of DefaultConnection, it writes connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\eCommerce.mdf;[Integrated](https://www.mindstick.com/forum/33532/handler-pagehandlerfactory-integrated-has-a-bad-module-managedpipelinehandler-in-its-module-list) Security=True".

Moreover, I get the message An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code when I click the Button in debugging mode if I use OnClick event instead of OnClientClick event.

So my question is, what should I type as the ConnectionString parameter and anything else should I modify to get func1 work as expected? I also appreciate any other methods.

## Replies

### Reply by Anonymous User

You can try like this.\

```
Dim connectionString As String = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionStringDim cn As New SqlConnection(connectionString)cn.Open()Dim cmd = New SqlCommand("INSERT INTO cart VALUES ('C001', 'P001', 1);", cn)cmd.ExecuteNonQuery()cn.Close()
```


---

Original Source: https://www.mindstick.com/forum/2491/sql-server-database-crud-operation-in-asp-dot-net-vb

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
