473,750 Members | 2,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SqlCommand.Exec uteNonQuery() insertion skipping half the rows.

Hello,
I have a function that looks like the following:

<code snippet>
private void function genRegCodes(lon g maxid, long numOfRowsToInse rt) {
bool ok;
SqlConnection sqlcon = new SqlConnection(" my connection string");
SqlCommand sqlcmd = new insertcmd("inse rt into regcodes (id, regcode) values
(@id, @regcode)", sqlcon);
sqlcon.Open();
for (long i = 0; i < numOfRowsToInse rt; i++)
{
ok = false;
while (!ok)
{
try
{
insertcmd.Param eters["@id"].Value = maxid;
insertcmd.Param eters["@regcode"].Value = random_regcode( maxid);
// random_regcode( ) generates a random regcode from maxid.
numrows = insertcmd.Execu teNonQuery();
}
catch (SqlException sqlexc)
{
maxid++;
}
if (numrows 0)
ok = true;
}
}
sqlcon.Close();
}
</code snippet>
The problem is that the line:
numrows = insertcmd.Execu teNonQuery();
inserts a row exactly every other iteration of the for loop, without
throwing an exception (I am certain that it did not throw an exception
because the id column of the table never skips after the call to the function
finishes). The value of numrows is also 1 for every iteration. So in the end,
if numOfRowsToInse rt is set to 100, I get 50 rows generated instead. What can
be causing this behavior?
Apr 11 '08 #1
0 1670

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

Similar topics

2
7345
by: Mark | last post by:
Hi, I have a sqlCommand object that I use to execute an SQL statement. I thought I could use the ExecuteNonQuery to test for the number of rows returned by the sql command but it does not seem to work My code is below.. Dim sSQL As String = "SELECT Blah FROM Table WHERE BlahID = 5" Dim oCOmmand As New SqlClient.SqlCommand(sSQL, openDatabase)
2
8328
by: Andre Ranieri | last post by:
I'm having trouble with what should be a simple task; beginning and committing T-SQL transactions using the SQLClient. I'm using a SqlCommand (cmd) to begin the transaction and delete records from two tables. if both records are successfully deleted, I'd like to commit the transaction. Everything works fine until I attempt to commit the first time the foreach loop runs, at this point I get the following exception. I haven't been able...
2
1380
by: Tracey | last post by:
Sorry for the repeated post. I tried to update a record in database using SqlCommand.ExecuteNonQuery( ) method (I failed using SqlDataAdapter). I traced the above statement and found that it returned 1 rows afftected. Then I checked my database, the data was not updated at all Can someone give me a clue ?
5
4456
by: JIM.H. | last post by:
Hello, I could not call this sub routine because it says SqlConnection and SqlCommand is not defined. Public Sub CreateMySqlCommand(ByVal myExecuteQuery As String, ByVal myConnection As SqlConnection) Dim myCommand As New SqlCommand(myExecuteQuery, myConnection) myCommand.Connection.Open() myCommand.ExecuteNonQuery() myConnection.Close() End Sub 'CreateMySqlCommand
4
2477
by: christof | last post by:
I'm trying to run this T-SQL code using SqlCommand: USE master; ALTER DATABASE AdventureWorks SET SINGLE_USER; GO I did it like that: public static int ObtainExclusiveAccess(Server srv, string database)
1
5848
by: Noppers | last post by:
I am trying to insert data into 2 tables, Order and Order_Item, in a transaction. Everything works fine if I only have 1 row in my objCartDT dataset. If I have only one row, the 2 tables are updated as expected. But if I have more than one row in the objCartDT, something fails, and I can't figure out what (but I suspect it has to do with the params in conjunction with "cmdNewOrder_Items.ExecuteNonQuery();" and the looping I'm trying to do. Maybe...
6
2658
by: Nuzzi | last post by:
Hello All, I have two pages that are very similar. One is working, one is not. Here is the code for both: Page 1 (Working): protected void btn_update_Click(object sender, EventArgs e) { Int32 item_id = Convert.ToInt32(ViewState);
7
3271
by: Gustaf | last post by:
Hi all, Just for fun, I'm working on a script to count the number of lines in source files. Some lines are auto-generated (by the IDE) and shouldn't be counted. The auto-generated part of files start with "Begin VB.Form" and end with "End" (first thing on the line). The "End" keyword may appear inside the auto-generated part, but not at the beginning of the line. I imagine having a flag variable to tell whether you're inside the...
2
2111
by: TheSteph | last post by:
Hi I have a SQLCommand that do updates on a table. I have a loop where I set the parameters then I call ExecuteNonQuery. For . { . Set the SQLCommand's Params Values;
0
9001
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9584
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9397
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9257
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8264
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.