Insert data into SQL Server Database in Windows Application in VB.Net

Leave a Comment
In this article, we will study how to insert Windows Forms Data into SQL Server Database in VB.Net.

In this tutorial, we have created Database called "StudentDemo" and add one table called "Student" as show below -


Now, Create Windows Form Application and create Form and add Windows Form called insertStudent.vb (To create Windows Forms Application Go to File -> New -> Windows Form Applications )


Now, create Form and add Windows Form called insertStudent.vb and add three label, three textbox and one button as shown below using toolbox.


Now, Double click the save button and create Save button click event and add below code -

Private Sub btn_save_Click(sender As System.Object, e As System.EventArgs) Handles btn_save.Click
        con.Open()
        Dim Name1 As String = txt_name.Text.ToString()
        Dim City1 As String = txt_city.Text.ToString()
        Dim Code1 As String = txt_code.Text.ToString()
        Dim CommandSQL As SqlCommand = New SqlCommand("INSERT INTO Student (Name ,City ,Code) VALUES (@Name ,@City ,@Code)")
        CommandSQL.Parameters.AddWithValue("@Name", Name1)
        CommandSQL.Parameters.AddWithValue("@City", City1)
        CommandSQL.Parameters.AddWithValue("@Code", Code1)
        CommandSQL.Connection = con
        CommandSQL.ExecuteNonQuery()
        con.Close()
        MessageBox.Show("Value Inserted Successfully")
        txt_name.Text = ""
        txt_city.Text = ""
        txt_code.Text = ""
    End Sub

In above code, we create SqlConnection object con and pass ConnectionString as shown below to connect the Database.
    Dim con As SqlConnection = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Developer\Desktop\Kisan\WindowsApplication1\WindowsApplication1\StudentDemo.mdf;Integrated Security=True;User Instance=True")

So, the complete code as shown below -

Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Sql
Public Class insertStudent
    Dim con As SqlConnection = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Developer\Desktop\Kisan\WindowsApplication1\WindowsApplication1\StudentDemo.mdf;Integrated Security=True;User Instance=True")
    Private Sub btn_save_Click(sender As System.Object, e As System.EventArgs) Handles btn_save.Click
        con.Open()
        Dim Name1 As String = txt_name.Text.ToString()
        Dim City1 As String = txt_city.Text.ToString()
        Dim Code1 As String = txt_code.Text.ToString()
        Dim CommandSQL As SqlCommand = New SqlCommand("INSERT INTO Student (Name ,City ,Code) VALUES (@Name ,@City ,@Code)")
        CommandSQL.Parameters.AddWithValue("@Name", Name1)
        CommandSQL.Parameters.AddWithValue("@City", City1)
        CommandSQL.Parameters.AddWithValue("@Code", Code1)
        CommandSQL.Connection = con
        CommandSQL.ExecuteNonQuery()
        con.Close()
        MessageBox.Show("Value Inserted Successfully")
        txt_name.Text = ""
        txt_city.Text = ""
        txt_code.Text = ""
    End Sub
    End Class

It's Done

Download Complete Source Code



0 comments:

Post a Comment