You are here:

VB.NET/VB.Net and Databases

Advertisement


Question
Hi Steve - I'm a fellow expert (in Word and Excel) and am coming to .net from the good old days of VB6.  I'm trying to convert a database application, and I'm fine with reading and modifying existing data, as the system seems to do it all for me!  I've created a full set of data controls (possibly more than I need) so I have one or more of:
binding source
table adapter
table manager
binding navigator
Data Set

What I'm TRYING to do is to add a new record to my table.  In vb6, I'd have a datacontrol, to which dataset I'd "addnew" and populate the fields.  I can see that in .net I need to create a new row, then get that populated and added, but I'm struggling to do this - and get the data to be saved!!  By the way, I'm also puzzled as to how I can (eventually) publish this application so that the data can be wherever the user put it!!

Any help VERY gratefully received!!

Answer
I am assuming you are using Datatables here and you are want to add a row to that datatable


''Create a row object
Dim rowNewRowL As DataRow

''assign it to your data table, this will give your row the field names and data types ''associated with the columns in the table.

rowNewRowL = MyDataTable.NewRow

''Assign values the fields in your row using the column names from the table.
rowNewRowL.Item("YourDataFieldname") = ValueToAddTable
rowNewRowL.Item("YourDataFieldname") = ValueToAddTable
rowNewRowL.Item("YourDataFieldname") = ValueToAddTable
rowNewRowL.Item("YourDataFieldname") = ValueToAddTable
rowNewRowL.Item("YourDataFieldname") = ValueToAddTable
rowNewRowL.Item("YourDataFieldname") = ValueToAddTable
rowNewRowL.Item("YourDataFieldname") = ValueToAddTable
rowNewRowL.Item("YourDataFieldname") = ValueToAddTable

''Add the row to the table
MyDatatable.Rows.Add(rowNewRowL)

If you are using sql server as a back end here is a bulk insert function I wrote to insert all the data in the table back to the table in SQL server.  I am assuming the datatable in code is a one to one match for the table in SQL server where the data will be stored.


  Public Function BulkInsert(ByVal DatabaseConnection As String, _
         ByVal TableToInsert As DataTable)

       Dim objSQLConnect As SqlConnection
       Dim objSQLBulk As SqlBulkCopy
       Dim intColumnCount As Int64
       Dim intColumnProcessed As Integer

       Try
         objSQLConnect = New SqlConnection(DatabaseConnection)
         objSQLConnect.Open()
         objSQLBulk = New SqlBulkCopy(objSQLConnect)
         intColumnCount = TableToInsert.Columns.Count
         While intColumnProcessed < intColumnCount
         objSQLBulk.ColumnMappings.Add(intColumnProcessed, intColumnProcessed)
         intColumnProcessed = intColumnProcessed + 1
         End While
         objSQLBulk.DestinationTableName = TableToInsert.TableName
         objSQLBulk.BatchSize = 1000
         objSQLBulk.BulkCopyTimeout = 90
         objSQLBulk.WriteToServer(TableToInsert)
         objSQLConnect.Close()

         Return True
       Catch ex As Exception
         Dim strMethod As String
         Dim objStackTrace As StackTrace = New StackTrace()
         Dim objStackFrame As StackFrame = objStackTrace.GetFrame(0)
         strMethod = objStackFrame.GetMethod.ToString
         Throw New Exception(" Error in  " & strMethod & ex.Message.ToString)
         Return False
       End Try
   End Function

VB.NET

All Answers


Answers by Expert:


Ask Experts

Volunteer


Steve

Expertise

General programming techniques, debugging questions.

Experience

12 Year programming in VB

Education/Credentials
BS Degree University of Northern Iowa

©2016 About.com. All rights reserved.