Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

adding a record to a datatable and hence the datasource

Posted on 2005-04-07
1
Medium Priority
?
233 Views
Last Modified: 2010-04-23
can somebody tell me whats wrong with this(or at least the theory of it)
im just trying to add a record to my datatable

Dim JobTypeDataAdapter As New OleDbDataAdapter _
("SELECT lngJobTypeID, strJobTypeTAG,curHourlyPayment, memAdditionalNotes FROM tblJobTypes", cnDBConnection)

Dim myDataTable As New DataTable
Dim myDataRow As DataRow = myDataTable.NewRow

private sub addrec()

myDataRow("lngJobTypeID") = CLng(111)
myDataRow("strJobTypeTAG") = txtTitle.Text
myDataRow("curHourlyPayment") = txtHrsRate.Text
myDataRow("memAdditionalNotes") = txtAddDesc.Text

JobTypeDataAdapter.Update(myDataTable)
JobTypeDataAdapter.Fill(myDataTable)

end sub
0
Comment
Question by:scm0sml
1 Comment
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 2000 total points
ID: 13733608
The problem is with the Function

private sub addrec()

myDataRow("lngJobTypeID") = CLng(111)
myDataRow("strJobTypeTAG") = txtTitle.Text
myDataRow("curHourlyPayment") = txtHrsRate.Text
myDataRow("memAdditionalNotes") = txtAddDesc.Text

JobTypeDataAdapter.Update(myDataTable) '<-- You are trying to Update the dataTable which has not yet been Filled, So your Next Line should come first
'Also you are trying to Insert New Rows into the BackEnd table but where did you specify an Insert Query ??? You have just specified a Select Query (at the time of creating the DataAdapter)
'So you need to either specify an Insert Query (and an Update, Delete Query if you plan to Update/Delete rows)
'Now .NET provides something called CommandBuilder to automatically generate these Queries for you, This can generate Queries if you use a single table at a time, i.e. No Join. See code below for a corrected version of the function

JobTypeDataAdapter.Fill(myDataTable)

end sub

Here's a corrected one...

private sub addrec()
'Create a CommandBuilder and pass it the DataAdapter instance so that it can set the DataAdapter's Insert/Update/Delete Queries
Dim CB as New OleDBCommandBuilder(JobTypeDataAdapter)

JobTypeDataAdapter.Fill(myDataTable) 'Fill it beforehand

myDataRow("lngJobTypeID") = CLng(111)
myDataRow("strJobTypeTAG") = txtTitle.Text
myDataRow("curHourlyPayment") = txtHrsRate.Text
myDataRow("memAdditionalNotes") = txtAddDesc.Text

myDataTable.Rows.Add(myDataRow) '<-- THIS is also REQUIRED unless and untill you Add the New row to the dataTable it does not work. This might be a thing to think because one might argue that since we have already said
'myDataRow As DataRow = myDataTable.NewRow why anothe Add is required, but this is how it is. You can think like when we say NewRow it actually creates an Empty row Pointer but that is assigned only when we say Add

JobTypeDataAdapter.Update(myDataTable) 'And Then Finally This

end sub


This will work for you but for a better programming, you should not have the dataAdapter opened at form level. Good Object Oriented programming says keep the Scope as small as possible. So the dataAdapter should open just before it is required within the function itself. Similarly for the dataRow variable it is practically not required after we have Added the Row to the Datatable so why keep a larger scope declare it within the function itself....
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question