Need a little more help but getting closer

vbinuyasha

Member
Joined
Feb 18, 2006
Messages
8
Programming Experience
Beginner
ok it nolonger gives me an error but it doesn't add anything to the db am i missing something here?

VB.NET:
[SIZE=2][COLOR=#0000ff]
Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] btnAdd_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnAdd.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] objRow [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataRow
[/SIZE][SIZE=2][COLOR=#008000]'Create a new DataRow object for this table
[/COLOR][/SIZE][SIZE=2]objRow = ds.Tables("CarInfo").NewRow()
[/SIZE][SIZE=2][COLOR=#008000]'Edit Each Field value
[/COLOR][/SIZE][SIZE=2]objRow.Item("Make") = txtmake.Text
objRow.Item("Model") = txtmodel.Text
objRow.Item("Color") = txtcolor.Text
objRow.Item("Year") = txtyear.Text
[/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2]cn = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Alex\My Documents\Visual Studio Projects\WindowsApplication3\Cars.mdb;")
cn.Open()
str = "Insert into CarInfo values(" & txtmake.Text & ",'" & txtmodel.Text & "','" & txtcolor.Text & "','" & txtyear.Text & "')"
[/SIZE][SIZE=2][COLOR=#008000]'string stores the command and CInt is used to convert number to string
[/COLOR][/SIZE][SIZE=2]cmd = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OleDbCommand
icount = cmd.ExecuteNonQuery
MessageBox.Show(icount)
[/SIZE][SIZE=2][COLOR=#0000ff]Catch
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'displays number of records inserted
[/COLOR][/SIZE][SIZE=2]cn.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

please can some one help me beside a doctor and a padded room!!!!!!
 
I think your SQL statement is not completely OK, I think you are missing at least one single quote (hint: use parameters instead of concatenating your SQL string).
Furthermore the OleDbCommand object has no OleDbConnection and no CommandText:
VB.NET:
cmd = [SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OleDbCommand(str, cn)[/SIZE]

I also take the liberty to recommend you Chapter 4 of this Free Book
 
Maybe this pice of code I am useing in my program will help you with your problem. I was haveing a problem with adding to my database untill I found that I was missing one line of code.Hope this might help you.


VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] btnAdd_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnAdd.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] newrow [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Data.DataRow
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] btnAdd.Text = "&Cancel" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]Cancel()
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Add new row
[/COLOR][/SIZE][SIZE=2]newrow = Condvd1.Media.NewRow
[/SIZE][SIZE=2][COLOR=#008000]' Enter data in each field
 
[/COLOR][/SIZE][SIZE=2]newrow.Item("Disk Name") = InputBox("Please Enter The Name Of The Movie")
newrow.Item("Disk ID") = InputBox("Please Enter The ID")
newrow.Item("Quantity") = InputBox("Please Enter The Quantity")
newrow.Item("Category") = InputBox("Please Enter What The Category IS")
Condvd1.Media.Rows.Add(newrow)
CONDVD.Update(Condvd1)
[/SIZE][SIZE=2][COLOR=#008000]' Move To Display Added Record Which Is At The End
[/COLOR][/SIZE][SIZE=2]currManager.Position = currManager.Count - 1
currManager.Refresh()
Showposition()
[/SIZE][SIZE=2][COLOR=#008000]'If Cancel Is Clicked During InputBox Or Other Errors Occur
[/COLOR][/SIZE][SIZE=2]MessageBox.Show("Canceling Add Operation")
 
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE]

 
vbonuyasha - Look in my sig for a couple of ADO.NET tutorials. You might find them helpful, especialy part 2, where I show how to use parameters. They are written against SQL Server, but can be eaisily moved to OLEDB by changing the types.

-tg
 
Back
Top