Hello,
I am using OLEDBConnection and OLEDBDataAdapter and connected to the MS-Access database.
how can i add the textbox value to the table in database on a button click and want to display it in datagridview??

Recommended Answers

All 2 Replies

If I'm understanding your question correctly, you need to add a dataset, add a datagrid to your form, then change the data source property of the datagrid to point to the dataset.

I will give you a sample which will guide you on this particular subject.
In your Form1 suppose you have:
1 DataGridView1
1 Textbox1
1 Button1

In your Database suppose you have

1 Table1
1 Field1 = Textfield

you can rename them if you like, so in your Button1_Click event

'our conn is outside the Button_Click so that we can re-use it anywhere within the Form1.

Dim conn As New Oledb.OledbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source = Your Database Name;Persist Security Info = False")

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If Textbox1.Text = "" Then
   MsgBox ("Textbox1 is Empty")
   Exit Sub
Else
conn.Open()

Dim da As New Oledb.OledbDataAdapter("Select * FROM Table1", conn)
Dim ds As New DataSet
Dim dt As DataTable, row As DataRow
Dim cb As New Oledb.OledbCommandBuilder(da)
da.Fill(ds)
dt = ds.Tables(0)
row = dt.NewRow

row("Field1") = Textbox1.Text

dt.Rows.Add(row)
da.Update(ds)

MsgBox ("1 Record Inserted")
DataGridView1.DataSource = Nothing
dView
Textbox1.Text =""
conn.Close()
End Sub

Private Sub dView()
conn.Open()
Dim da As New Oledb.OledbDataAdapter("Select Field1 From Table1 Order By Field1", conn)
Dim dt As New DataTable
da.Fill(dt)

Dim col0 As New DataGridViewTexBoxColumn
col0.HeaderText = "My Field 1"
col0.DataPropertyName = "Field1"
col0.Width = 150
DataGridView1.Columns.Add(col0)

DataGridView1.DataSource = dt

End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.