In this article I will explain with an example, how to Insert data into Database in ASP.Net using C# and VB.Net.
ADO.Net will be used to perform Insert operation in ASP.Net.
Database
I have made use of the following table Customers with the schema as follows.
Note: You can download the database table SQL by clicking the download link below.
HTML Markup
The HTML Markup consists of:
HTML Button – For opening the Bootstrap Modal Popup.
TextBox – For inputting Name.
DropDownList – For selecting Country.
Button – For submitting the Form.
The Button has been assigned with OnClick event handler.
Inside the HTML Form the following Bootstrap CSS is inherited.
1. bootstrap.min.css
1. jquery-3.2.1.slim.min.js
2. bootstrap.min.js
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">Insert</button>
<div id="exampleModal" class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog"role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Customer Details Form</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>Name:</label>
<asp:TextBox runat="server" ID="txtName" CssClass="form-control" />
</div>
<div class="form-group">
<label>Country:</label>
<asp:DropDownList runat="server" ID="ddlCountries" CssClass="form-control">
<asp:ListItem Text="Please select" Value="" />
<asp:ListItem Text="India" Value="India" />
<asp:ListItem Text="China" Value="China" />
<asp:ListItem Text="Australia" Value="Australia" />
<asp:ListItem Text="France" Value="France" />
<asp:ListItem Text="Unites States" Value="Unites States" />
<asp:ListItem Text="Russia" Value="Russia" />
<asp:ListItem Text="Canada" Value="Canada" />
</asp:DropDownList>
</div>
<div class="modal-footer">
<asp:Button Text="Save changes" runat="server" CssClass="btn btn-primary" OnClick="OnInsert" />
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
Namespaces
You will need to import the following namespaces.
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Inserting record in Database in ASP.Net
When the Save changes Button is clicked, following event handler is executed.
Inside this event handler, the name value is fetched from the TextBox, while and the country value is fetched from the DropDownList and are inserted into the SQL Server database table using ADO.Net.
Finally, the CustomerId of the inserted record is fetched and displayed in JavaScript Alert Message Box using RegisterStartupScript method.
C#
protected void OnInsert(object sender, EventArgs e)
{
int customerId;
string query = "INSERT INTO Customers(Name, Country) VALUES(@Name, @Country)";
query += " SELECT SCOPE_IDENTITY()";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Country", ddlCountries.SelectedItem.Text);
cmd.Connection = con;
con.Open();
customerId = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Inserted Customer ID: " + customerId + "');", true);
}
VB.Net
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Dim customerId As Integer
Dim query As String = "INSERT INTO Customers(Name, Country) VALUES(@Name, @Country)"
query += " SELECT SCOPE_IDENTITY()"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Country", ddlCountries.SelectedItem.Text)
cmd.Connection = con
con.Open()
customerId = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
End Using
End Using
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Inserted Customer ID: " & customerId & "');", True)
End Sub
Screenshot
The Form
Record after Insert in database
Downloads