0

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??

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by c0deFr3aK
0

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.

0

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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.