Pls let me know how to add a record in a database using a dataset.


vidhya

I'm having a similar problem with SQL or Access either one. Using the MS Visual Studio DataSet binding source and dataset table binder.

I can enter data into my forms, but it's not being saved into the tables, when I press the save button, or add button, created in VS.2005 VB.NET.

Any help would be greatly appreciated. I couldn't get it to save the data in a SQL database either.

Is there some code that needs to be added to the data wizard?

Michael

Here is the solution Michael

Consider you have 3 fields in your table (id, name, course) and to add a new record from your form to database........

Dim conn1 As New OleDbConnection(" your connection string")
Dim cmd1 As New OleDbCommand("insert into stu (id,name,course) VALUES ('" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & TextBox3.Text & "')", conn1)
Dim da1 As New OleDbDataAdapter(cmd1)
da1.Fill(ds, "stu")

Dim dr As DataRow = ds.Tables("stu").NewRow
dr.Item(0) = TextBox1.Text
dr.Item(1) = TextBox2.Text
dr.Item(2) = TextBox3.Text
da1.Update(ds, "stu")
MsgBox("RECORD ADDED")

Vidhya Thiyagu

Dim conn1 As New OleDbConnection("connection string")
Dim cmd1 As New OleDbCommand("insert into stu (id,name,course) VALUES ('" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & TextBox3.Text & "')", conn1)
Dim da1 As New OleDbDataAdapter(cmd1)
da1.Fill(ds, "stu")
Dim dr As DataRow = ds.Tables("stu").NewRow
dr.Item(0) = TextBox1.Text
dr.Item(1) = TextBox2.Text
dr.Item(2) = TextBox3.Text
da1.Update(ds, "stu")
MsgBox("ADDED")

Thank you for the code assistance, however I couldn't get the code to work :(

Dim conn1 As New OleDbConnection("connection string")
Dim cmd1 As New OleDbCommand("insert into stu (id,name,course) VALUES ('" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & TextBox3.Text & "')", conn1)
Dim da1 As New OleDbDataAdapter(cmd1)
da1.Fill(ds, "stu")
Dim dr As DataRow = ds.Tables("stu").NewRow
dr.Item(0) = TextBox1.Text
dr.Item(1) = TextBox2.Text
dr.Item(2) = TextBox3.Text
da1.Update(ds, "stu")

i have tried this process, but the problem i got is new. when i enter some data, the data is stored, but when i restart the application and start to enter the previous data is deleted and new datas are stored only

how can you save a record without neccessrily adding the value of id field

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.