473,385 Members | 1,944 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,385 software developers and data experts.

extra row being added to table

Whenever I run this code, I get an extra row added to my table with a
primary field text of "System.Object". This code is intended to
simply add a single row to a single table based on the value in the
tbOrd.text textbox. Instead, two rows are going in - one is intended,
the second is not. Anybody see why this is happening and what I need
to do to stop the extra row from being added.

Dim strNewOrder, strSQL As String
Dim rowToInsert As DataRow
Dim tbl As New DataTable("Orders")

tbl.Columns.Add("Order", GetType(String))
tbl.Columns.Add("ID", GetType(String))

strNewOrder = tbOrd.Text
strSQL = "INSERT INTO Orders " & _
" (Order) VALUES " & _
" (@Order)"

Dim da As New SqlDataAdapter(strSQL, cn)
da.SelectCommand.Parameters.AddWithValue("Order", strNewOrder)
da.Fill(tbl)

cn.Open()

rowToInsert = tbl.Rows.Add(New Object(), strNewOrder)

Dim cmdInsert As New SqlCommand(strSQL, cn)
cmdInsert.Parameters.AddWithValue("@Order",
rowToInsert("Order"))

Try
cmdInsert.ExecuteNonQuery()
rowToInsert.AcceptChanges()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

cn.Close()
End Sub

Thanks

Apr 27 '07 #1
2 1164
When you are adding your row, you are not doing it correctly.
The method Add doesn't have two parameters. Only one which is an array of
values (Each for 1 column).

So you should change the tbl.Add to tbl.Add(New Object() {strNewOrder})

Jonathan Boivin
---
jo************@cints.net | http://www.cints.net
"Alex" <fk****@yahoo.coma écrit dans le message de news:
11**********************@o40g2000prh.googlegroups. com...
Whenever I run this code, I get an extra row added to my table with a
primary field text of "System.Object". This code is intended to
simply add a single row to a single table based on the value in the
tbOrd.text textbox. Instead, two rows are going in - one is intended,
the second is not. Anybody see why this is happening and what I need
to do to stop the extra row from being added.

Dim strNewOrder, strSQL As String
Dim rowToInsert As DataRow
Dim tbl As New DataTable("Orders")

tbl.Columns.Add("Order", GetType(String))
tbl.Columns.Add("ID", GetType(String))

strNewOrder = tbOrd.Text
strSQL = "INSERT INTO Orders " & _
" (Order) VALUES " & _
" (@Order)"

Dim da As New SqlDataAdapter(strSQL, cn)
da.SelectCommand.Parameters.AddWithValue("Order", strNewOrder)
da.Fill(tbl)

cn.Open()

rowToInsert = tbl.Rows.Add(New Object(), strNewOrder)

Dim cmdInsert As New SqlCommand(strSQL, cn)
cmdInsert.Parameters.AddWithValue("@Order",
rowToInsert("Order"))

Try
cmdInsert.ExecuteNonQuery()
rowToInsert.AcceptChanges()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

cn.Close()
End Sub

Thanks

Apr 27 '07 #2
Thanks, Jonathan. Your suggestion was right. Once I changed that
line (and re-ran the app), the record that included the field
System.Object immediately changed to the correct value. However, I
still had an extra row - now the rows were identical. That tipped me
off that I was calling the add command twice. I took out the
cmdInsert.ExecuteNonQuery and everything is working as intended.

Thanks for your help.

Apr 27 '07 #3

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

Similar topics

0
by: scooterm | last post by:
### Question Can anyone name a text editor or XML editor (prefer open source) that can open a simple table XML file in a spreadsheet-type GUI, without adding any extra formatting or junk to the...
5
by: Brian | last post by:
Hello group, http://people.umass.edu/btrembla/ (html 4.01/strict) uses a conventional menu marked up as a list, and css http://people.umass.edu/btrembla/css/brian.css...
9
by: Neil Ginsberg | last post by:
I have a strange situation using Access to automate a Word mail merge. Using Access 2000 and Word 2000, the code opens Word, opens the document in Word, sets a table in the calling Access...
5
by: dw | last post by:
hello - first, let me state that i am an Asp.Net rookie. here is the situation: i have a page that looks good in the vs.net designer, but when the page renders there are extra amounts...
11
by: cdkorzen | last post by:
I'm sorry if this is a rehash, but all I see is the same info. Here's my debacle: I CAN get the PATH_INFO to work. With ANYTHING but ASP. Python, Perl, Cmd files... works fine. ASP can't...
3
by: dchristjohn | last post by:
I am currently developing a small windows application using Visual Basic via Visual Studio 2005. My database resides on a SQL 2000 server. I have a table with three fields: id (int, Not Null)...
2
by: chrism | last post by:
The problem that I am having is adding a new record to a table. Everytime I close the form, it adds a blank field to the table. How do I stop that from happening? The following is the code that is...
1
by: metaperl | last post by:
Far below (in section "original 3 steps"), you see the following: 1. a temp table is created 2. some data is inserted into this table 3. some of the inserted data is removed based on a join with...
4
Fary4u
by: Fary4u | last post by:
Hi for the last few months i've just stuck in 1 error & i don't know how to figer it out could any body find out where is the problem gonna be ? it's working fine but when u add product into...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.