Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to Populate values in Data Grid and Combo Box from database on Run time depend upon Given Text .NET 2.0.

Author: DevASP
Download Source Code : 594_values_on_runtime.zip

In this simple article you will learn that how you can populate values in Data Grid and Combo Box from database on Run time depend upon Given Text

Simply just create New Project of console application in VB.

 

Drag one text box, one Combo box and a Data Grid on form.

 

In this sample I am using MS Access database and getting values from access on run time. This is a good example for beginners.

 

Import this library

 

 

Imports ss = System.Data.SqlClient

 Now declare global variables 

  Dim cn As New System.Data.OleDb.OleDbConnection

  Dim cm As New System.Data.OleDb.OleDbCommand

  Dim rd As System.Data.OleDb.OleDbDataReader

 

Now just write code against these events

 

 

Private Sub txtbyName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbyName.TextChanged

        cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=purchases.mdb"

        Try

            cn.Open()

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

        cmbbyName.Items.Clear()

        cm.Connection = cn

        cm.CommandText = "select * from pDetails where Name like '" & txtbyName.Text & "%'"

        rd = cm.ExecuteReader()

 

        cmbbyName.Items.Clear()

        While (rd.Read)

            cmbbyName.Items.Add(rd("Name"))

        End While

        rd.Close()

        If cmbbyName.Items.Count > 0 Then

            cmbbyName.SelectedIndex = 0

        End If

        Try

            Dim adp As New Data.OleDb.OleDbDataAdapter

            Dim ds As New DataSet

            Dim dv As New DataView

            adp.SelectCommand = cm

            adp.Fill(ds, "dataset")

            dv.Table = ds.Tables(0)

            dv.Sort = "name"

            DataGrid1.DataSource = dv

            DataGrid1.Refresh()

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        Finally

            cn.Close()

        End Try

    End Sub

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=purchases.mdb"

        Try

            cn.Open()

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

        cmbbyName.Items.Clear()

        cm.Connection = cn

        cm.CommandText = "select * from pDetails where Name like '" & txtbyName.Text & "%'"

        rd = cm.ExecuteReader()

 

        cmbbyName.Items.Clear()

        While (rd.Read)

            cmbbyName.Items.Add(rd("Name"))

        End While

        rd.Close()

        If cmbbyName.Items.Count > 0 Then

            cmbbyName.SelectedIndex = 0

        End If

        Try

            Dim adp As New Data.OleDb.OleDbDataAdapter

            Dim ds As New DataSet

            Dim dv As New DataView

            adp.SelectCommand = cm

            adp.Fill(ds, "dataset")

            dv.Table = ds.Tables(0)

            dv.Sort = "name"

            DataGrid1.DataSource = dv

            DataGrid1.Refresh()

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        Finally

            cn.Close()

        End Try

 

    End Sub

 

Article Comments
Try this for the database

Posted on 12/19/2006 9:46:32 AM by Ricky

How to add text box value in datagrid on click add button not to retrive from database and on edit button in datagrid value come in text box

Posted on 4/17/2007 5:05:13 AM by dheeraj

i want to create combobox or dropdown box in datatgrid for one particular cell of that datagrid's column .

pls help me regarding that and send code in vb.net if possible .

Posted on 4/14/2008 4:45:59 AM by mrudula

dv.Table = ds.Tables(0)

dv.Sort = "name"

DataGrid1.DataSource = dv

DataGrid1.Refresh()

Posted on 9/16/2008 6:45:00 AM by siva

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to SEARCH values from database on Run time depend upon value in text box .NET 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net