In this article I will explain with an example, how to insert ComboBox value into
SQL Server database in Windows Forms (WinForms) Application using C# and VB.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.
Form Design
The following Form consists of:
Label – For labelling controls.
TextBox – For capturing Name to be inserted.
ComboBox – For capturing Country name to be inserted.
Adding ConnectionString to the App.Config file
You need to add the Connection String in the ConnectionStrings section of the App.Config file in the following way.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="ConString" connectionString="Data Source=Mudassar-PC\SQL2019;Initial Catalog=AjaxSamples;Integrated Security=true" />
</connectionStrings>
</configuration>
Namespace
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 into the Database in Windows Forms
Inside the Form Load event handler, the ComboBox items are added.
When
Insert button is clicked, the
Name value is fetched from the TextBox, while the
Country value is fetched from the ComboBox are inserted into the
SQL Server database table using
ADO.Net.
Finally, the TextBox and ComboBox value is set to empty and CustomerId of the inserted record is fetched and displayed in MessageBox.
C#
private void Form1_Load(object sender, EventArgs e)
{
cbCountries.Items.Add("Please Select");
cbCountries.Items.Add("United States");
cbCountries.Items.Add("India");
cbCountries.Items.Add("France");
cbCountries.Items.Add("Russia");
}
private void OnInsert(object sender, EventArgs e)
{
int customerId;
string query = "INSERT INTO Customers (Name, Country) VALUES(@Name, @Country)";
query += " SELECT SCOPE_IDENTITY()";
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Country", cbCountries.SelectedItem);
cmd.Connection = con;
con.Open();
customerId = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
txtName.Text = string.Empty;
cbCountries.Text = string.Empty;
MessageBox.Show("Inserted Customer ID: " + customerId);
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cbCountries.Items.Add("Please Select")
cbCountries.Items.Add("United States")
cbCountries.Items.Add("India")
cbCountries.Items.Add("France")
cbCountries.Items.Add("Russia")
End Sub
Private Sub OnInsert(ByVal sender As Object, ByVal e As EventArgs) Handles btnInsert.Click
Dim customerId As Integer
Dim query As String = "INSERT INTO Customers (Name, Country) VALUES(@Name, @Country)"
query += " SELECT SCOPE_IDENTITY()"
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Country", cbCountries.SelectedItem)
cmd.Connection = con
con.Open()
customerId = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
End Using
End Using
txtName.Text = String.Empty
cbCountries.Text = String.Empty
MessageBox.Show("Inserted Customer ID: " & customerId)
End Sub
Screenshots
Record after Insert in database
Downloads