I am new to vb.net and ado.net. I am trying to insert some data fom 3 testboxes into an access db and on the form is an add button. This is the code for the add btn:

Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
Dim cn As String = txtcn.Text
dbmanager.connect()
dbmanager.insertsql("insert into carinfo (Car Name) values('pip')")
End Sub

Re: ado.net (insert command) by Wes

Wes
Fri Feb 20 12:07:23 CST 2004

I'm not quite sure what the code you posted does. What is type is
dbmanager?

I would recommend not mixing your GUI and your Data logic (connecting to
the db and inserting the new row).

If dbmanager is a class that you defined to do all your db stuff then I
really cannot help you without seeing the code for the InsertSql
function. Here is how I would do an insert:

Public Class MyDataObject
Public Function InsertCar(ByVal carName As String, ByVal carMake As
String, ByVal carModel As String) As Integer
Dim myConn As New OleDbConnection("MyConectionString")
Dim cmd As New OleDbCommand("INSERT INTO CarInfo (CarName,
CarMake, CarModel) VALUES (@CarName, @CarMake, @CarModel);", myConn)

Try
myConn.Open()

cmd.Parameters.Add("@CarName", carName)
cmd.Parameters.Add("@CarMake", carMake)
cmd.Parameters.Add("@CarModel", carModel)

Return cmd.ExecuteNonQuery()
Finally
myConn.Close()

myConn.Dispose()
End Try
End Function
End Class

Your button click handler would look like this:
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnadd.Click Dim dataMapper As New
MyDataObject()

Try
dataMapper.InsertCar(txtName.Text, txtMake.Text, txtModel.Text)
Catch ex As Exception
MessageBox.Show("Insert Error: " & ControlChars.CrLf & ex.Message)
End Try
End Sub


This is obviously quite simplified from a design standpoint but should get
you started in the right direction.

Wes


On Fri, 20 Feb 2004 05:36:07 -0800, freddy
<anonymous@discussions.microsoft.com> wrote:

> I am new to vb.net and ado.net. I am trying to insert some data fom 3
> testboxes into an access db and on the form is an add button. This is
> the code for the add btn:
>
> Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles btnadd.Click
> Dim cn As String = txtcn.Text
> dbmanager.connect()
> dbmanager.insertsql("insert into carinfo (Car Name)
> values('pip')")
> End Sub