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

?At a loss? ExecuteNonQuery executing my Insert Statement 3 times in debug mode

I know this sounds strange but I am at a loss. I am calling a simple
funtion that opens a connection to a SQL Server 2000 database and executes
an Insert Statement.

private void AddMinimunWageStipen(string payrollid,double amount)
{

System.Data.SqlClient.SqlConnection cn = null;
System.Data.SqlClient.SqlCommand cm = null;

string sql = "INSERT INTO tblStipen (EmployeeID, StipenTypeID, Units,
Amount, AddHours, PayrollID) ";
sql+= "VALUES (" + _employeeid + ", 3, 1, " + amount + ", 0, " + payrollid
+ ")";

try
{
cn = new
System.Data.SqlClient.SqlConnection(PayrollSystem. Data.SQLSERVER);
cn.Open();
cm = new System.Data.SqlClient.SqlCommand(sql,cn);
int flag = cm.ExecuteNonQuery();
cn.Close();
}
catch(Exception ex)
{
throw ex;
}
finally
{
cm.Dispose();
cn.Dispose();
}

}

When I run this code in the debugger and step through this funtion, when I
execute the line "int flag = cm.ExecuteNonQuery();" the Insert Statement
gets exectued 3 times. So I have the same record in the database 3 times??
And I know the the code is only being exectuted once. The function isn't
being called 3 times with the same parameters being passed in. I am also
not doing any special threading.

If I run the program without stopping in this function the code behave like
it should and the record only gets inserted once.

Is there something I am missing? Has anyone else ran into this problem?

Thanks in advance,

Mike R
Nov 15 '05 #1
10 3367
I have found similar behavior when stepping through code that deals with a
datareader's read method. What I found was that anytime the debugger was
asked to give me ANY information about the data (i.e. watch, autos and local
windows of the debugger) it would cause the datareader to invoke its read
method because it had to try to get some data for me to look at while
debugging.

It sounds like the ExecuteNonQuery method of the command object acts the
same way in the debugger. This behavior is troubling because it basically
means that you can't debug (with any degree of certainty) certain
statements.
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
I know this sounds strange but I am at a loss. I am calling a simple
funtion that opens a connection to a SQL Server 2000 database and executes
an Insert Statement.

private void AddMinimunWageStipen(string payrollid,double amount)
{

System.Data.SqlClient.SqlConnection cn = null;
System.Data.SqlClient.SqlCommand cm = null;

string sql = "INSERT INTO tblStipen (EmployeeID, StipenTypeID, Units,
Amount, AddHours, PayrollID) ";
sql+= "VALUES (" + _employeeid + ", 3, 1, " + amount + ", 0, " + payrollid + ")";

try
{
cn = new
System.Data.SqlClient.SqlConnection(PayrollSystem. Data.SQLSERVER);
cn.Open();
cm = new System.Data.SqlClient.SqlCommand(sql,cn);
int flag = cm.ExecuteNonQuery();
cn.Close();
}
catch(Exception ex)
{
throw ex;
}
finally
{
cm.Dispose();
cn.Dispose();
}

}

When I run this code in the debugger and step through this funtion, when I
execute the line "int flag = cm.ExecuteNonQuery();" the Insert Statement
gets exectued 3 times. So I have the same record in the database 3 times??
And I know the the code is only being exectuted once. The function isn't
being called 3 times with the same parameters being passed in. I am also
not doing any special threading.

If I run the program without stopping in this function the code behave like it should and the record only gets inserted once.

Is there something I am missing? Has anyone else ran into this problem?

Thanks in advance,

Mike R

Nov 15 '05 #2
Mike:

How do you know it's being executed 3 times? If this isn't in a loop and
there's not multiple threads running it, I would be the house that it's not
really executing three times.

As an aside, I'd really lose the dynamic sql and either use Parameters or
ideally make it a proc...

sql = "INSERT INTO tblStipen(EmployeeId, StipenTypeID, Units, Amount,
AddHours, PayrollID) Values
@EmployeeId, @StipType, @Units, @Amount, @Hours, @PayrollID)";

cmd.Parameters.Clear();
cmd.Parameters.Add("@EmployeeId", SqlDbType.Employee'sType ie Int,
Length).Value = _employeeid;
cmd.Parameters.Add("@StipType", SqlDbType.SmallInt).Value = 3;

etc.

I'm guessing when you say it's gettting executed 3 times it's based on the
return value being 3? First verify that it's not actually being inserted 3
times by querying the DB. Since it's a simple insert, I can't see how 3
records could be getting inserted, but you may have a trigger or something
that's causing this number to behave funny.

First let me know if it's the number 3 as a return value that's the issue or
if there are verifiably 3 records getting inserted.

HTH,

Bill
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
I know this sounds strange but I am at a loss. I am calling a simple
funtion that opens a connection to a SQL Server 2000 database and executes
an Insert Statement.

private void AddMinimunWageStipen(string payrollid,double amount)
{

System.Data.SqlClient.SqlConnection cn = null;
System.Data.SqlClient.SqlCommand cm = null;

string sql = "INSERT INTO tblStipen (EmployeeID, StipenTypeID, Units,
Amount, AddHours, PayrollID) ";
sql+= "VALUES (" + _employeeid + ", 3, 1, " + amount + ", 0, " + payrollid + ")";

try
{
cn = new
System.Data.SqlClient.SqlConnection(PayrollSystem. Data.SQLSERVER);
cn.Open();
cm = new System.Data.SqlClient.SqlCommand(sql,cn);
int flag = cm.ExecuteNonQuery();
cn.Close();
}
catch(Exception ex)
{
throw ex;
}
finally
{
cm.Dispose();
cn.Dispose();
}

}

When I run this code in the debugger and step through this funtion, when I
execute the line "int flag = cm.ExecuteNonQuery();" the Insert Statement
gets exectued 3 times. So I have the same record in the database 3 times??
And I know the the code is only being exectuted once. The function isn't
being called 3 times with the same parameters being passed in. I am also
not doing any special threading.

If I run the program without stopping in this function the code behave like it should and the record only gets inserted once.

Is there something I am missing? Has anyone else ran into this problem?

Thanks in advance,

Mike R

Nov 15 '05 #3
Well the way I can tell that the Insert statement is being executed 3 times
is because there are 3 records in the database.

1 is being returned by the ExecuteNonQuery command. No there aren't any
triggers running.
Mike

"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:eq**************@TK2MSFTNGP10.phx.gbl...
Mike:

How do you know it's being executed 3 times? If this isn't in a loop and
there's not multiple threads running it, I would be the house that it's not really executing three times.

As an aside, I'd really lose the dynamic sql and either use Parameters or
ideally make it a proc...

sql = "INSERT INTO tblStipen(EmployeeId, StipenTypeID, Units, Amount,
AddHours, PayrollID) Values
@EmployeeId, @StipType, @Units, @Amount, @Hours, @PayrollID)";

cmd.Parameters.Clear();
cmd.Parameters.Add("@EmployeeId", SqlDbType.Employee'sType ie Int,
Length).Value = _employeeid;
cmd.Parameters.Add("@StipType", SqlDbType.SmallInt).Value = 3;

etc.

I'm guessing when you say it's gettting executed 3 times it's based on the
return value being 3? First verify that it's not actually being inserted 3 times by querying the DB. Since it's a simple insert, I can't see how 3
records could be getting inserted, but you may have a trigger or something
that's causing this number to behave funny.

First let me know if it's the number 3 as a return value that's the issue or if there are verifiably 3 records getting inserted.

HTH,

Bill
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
I know this sounds strange but I am at a loss. I am calling a simple
funtion that opens a connection to a SQL Server 2000 database and executes an Insert Statement.

private void AddMinimunWageStipen(string payrollid,double amount)
{

System.Data.SqlClient.SqlConnection cn = null;
System.Data.SqlClient.SqlCommand cm = null;

string sql = "INSERT INTO tblStipen (EmployeeID, StipenTypeID, Units,
Amount, AddHours, PayrollID) ";
sql+= "VALUES (" + _employeeid + ", 3, 1, " + amount + ", 0, " +

payrollid
+ ")";

try
{
cn = new
System.Data.SqlClient.SqlConnection(PayrollSystem. Data.SQLSERVER);
cn.Open();
cm = new System.Data.SqlClient.SqlCommand(sql,cn);
int flag = cm.ExecuteNonQuery();
cn.Close();
}
catch(Exception ex)
{
throw ex;
}
finally
{
cm.Dispose();
cn.Dispose();
}

}

When I run this code in the debugger and step through this funtion, when I execute the line "int flag = cm.ExecuteNonQuery();" the Insert Statement gets exectued 3 times. So I have the same record in the database 3 times?? And I know the the code is only being exectuted once. The function isn't being called 3 times with the same parameters being passed in. I am also not doing any special threading.

If I run the program without stopping in this function the code behave

like
it should and the record only gets inserted once.

Is there something I am missing? Has anyone else ran into this problem?

Thanks in advance,

Mike R


Nov 15 '05 #4
Mike,
AFAIK, i have never seen this happen.
Is it possible that you have the expression "cm.ExecuteNonQuery();" in your
watch list? This might cause the expression to be called and there by
inserting rows.
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:ez**************@TK2MSFTNGP09.phx.gbl...
Well the way I can tell that the Insert statement is being executed 3 times is because there are 3 records in the database.

1 is being returned by the ExecuteNonQuery command. No there aren't any
triggers running.
Mike

"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:eq**************@TK2MSFTNGP10.phx.gbl...
Mike:

How do you know it's being executed 3 times? If this isn't in a loop and
there's not multiple threads running it, I would be the house that it's not
really executing three times.

As an aside, I'd really lose the dynamic sql and either use Parameters or ideally make it a proc...

sql = "INSERT INTO tblStipen(EmployeeId, StipenTypeID, Units, Amount,
AddHours, PayrollID) Values
@EmployeeId, @StipType, @Units, @Amount, @Hours, @PayrollID)";

cmd.Parameters.Clear();
cmd.Parameters.Add("@EmployeeId", SqlDbType.Employee'sType ie Int,
Length).Value = _employeeid;
cmd.Parameters.Add("@StipType", SqlDbType.SmallInt).Value = 3;

etc.

I'm guessing when you say it's gettting executed 3 times it's based on the return value being 3? First verify that it's not actually being inserted 3
times by querying the DB. Since it's a simple insert, I can't see how 3
records could be getting inserted, but you may have a trigger or
something that's causing this number to behave funny.

First let me know if it's the number 3 as a return value that's the

issue or
if there are verifiably 3 records getting inserted.

HTH,

Bill
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
I know this sounds strange but I am at a loss. I am calling a simple
funtion that opens a connection to a SQL Server 2000 database and
executes an Insert Statement.

private void AddMinimunWageStipen(string payrollid,double amount)
{

System.Data.SqlClient.SqlConnection cn = null;
System.Data.SqlClient.SqlCommand cm = null;

string sql = "INSERT INTO tblStipen (EmployeeID, StipenTypeID, Units,
Amount, AddHours, PayrollID) ";
sql+= "VALUES (" + _employeeid + ", 3, 1, " + amount + ", 0, " +

payrollid
+ ")";

try
{
cn = new
System.Data.SqlClient.SqlConnection(PayrollSystem. Data.SQLSERVER);
cn.Open();
cm = new System.Data.SqlClient.SqlCommand(sql,cn);
int flag = cm.ExecuteNonQuery();
cn.Close();
}
catch(Exception ex)
{
throw ex;
}
finally
{
cm.Dispose();
cn.Dispose();
}

}

When I run this code in the debugger and step through this funtion, when I
execute the line "int flag = cm.ExecuteNonQuery();" the Insert Statement gets exectued 3 times. So I have the same record in the database 3 times?? And I know the the code is only being exectuted once. The function isn't being called 3 times with the same parameters being passed in. I am also not doing any special threading.

If I run the program without stopping in this function the code behave

like
it should and the record only gets inserted once.

Is there something I am missing? Has anyone else ran into this

problem?
Thanks in advance,

Mike R



Nov 15 '05 #5
I've seen similar with other sqlclient classes when using the debugger. My
theory is that the debugger queries the object for information and causes it
to execute multiple times.

Try closing all watch windows, the locals window, and the autos window. I'd
even close the "this" window. Then step through and see what happens.
I've done this and eliminated the problem -- but it kind of defeats the
purpose of running in the debugger.
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
I know this sounds strange but I am at a loss. I am calling a simple
funtion that opens a connection to a SQL Server 2000 database and executes
an Insert Statement.

private void AddMinimunWageStipen(string payrollid,double amount)
{

System.Data.SqlClient.SqlConnection cn = null;
System.Data.SqlClient.SqlCommand cm = null;

string sql = "INSERT INTO tblStipen (EmployeeID, StipenTypeID, Units,
Amount, AddHours, PayrollID) ";
sql+= "VALUES (" + _employeeid + ", 3, 1, " + amount + ", 0, " + payrollid + ")";

try
{
cn = new
System.Data.SqlClient.SqlConnection(PayrollSystem. Data.SQLSERVER);
cn.Open();
cm = new System.Data.SqlClient.SqlCommand(sql,cn);
int flag = cm.ExecuteNonQuery();
cn.Close();
}
catch(Exception ex)
{
throw ex;
}
finally
{
cm.Dispose();
cn.Dispose();
}

}

When I run this code in the debugger and step through this funtion, when I
execute the line "int flag = cm.ExecuteNonQuery();" the Insert Statement
gets exectued 3 times. So I have the same record in the database 3 times??
And I know the the code is only being exectuted once. The function isn't
being called 3 times with the same parameters being passed in. I am also
not doing any special threading.

If I run the program without stopping in this function the code behave like it should and the record only gets inserted once.

Is there something I am missing? Has anyone else ran into this problem?

Thanks in advance,

Mike R

Nov 15 '05 #6
This is what I suspect is the problem.
"Sushil Chordia" <su*****@online.microsoft.com> wrote in message
news:er**************@TK2MSFTNGP12.phx.gbl...
Mike,
AFAIK, i have never seen this happen.
Is it possible that you have the expression "cm.ExecuteNonQuery();" in your watch list? This might cause the expression to be called and there by
inserting rows.
--
This posting is provided "AS IS" with no warranties, and confers no rights. "Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:ez**************@TK2MSFTNGP09.phx.gbl...
Well the way I can tell that the Insert statement is being executed 3

times
is because there are 3 records in the database.

1 is being returned by the ExecuteNonQuery command. No there aren't any
triggers running.
Mike

"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:eq**************@TK2MSFTNGP10.phx.gbl...
Mike:

How do you know it's being executed 3 times? If this isn't in a loop and there's not multiple threads running it, I would be the house that it's
not
really executing three times.

As an aside, I'd really lose the dynamic sql and either use Parameters or ideally make it a proc...

sql = "INSERT INTO tblStipen(EmployeeId, StipenTypeID, Units, Amount,
AddHours, PayrollID) Values
@EmployeeId, @StipType, @Units, @Amount, @Hours, @PayrollID)";

cmd.Parameters.Clear();
cmd.Parameters.Add("@EmployeeId", SqlDbType.Employee'sType ie Int,
Length).Value = _employeeid;
cmd.Parameters.Add("@StipType", SqlDbType.SmallInt).Value = 3;

etc.

I'm guessing when you say it's gettting executed 3 times it's based on the return value being 3? First verify that it's not actually being inserted
3
times by querying the DB. Since it's a simple insert, I can't see how
3 records could be getting inserted, but you may have a trigger or

something that's causing this number to behave funny.

First let me know if it's the number 3 as a return value that's the issue
or
if there are verifiably 3 records getting inserted.

HTH,

Bill
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
> I know this sounds strange but I am at a loss. I am calling a simple > funtion that opens a connection to a SQL Server 2000 database and

executes
> an Insert Statement.
>
> private void AddMinimunWageStipen(string payrollid,double amount)
> {
>
> System.Data.SqlClient.SqlConnection cn = null;
> System.Data.SqlClient.SqlCommand cm = null;
>
> string sql = "INSERT INTO tblStipen (EmployeeID, StipenTypeID, Units, > Amount, AddHours, PayrollID) ";
> sql+= "VALUES (" + _employeeid + ", 3, 1, " + amount + ", 0, " +
payrollid
> + ")";
>
> try
> {
> cn = new
> System.Data.SqlClient.SqlConnection(PayrollSystem. Data.SQLSERVER);
> cn.Open();
> cm = new System.Data.SqlClient.SqlCommand(sql,cn);
> int flag = cm.ExecuteNonQuery();
> cn.Close();
> }
> catch(Exception ex)
> {
> throw ex;
> }
> finally
> {
> cm.Dispose();
> cn.Dispose();
> }
>
> }
>
> When I run this code in the debugger and step through this funtion,

when
I
> execute the line "int flag = cm.ExecuteNonQuery();" the Insert

Statement
> gets exectued 3 times. So I have the same record in the database 3

times??
> And I know the the code is only being exectuted once. The function

isn't
> being called 3 times with the same parameters being passed in. I am also
> not doing any special threading.
>
> If I run the program without stopping in this function the code

behave like
> it should and the record only gets inserted once.
>
> Is there something I am missing? Has anyone else ran into this

problem? >
> Thanks in advance,
>
> Mike R
>
>



Nov 15 '05 #7
If 1 is being returned as the records affected, I'd be the house that the
insert isn't executing 3 times...I think the watch list issue has already
been brought up, but if all else fails, put a PK on the table temporarily
and see when you get a violation. you may also want to change where you
call this from and see if it happens repeatedly even if you call it from
another proc.
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:ez**************@TK2MSFTNGP09.phx.gbl...
Well the way I can tell that the Insert statement is being executed 3 times is because there are 3 records in the database.

1 is being returned by the ExecuteNonQuery command. No there aren't any
triggers running.
Mike

"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:eq**************@TK2MSFTNGP10.phx.gbl...
Mike:

How do you know it's being executed 3 times? If this isn't in a loop and
there's not multiple threads running it, I would be the house that it's not
really executing three times.

As an aside, I'd really lose the dynamic sql and either use Parameters or ideally make it a proc...

sql = "INSERT INTO tblStipen(EmployeeId, StipenTypeID, Units, Amount,
AddHours, PayrollID) Values
@EmployeeId, @StipType, @Units, @Amount, @Hours, @PayrollID)";

cmd.Parameters.Clear();
cmd.Parameters.Add("@EmployeeId", SqlDbType.Employee'sType ie Int,
Length).Value = _employeeid;
cmd.Parameters.Add("@StipType", SqlDbType.SmallInt).Value = 3;

etc.

I'm guessing when you say it's gettting executed 3 times it's based on the return value being 3? First verify that it's not actually being inserted 3
times by querying the DB. Since it's a simple insert, I can't see how 3
records could be getting inserted, but you may have a trigger or
something that's causing this number to behave funny.

First let me know if it's the number 3 as a return value that's the

issue or
if there are verifiably 3 records getting inserted.

HTH,

Bill
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
I know this sounds strange but I am at a loss. I am calling a simple
funtion that opens a connection to a SQL Server 2000 database and
executes an Insert Statement.

private void AddMinimunWageStipen(string payrollid,double amount)
{

System.Data.SqlClient.SqlConnection cn = null;
System.Data.SqlClient.SqlCommand cm = null;

string sql = "INSERT INTO tblStipen (EmployeeID, StipenTypeID, Units,
Amount, AddHours, PayrollID) ";
sql+= "VALUES (" + _employeeid + ", 3, 1, " + amount + ", 0, " +

payrollid
+ ")";

try
{
cn = new
System.Data.SqlClient.SqlConnection(PayrollSystem. Data.SQLSERVER);
cn.Open();
cm = new System.Data.SqlClient.SqlCommand(sql,cn);
int flag = cm.ExecuteNonQuery();
cn.Close();
}
catch(Exception ex)
{
throw ex;
}
finally
{
cm.Dispose();
cn.Dispose();
}

}

When I run this code in the debugger and step through this funtion, when I
execute the line "int flag = cm.ExecuteNonQuery();" the Insert Statement gets exectued 3 times. So I have the same record in the database 3 times?? And I know the the code is only being exectuted once. The function isn't being called 3 times with the same parameters being passed in. I am also not doing any special threading.

If I run the program without stopping in this function the code behave

like
it should and the record only gets inserted once.

Is there something I am missing? Has anyone else ran into this

problem?
Thanks in advance,

Mike R



Nov 15 '05 #8
It's got to be executing 3 times because the OP says that when he checks the
db, there are 3 records inserted.
"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:OH**************@TK2MSFTNGP10.phx.gbl...
If 1 is being returned as the records affected, I'd be the house that the
insert isn't executing 3 times...I think the watch list issue has already
been brought up, but if all else fails, put a PK on the table temporarily
and see when you get a violation. you may also want to change where you
call this from and see if it happens repeatedly even if you call it from
another proc.
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:ez**************@TK2MSFTNGP09.phx.gbl...
Well the way I can tell that the Insert statement is being executed 3

times
is because there are 3 records in the database.

1 is being returned by the ExecuteNonQuery command. No there aren't any
triggers running.
Mike

"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:eq**************@TK2MSFTNGP10.phx.gbl...
Mike:

How do you know it's being executed 3 times? If this isn't in a loop and there's not multiple threads running it, I would be the house that it's
not
really executing three times.

As an aside, I'd really lose the dynamic sql and either use Parameters or ideally make it a proc...

sql = "INSERT INTO tblStipen(EmployeeId, StipenTypeID, Units, Amount,
AddHours, PayrollID) Values
@EmployeeId, @StipType, @Units, @Amount, @Hours, @PayrollID)";

cmd.Parameters.Clear();
cmd.Parameters.Add("@EmployeeId", SqlDbType.Employee'sType ie Int,
Length).Value = _employeeid;
cmd.Parameters.Add("@StipType", SqlDbType.SmallInt).Value = 3;

etc.

I'm guessing when you say it's gettting executed 3 times it's based on the return value being 3? First verify that it's not actually being inserted
3
times by querying the DB. Since it's a simple insert, I can't see how
3 records could be getting inserted, but you may have a trigger or

something that's causing this number to behave funny.

First let me know if it's the number 3 as a return value that's the issue
or
if there are verifiably 3 records getting inserted.

HTH,

Bill
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
> I know this sounds strange but I am at a loss. I am calling a simple > funtion that opens a connection to a SQL Server 2000 database and

executes
> an Insert Statement.
>
> private void AddMinimunWageStipen(string payrollid,double amount)
> {
>
> System.Data.SqlClient.SqlConnection cn = null;
> System.Data.SqlClient.SqlCommand cm = null;
>
> string sql = "INSERT INTO tblStipen (EmployeeID, StipenTypeID, Units, > Amount, AddHours, PayrollID) ";
> sql+= "VALUES (" + _employeeid + ", 3, 1, " + amount + ", 0, " +
payrollid
> + ")";
>
> try
> {
> cn = new
> System.Data.SqlClient.SqlConnection(PayrollSystem. Data.SQLSERVER);
> cn.Open();
> cm = new System.Data.SqlClient.SqlCommand(sql,cn);
> int flag = cm.ExecuteNonQuery();
> cn.Close();
> }
> catch(Exception ex)
> {
> throw ex;
> }
> finally
> {
> cm.Dispose();
> cn.Dispose();
> }
>
> }
>
> When I run this code in the debugger and step through this funtion,

when
I
> execute the line "int flag = cm.ExecuteNonQuery();" the Insert

Statement
> gets exectued 3 times. So I have the same record in the database 3

times??
> And I know the the code is only being exectuted once. The function

isn't
> being called 3 times with the same parameters being passed in. I am also
> not doing any special threading.
>
> If I run the program without stopping in this function the code

behave like
> it should and the record only gets inserted once.
>
> Is there something I am missing? Has anyone else ran into this

problem? >
> Thanks in advance,
>
> Mike R
>
>



Nov 15 '05 #9
I can very clearly see 3 records in the database. And the table does have a
primary key and and it set as an Identity field. Each record is given a
unique ID.

Ive been just stepping over the methods where I am doing the inserts and it
hasn't happen since. The debugger must be executing the code several times
to get the value the ExecuteNonQuery is returning to display in the various
debug windows I have open. Seems like a giant bug that is incredibly
annoying.

Everything has been working great since I stopped stepping through the
method.

"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:OH**************@TK2MSFTNGP10.phx.gbl...
If 1 is being returned as the records affected, I'd be the house that the
insert isn't executing 3 times...I think the watch list issue has already
been brought up, but if all else fails, put a PK on the table temporarily
and see when you get a violation. you may also want to change where you
call this from and see if it happens repeatedly even if you call it from
another proc.
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:ez**************@TK2MSFTNGP09.phx.gbl...
Well the way I can tell that the Insert statement is being executed 3

times
is because there are 3 records in the database.

1 is being returned by the ExecuteNonQuery command. No there aren't any
triggers running.
Mike

"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:eq**************@TK2MSFTNGP10.phx.gbl...
Mike:

How do you know it's being executed 3 times? If this isn't in a loop and there's not multiple threads running it, I would be the house that it's
not
really executing three times.

As an aside, I'd really lose the dynamic sql and either use Parameters or ideally make it a proc...

sql = "INSERT INTO tblStipen(EmployeeId, StipenTypeID, Units, Amount,
AddHours, PayrollID) Values
@EmployeeId, @StipType, @Units, @Amount, @Hours, @PayrollID)";

cmd.Parameters.Clear();
cmd.Parameters.Add("@EmployeeId", SqlDbType.Employee'sType ie Int,
Length).Value = _employeeid;
cmd.Parameters.Add("@StipType", SqlDbType.SmallInt).Value = 3;

etc.

I'm guessing when you say it's gettting executed 3 times it's based on the return value being 3? First verify that it's not actually being inserted
3
times by querying the DB. Since it's a simple insert, I can't see how
3 records could be getting inserted, but you may have a trigger or

something that's causing this number to behave funny.

First let me know if it's the number 3 as a return value that's the issue
or
if there are verifiably 3 records getting inserted.

HTH,

Bill
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
> I know this sounds strange but I am at a loss. I am calling a simple > funtion that opens a connection to a SQL Server 2000 database and

executes
> an Insert Statement.
>
> private void AddMinimunWageStipen(string payrollid,double amount)
> {
>
> System.Data.SqlClient.SqlConnection cn = null;
> System.Data.SqlClient.SqlCommand cm = null;
>
> string sql = "INSERT INTO tblStipen (EmployeeID, StipenTypeID, Units, > Amount, AddHours, PayrollID) ";
> sql+= "VALUES (" + _employeeid + ", 3, 1, " + amount + ", 0, " +
payrollid
> + ")";
>
> try
> {
> cn = new
> System.Data.SqlClient.SqlConnection(PayrollSystem. Data.SQLSERVER);
> cn.Open();
> cm = new System.Data.SqlClient.SqlCommand(sql,cn);
> int flag = cm.ExecuteNonQuery();
> cn.Close();
> }
> catch(Exception ex)
> {
> throw ex;
> }
> finally
> {
> cm.Dispose();
> cn.Dispose();
> }
>
> }
>
> When I run this code in the debugger and step through this funtion,

when
I
> execute the line "int flag = cm.ExecuteNonQuery();" the Insert

Statement
> gets exectued 3 times. So I have the same record in the database 3

times??
> And I know the the code is only being exectuted once. The function

isn't
> being called 3 times with the same parameters being passed in. I am also
> not doing any special threading.
>
> If I run the program without stopping in this function the code

behave like
> it should and the record only gets inserted once.
>
> Is there something I am missing? Has anyone else ran into this

problem? >
> Thanks in advance,
>
> Mike R
>
>



Nov 15 '05 #10
Yeah Mike, that's what I thought. It seems that the debugger is a bit
buggy!
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:uM**************@tk2msftngp13.phx.gbl...
I can very clearly see 3 records in the database. And the table does have a primary key and and it set as an Identity field. Each record is given a
unique ID.

Ive been just stepping over the methods where I am doing the inserts and it hasn't happen since. The debugger must be executing the code several times to get the value the ExecuteNonQuery is returning to display in the various debug windows I have open. Seems like a giant bug that is incredibly
annoying.

Everything has been working great since I stopped stepping through the
method.

"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:OH**************@TK2MSFTNGP10.phx.gbl...
If 1 is being returned as the records affected, I'd be the house that the
insert isn't executing 3 times...I think the watch list issue has already been brought up, but if all else fails, put a PK on the table temporarily and see when you get a violation. you may also want to change where you
call this from and see if it happens repeatedly even if you call it from
another proc.
"Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
news:ez**************@TK2MSFTNGP09.phx.gbl...
Well the way I can tell that the Insert statement is being executed 3 times
is because there are 3 records in the database.

1 is being returned by the ExecuteNonQuery command. No there aren't any triggers running.
Mike

"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:eq**************@TK2MSFTNGP10.phx.gbl...
> Mike:
>
> How do you know it's being executed 3 times? If this isn't in a loop
and
> there's not multiple threads running it, I would be the house that it's not
> really executing three times.
>
> As an aside, I'd really lose the dynamic sql and either use
Parameters or
> ideally make it a proc...
>
> sql = "INSERT INTO tblStipen(EmployeeId, StipenTypeID, Units,
Amount, > AddHours, PayrollID) Values
> @EmployeeId, @StipType, @Units, @Amount, @Hours, @PayrollID)";
>
> cmd.Parameters.Clear();
> cmd.Parameters.Add("@EmployeeId", SqlDbType.Employee'sType ie Int,
> Length).Value = _employeeid;
> cmd.Parameters.Add("@StipType", SqlDbType.SmallInt).Value = 3;
>
> etc.
>
> I'm guessing when you say it's gettting executed 3 times it's based on the
> return value being 3? First verify that it's not actually being

inserted
3
> times by querying the DB. Since it's a simple insert, I can't see
how 3 > records could be getting inserted, but you may have a trigger or

something
> that's causing this number to behave funny.
>
> First let me know if it's the number 3 as a return value that's the

issue
or
> if there are verifiably 3 records getting inserted.
>
> HTH,
>
> Bill
> "Mike" <mr********@NOSPAMATALLcalibrus.com> wrote in message
> news:uH**************@TK2MSFTNGP11.phx.gbl...
> > I know this sounds strange but I am at a loss. I am calling a simple > > funtion that opens a connection to a SQL Server 2000 database and
executes
> > an Insert Statement.
> >
> > private void AddMinimunWageStipen(string payrollid,double amount)
> > {
> >
> > System.Data.SqlClient.SqlConnection cn = null;
> > System.Data.SqlClient.SqlCommand cm = null;
> >
> > string sql = "INSERT INTO tblStipen (EmployeeID, StipenTypeID, Units, > > Amount, AddHours, PayrollID) ";
> > sql+= "VALUES (" + _employeeid + ", 3, 1, " + amount + ", 0, " +
> payrollid
> > + ")";
> >
> > try
> > {
> > cn = new
> > System.Data.SqlClient.SqlConnection(PayrollSystem. Data.SQLSERVER);
> > cn.Open();
> > cm = new System.Data.SqlClient.SqlCommand(sql,cn);
> > int flag = cm.ExecuteNonQuery();
> > cn.Close();
> > }
> > catch(Exception ex)
> > {
> > throw ex;
> > }
> > finally
> > {
> > cm.Dispose();
> > cn.Dispose();
> > }
> >
> > }
> >
> > When I run this code in the debugger and step through this
funtion, when
I
> > execute the line "int flag = cm.ExecuteNonQuery();" the Insert
Statement
> > gets exectued 3 times. So I have the same record in the database 3
times??
> > And I know the the code is only being exectuted once. The

function isn't
> > being called 3 times with the same parameters being passed in. I

am also
> > not doing any special threading.
> >
> > If I run the program without stopping in this function the code behave > like
> > it should and the record only gets inserted once.
> >
> > Is there something I am missing? Has anyone else ran into this

problem?
> >
> > Thanks in advance,
> >
> > Mike R
> >
> >
>
>



Nov 15 '05 #11

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

Similar topics

10
by: Heiko Pliefke | last post by:
Hi NG! I wrote a stored procedure which at first deletes 100.000 rows and then inserts 100.000 new rows. There is a huge difference between executing this SP in the query-analyzer (runtime...
10
by: florian | last post by:
Hi, we have a contention problem because of an application which tries to insert a duplicate row in a table with primary key. This insert fails of course but the locks are not released within...
9
by: Lloyd Sheen | last post by:
In an attempt to reformat the web page I moved controls into a frame and then when that did no good I replaced then on the aspx in the same order as before. When I did the CTL-X the handlers lost...
2
by: Steve Remer | last post by:
OK, I think I understand session state pretty well. I've done additional research on Google over the last few days to fill in any holes. To begin with, I'm using StateServer and not InProc for...
3
by: Martin B | last post by:
Hallo! I'm working with C# .NET 2.0, implementing Client/Server Applications which are connecting via Network to SQL-Server or Oracle Databases. To stay independent from the underlaying Database...
2
by: jzogg7272 | last post by:
In my code I am executing a stored procedure to do a single row insert. I check the return value of the execution and I am getting -1, whereas a few weeks ago it was returning 0. Actually, I found...
9
by: Michael.Suarez | last post by:
Suppose I have a program that prompts you with a dialogbox to enter a password. If you get the password correct, it allows you into the program, else it kills the program. Suppose that when I...
22
by: b_r | last post by:
Hi, I'm trying to make a simple operation (insert into DB) in VB 2005 and SQL Server. The code is as follows: Dim sConnectionString As String = _ "Data...
0
by: wugon.net | last post by:
Hi , Anyone know how to monitor db2 trigger activity ? We suffer some trigger issue today and we try to monitor trigger's behavior use event monitor and db2audit, but both tools can not get...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...

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.