Tuesday, February 18, 2020

How to Import data from Database to DataGridView in Visual Studio \\ 2020

How to Import data from Database

 to DataGridView in Visual Studio 

--------------------------------------------------------------------------------------------------------------------------
The DataGridView control and its related classes are designed to be a flexible, extensible system for displaying and editing tabular data. You can use a DataGridView control to display data with or without an underlying data source.
The following vb.net source code shows how to Import data from an Excel file to a DataGridView control .

You can use WinForms DataGridView control to perform simple CRUD operations. Your data source can be anything like an SQL Server database, JSON or Ms-Excel. Here in this post I am sharing a small program in C# and Vb.Net showing how to import data from an Excel to a DataGridView control and modify the existing data or add new data back to the Excel sheet.


Source code sample

Imports System.Data.OleDb
--------------------------------------------------------------------------------------------------------------------------
Public Class Form1
    Dim con As New OleDbConnection
    Dim constring As String
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
--------------------------------------------------------------------------------------------------------------------------

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            constring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/dat.accdb"
            With con

                .ConnectionString = constring
                .Open()

            End With

            Me.Label4.Text = "Connection State : Active!"
            Me.Label4.ForeColor = Color.DarkGreen

            FillStates()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        
    End Sub
--------------------------------------------------------------------------------------------------------------------------
    Public Sub FillStates()
        cmd = New OleDbCommand

        With cmd
            .Connection = con
            .CommandType = CommandType.Text
            .CommandText = "SELECT DISTINCT tbldat.State from tbldat"
        End With

        dr = cmd.ExecuteReader()
        While dr.Read()
            cbostates.Items.Add(dr("State"))
        End While

        cmd.Dispose()

    End Sub
--------------------------------------------------------------------------------------------------------------------------
    Public Sub FillCities()
        Dim cmd1 As New OleDbCommand
        Dim dr1 As OleDbDataReader

        With cmd1
            .Connection = con
            .CommandType = CommandType.Text
            .CommandText = "SELECT * from tbldat where [State] ='" & Me.cbostates.Text & "'"
        End With

        dr1 = cmd1.ExecuteReader()

        While dr1.Read()
            cbocities.Items.Add(dr1("City"))
        End While
    End Sub
--------------------------------------------------------------------------------------------------------------------------
    Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
        con.Close()
        Dispose()

    End Sub
--------------------------------------------------------------------------------------------------------------------------
    Private Sub cbostates_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbostates.SelectedIndexChanged
        cbocities.Text = "-Select-"
        If Me.cbocities.SelectedIndex >= -1 Then
            Me.cbocities.Items.Clear()
            FillCities()
        Else
            FillCities()

        End If
        
    End Sub
--------------------------------------------------------------------------------------------------------------------------
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim Sql As String = "SELECT * FROM tbldat"

        Dim da As OleDbDataAdapter = New OleDbDataAdapter(Sql, con)

        Dim ds As New DataSet

        da.Fill(ds)

        DataGridView1.DataSource = ds.Tables(0)

    End Sub
--------------------------------------------------------------------------------------------------------------------------
End Class
--------------------------------------------------------------------------------------------------------------------------

Form Design
--------------------------------------------------------------------------------------------------------------------------

How to Import data from Database to DataGridView in VB.Net
--------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------
#How to export to Excel file in C# and VB.NET, #How to export DataTable to Excel file in .NET, #How to export GridView to Excel file in ASP.NET, #How to export DataGridView to Excel file in .NET, #How to export DataGrid to Excel file in .NET, #How to import Excel file in C# and VB.NET, #How to import Excel file to SQL table in .NET, #How to import Excel file to DataTable in .NET, #How to import Excel file to GridView in .NET, #How to import Excel file to DataGridView in .NET, #How to import Excel file to DataGrid in .NET, #How to read Excel file in C# and VB.NET, #How to export to Excel file in PHP and ASP classic, #How to import Excel file in PHP and ASP classic, #How to import Excel data to MySQL, #SQL Server in PHP or ASP classic

0 comments: