SqlCommand.ExecuteNonQuery() insertion skipping half the rows.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?U3dvcmRBbmdlbA==?=

    SqlCommand.ExecuteNonQuery() 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?
Working...