Adding database records

Adding database records to existing dataset.

Page 1 of 1

2 Replies - 1131 Views - Last Post: 13 March 2009 - 05:40 AM Rate Topic: -----

#1 UnknownCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 137
  • Joined: 22-February 09

Adding database records

Posted 12 March 2009 - 03:36 PM

Hey guys having trouble adding records to an existing dataset, I tried the following code but it made a new connection which I don't want:

Dim inc As Integer
		Dim con As New OleDb.OleDbConnection
		Dim da As OleDb.OleDbDataAdapter
		Dim sql As String
		Dim ds As MembersDataSet
		da = New OleDbDataAdapter(sql, con)
		con.ConnectionString = My.Settings.MembersConnectionString
		con.Open()
		If inc <> -1 Then

			Dim NewRecord As DataRow

			NewRecord = ds.Tables("Members").NewRow()

			NewRecord.Item("Firstname") = TextBox1.Text
			NewRecord.Item("Lastname") = TextBox2.Text

			ds.Tables("Members").Rows.Add(NewRecord)

			da.Update(ds, "Members")

			MsgBox("New Record added to the Members Database")
			con.Close()

		End If


I am not very good with databses and if anyone could help me it would be greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Adding database records

#2 Damage  Icon User is offline

  • Lord of Schwing
  • member icon

Reputation: 28
  • View blog
  • Posts: 1,090
  • Joined: 05-June 08

Re: Adding database records

Posted 12 March 2009 - 04:45 PM

i'm a bit confused. It looks like your creating your dataset and filling it with data from you databse, for which you need to have a connection. I don't see any reference to a dataset created outside of this code? Am i just missing something?
Was This Post Helpful? 0
  • +
  • -

#3 hb21l6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-March 09

Re: Adding database records

Posted 13 March 2009 - 05:40 AM

This is how I add data to SQL DB. Not sure if its the best way, but it works for me.

Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient

public sub someFunction()
Dim SQL As String
Public myCommand As SqlCommand
Public myConnection As SqlConnection

myConnection = New SqlConnection("server=ServerName;uid=ua;pwd=pwd;database=Databasename")

SQL = "insert into tbl_Name (Field1, field2)" & _
" VALUES " & _
" ('" & Txt_field1.Text & "', '" & Txt_field2.Text & "',)"

Try
myCommand = New SqlCommand(SQL, myConnection)
myCommand.ExecuteNonQuery()
myCommand = Nothing
Catch ex as As Exception

End try
End function


regards
Dave
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1