Adding a new row to a database

Google

New member
Joined
Jan 16, 2007
Messages
1
Programming Experience
Beginner
Hi, I'm new to VB.Net and using databases (MS Access). I'm trying to create the coding to add the text from a combo and 2 textboxes to the database.

Dim strSQL As String = "INSERT INTO Person(Title, first_name, surname) VALUES(@Title, @first_name, @surname)"
Dim insertDataAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(strSQL, conn)
Dim insertCommand As New Data.OleDb.OleDbCommand(strSQL)
insertCommand.Parameters.Add(
"@Title", DbType.String, 255, Title.Text)
insertCommand.Parameters.Add(
"@first_name", DbType.String, 255, TextBox1.Text)
insertCommand.Parameters.Add(
"@surname", DbType.String, 255, TextBox2.Text)

Dim insertDataSet As DataSet = New DataSet ' Now DataSet
insertDataAdapter.Fill(insertDataSet, "AllRows")
' Get a reference to the "AllRows" DataTable.
Dim insertDataTable As DataTable = insertDataSet.Tables("AllRows")
Dim row As DataRow
row = insertDataTable.NewRow()
' Add a record
row("Title") = Trim(Title.Text)
row(
"first_name") = Trim(TextBox1.Text)
row(
"surname") = Trim(TextBox2.Text)
insertDataTable.Rows.Add(row)
insertDataAdapter.Update(insertDataSet,
"AllRows") ' Update the database
insertDataAdapter.Dispose()
conn.Close()
conn.Dispose()

On the first underlined line i get the error message: No value given for one or more required parameters.

On the second underlined line i get the em: Object reference not set to an instance of an object.

 
Need a select command before can call FILL

You called FILL but didn't define a select command for the dataadapter. The insert command may not like the field TITLE but I am not for sure.

I attached a working example to look at.

HTH
 

Attachments

  • MSACCESS_EDITABLE_GRID.zip
    64.1 KB · Views: 27
Because the Fill failed, the NewRow won't return a proper DataRow object.

-tg
 
Back
Top