Add Record Not Using Bound Data

  • Thread starter Thread starter Wayne Wengert
  • Start date Start date
W

Wayne Wengert

I have a VSNET ASP/VB app in which a user selects options on the page and
then I want to add a record with that data to a table. The table is in an
SQL Server 2000. Nothing is bound to the table. In old ADO I would create a
connection and then do an .AddNew, set the field values from the textboxes
and then do an .Update

How do I accomplish this in .NET?

Wayne
 
Dim dro as DataRow = dataTableName.NewRow
dro(0) = WhateverFirstValue
dro(1) = WhateverSecondValue
dro(0) = EtcEtc

datatTableName.Rows.Add(dro)

myDataAdapter.Update(dataTableName)
 
William;

Thanks for the suggestion. I tried adapting my code as you suggest but I am
getting a message:

Error attempting to save data: Update requires a valid InsertCommand when
passed DataRow collection with new rows.

When I run the code. My code is as follows (I cut some lines to save space)

==================================================
Dim strConn, strSQL As String

strConn = "Provider=SQLOLEDB;Data Source=sql.wengert.org;Initial
Catalog=wengert;UID=wengert;PWD=wpr5893"

strSQL = "Select * FROM judgingoveralleval"

Dim da As New OleDb.OleDbDataAdapter(strSQL, strConn)

Dim tbl As New DataTable("OverallEvals")

da.Fill(tbl)

'tbl.Rows.Add(New Object() strNewData

Dim dro As DataRow = tbl.NewRow

'dro(0) = First col is Identity

dro(1) = 123 '***** Will be Unit ID

dro(2) = IIf((aryOverall(0) > 0), aryOverall(0), 0)

dro(3) = IIf((aryOverall(1) > 0), aryOverall(1), 0)

dro(4) = IIf((aryOverall(2) > 0), aryOverall(2), 0)

dro(5) = IIf((aryOverall(3) > 0), aryOverall(3), 0)

..........................

tbl.Rows.Add(dro)

' Submit the changes

Try

da.Update(tbl)

Response.Write("Data has been saved!")

Catch ex As Exception

Response.Write("Error attempting to save data: " & vbCrLf & ex.Message)

Response.End()

End Try

==================================================

Wayne
 
Thanks guys. I had done some more googleing and found that ADO.NET link. I
added the code to build the Update and got things working.

I appreciate all the help.

Wayne
 
Back
Top