473,386 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Inserting into a Database Error

Another question for the whizzes out there. I have the following code
in a database class:

Public Sub insertData(ByVal argInsertSql As String, ByVal argTable As
String)
Dim myConnection As New OleDbConnection(dbConnString)
Dim strSelectSql As String = "SELECT * FROM " & argTable
Dim myDataAdapter As New OleDbDataAdapter(strSelectSql,
myConnection)
Dim myDataSet As New DataSet()
Dim myDataTable As DataTable()
Dim myDataRow As DataRow()

Try
myConnection.Open()
myDataAdapter.Fill(myDataSet, argTable)
myDataTable = myDataSet.Tables(argTable)

myDataRow = myDataTable.newrow
Catch
DisplayErrorMessage("clsDatabase:insertData")
End Try

End Sub

The myDataTable = myDataSet.Tables(argTable) AND
the myDataRow = myDataTable.newrow

are both generating errors. Saying cant convert data table to
1-dimensional array of type datatable. I am trying to learn how to
update a database via ADO.Net directly from an online book I have and
there example isn't working.

What am I doing wrong?

-Ivan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #1
6 1030
Hi Ivan,
Try to follow the steps below. I create the connection; I load a
dataadapter; I use it to load the dataset; then I add a new row; finally I
add a commandbuilder (you must have this or more involved insert commands to
update the back end). I'd recommend you get a good text of databases using
ado .net - ADO .Net by David Sceppa (MS Press) is about the best.

HTH,

Bernie Yaeger

Dim oconn As New SqlConnection("data source=d5z0071;initial
catalog=imc;integrated security=sspi;")

Dim ocmd As New SqlCommand("select * from histd_", oconn)

Dim oda As New SqlDataAdapter(ocmd)

Dim ods As New DataSet("History Details")

oconn.Open()

oda.Fill(ods, "History Details")

Dim r As DataRow

r = ods.Tables(0).NewRow()

r("bipad") = "98705 "

r("imcacct") = "81378-001456"

r("issuecode") = "200212"

r("posstatus") = "u"

r("draw") = 17

r("rreturn") = 2

r("net") = r("draw") - r("rreturn")

' now, to update the back end, this won't work without a commandbuilder that
references the dataadapter

Dim mcommandbuilder As SqlCommandBuilder = New SqlCommandBuilder(oda)

ods.Tables(0).Rows.Add(r)

Try

oda.Update(ods, "history details")

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

oconn.Close()
"Ivan Weiss" <iv*****@optonline.net> wrote in message
news:ui**************@TK2MSFTNGP09.phx.gbl...
Another question for the whizzes out there. I have the following code
in a database class:

Public Sub insertData(ByVal argInsertSql As String, ByVal argTable As
String)
Dim myConnection As New OleDbConnection(dbConnString)
Dim strSelectSql As String = "SELECT * FROM " & argTable
Dim myDataAdapter As New OleDbDataAdapter(strSelectSql,
myConnection)
Dim myDataSet As New DataSet()
Dim myDataTable As DataTable()
Dim myDataRow As DataRow()

Try
myConnection.Open()
myDataAdapter.Fill(myDataSet, argTable)
myDataTable = myDataSet.Tables(argTable)

myDataRow = myDataTable.newrow
Catch
DisplayErrorMessage("clsDatabase:insertData")
End Try

End Sub

The myDataTable = myDataSet.Tables(argTable) AND
the myDataRow = myDataTable.newrow

are both generating errors. Saying cant convert data table to
1-dimensional array of type datatable. I am trying to learn how to
update a database via ADO.Net directly from an online book I have and
there example isn't working.

What am I doing wrong?

-Ivan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #2
Cor
Hi Ivan,

Have a look at the datatable "copy" method.

I hope this helps a little bit

Cor
myDataTable = myDataSet.Tables(argTable)
What am I doing wrong?

Nov 20 '05 #3
I was reading a little bit and the text I have said to avoid using
command builders as they impact performance negatively and require a lot
of overhead/resources.

Is this true or are they adequate and effective?

Also, In general I am new to OOP. Is it preferred practice to create a
database class rather than typing the database code individually into
each sub I need it in throughout my program?

-Ivan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #4
Hi Ivan,

Yes, it says that everywhere. Nevertheless, if you have a single table to
update, they work just fine. Overhead? Yes, it might take 5000 times as
long - but .0000000001 of a second isn't hurt very much when it becomes
..0002 of a second, unless your updating 500,000 rows at once. Yes, there
are times when you have to 'roll your own', but it isn't terribly difficult
to do, just tedious.

Re tying the database creation code into each sub - you could do this in a
form's init code, above the load event, and load it in the load event - then
it's available throughout the form. Only problem is it is in memory
throughout, so you have to decide which approach you prefer.

Again, I strongly recommend the Sceppa book or the Vaughn book (Apress, ADO
and ADO .Net Best Practices).

HTH,

Bernie
"Ivan Weiss" <iv*****@optonline.net> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl...
I was reading a little bit and the text I have said to avoid using
command builders as they impact performance negatively and require a lot
of overhead/resources.

Is this true or are they adequate and effective?

Also, In general I am new to OOP. Is it preferred practice to create a
database class rather than typing the database code individually into
each sub I need it in throughout my program?

-Ivan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #5
I am going to take your recommendation on getting a book for sure but in
the meantime I miss the old days of VB database classes.

Is there a way to simply update the database using SQL statements
(INSERT) etc....

All I want to is add one row, and it seems to me like Visual Studio .Net
has made this unneccessarily hard to do and memory intensive. I do not
want to fill a dataset and use up RAM, I just want to quickly update my
database and thats it.

-Ivan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #6
Hi Ivan,

You're essentially correct; it's a lot more involved than it used to be.
yes, it's more flexible, but it seems unnecessarily difficult at times,
although I've gotten used to it and basically comfortable these days (but it
still takes a lot longer than it used to).

Yes, you can surely add a row using a stored procedure. Here's an example:
Dim oconn As New SqlConnection("data source=d5z0071;initial
catalog=imc;integrated security=sspi;")

Dim addcmd As New SqlCommand

addcmd = New SqlCommand("sp_histd_add2", oconn)

addcmd.CommandType = CommandType.StoredProcedure

Dim param1 As SqlParameter

param1 = addcmd.Parameters.Add("@ibipad", SqlDbType.Char, 6)

param1.Direction = ParameterDirection.Input

param1.Value = "00893 "

Try

addcmd.ExecuteNonQuery()

Catch ex As Exception

MessageBox.Show(ex.message)

End Try

oconn.Close()

HTH,

Bernie

"Ivan Weiss" <iv*****@optonline.net> wrote in message
news:eZ**************@TK2MSFTNGP09.phx.gbl...
I am going to take your recommendation on getting a book for sure but in
the meantime I miss the old days of VB database classes.

Is there a way to simply update the database using SQL statements
(INSERT) etc....

All I want to is add one row, and it seems to me like Visual Studio .Net
has made this unneccessarily hard to do and memory intensive. I do not
want to fill a dataset and use up RAM, I just want to quickly update my
database and thats it.

-Ivan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Miranda | last post by:
Hi, I have a ASP/vbscript program that generates random passwords. The problem is I need to insert those passwords into an Access database of 327 clients. I have the random password program...
0
by: Marko Poutiainen | last post by:
Situation: We had to make our SQLServer 2000 database multi-lingual. That is, certain things (such as product names) in the database should be shown in the language the user is using (Finnish,...
1
by: pmud | last post by:
I have an ASP.NET web application using C# code. I am trying to insert values from a web form into an SQL database. I am using SQL COMMAND object for this. I need to know HOW TO INSERT THE RADIO...
2
by: Charles Wilt | last post by:
I have a IBM iSeries (aka AS-400) running v5r3 of OS/400 that I access via a linked server from SQL Server 2000. The following select works fine: select * from...
0
by: pd123 | last post by:
I'm new to C# and .net and I'm trying to create a form that will register users in a sql server database. I have the following code but when I run the code I get an error " The name 'Peter' is...
0
by: toyin | last post by:
hello, pls help look through this code its not inserting the record in dataset into the another database. i want to insert the row in the dataset into another table in another database. pls help....
6
by: ashes | last post by:
Hi, I am creating an ecommerce website using Microsoft Visual Studio, VB.Net and MS Access 2003. I am new to VB.Net When someone wants to register on the website, they fill out a form and the...
6
by: Bunty | last post by:
I want to insert values in the database.If i insert values one by one then it works till 4 or 5 fields then after it gives error.In my database there are more than 20 field.Pls help me.
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
2
by: hakkatil | last post by:
Hi to all, I have a page that inserts excel sheet to access database. I am using asp. What I want to do is to check the inserting record if it is in the database. Basicly checking the dublicate...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.