2

Is it possible in C# / ASP.NET to know if the ExecuteNonQuery inserted a record or not?

I am checking to make sure the email address does not exist in the table using a subquery.

Is there a way to know if an Insert was made in ASP.NET?

CommandPrizeEmails.Parameters.Add("@Email", SqlDbType.VarChar, 50);
CommandPrizeEmails.Parameters.Add("@DateToday", SqlDbType.DateTime);

CommandPrizeEmails.Parameters["@Email"].Value = txtEmail.Text;
CommandPrizeEmails.Parameters["@DateToday"].Value = DateTime.Now;

CommandPrizeEmails.ExecuteNonQuery();

//int newID = (int)CommandPrizeEmails.ExecuteScalar();

//CommandPrizeEmails.ExecuteNonQuery();
//if (newID >= 1) {
//    divSuccesfulEntry.Visible = true;
//} else {
//    divRepeatEntry.Visible = true;
//}
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
JoJo
  • 4,643
  • 9
  • 42
  • 65
  • What is wrong with the `ExecuteScalar()`? If i'm not mistaken, it should return the number of rows affected by the query. – Shimrod Aug 28 '13 at 13:16

3 Answers3

1

You can get the rows affected in return to verify the process.

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.

When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of

rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

http://blogs.msdn.com/b/spike/archive/2009/01/27/sqlcommand-executenonquery-returns-1-when-doing-insert-update-delete.aspx

Neha
  • 2,933
  • 3
  • 19
  • 23
  • Does `int retVal = CommandPrizeEmails.ExecuteNonQuery();` do BOTH an Insert (if possible) AND set a `retVal`? ... My Insert is not working for a fresh email addie. – JoJo Aug 28 '13 at 13:39
  • yeah... if your sql query works fine it will insert & return retVal the no of row effected. And you can check retVal for finding whether it works or not – Neha Aug 28 '13 at 13:45
  • Thank you but I must be doing something wrong. I am not getting an Insert. Does this REQUIRE I use a SP? I really don't need a SP here.. Thanks. – JoJo Aug 28 '13 at 13:55
  • For insert you have to either use sp or any direct query depends.. nothing wrong to use sp. First try your SP is working in sql directly excute there. – Neha Aug 28 '13 at 13:56
  • I think there is something wrong with my subquery. Do you know what it may be? `INSERT INTO Emails (email, insertDate) VALUES (@Email, @DateToday) WHERE NOT EXISTS (SELECT Emails.email FROM Emails WHERE Emails.email = @Email);"` – JoJo Aug 28 '13 at 15:39
  • I am using MS SQL SERVER – JoJo Aug 28 '13 at 15:49
1

ExecuteNonQuery Return Boolean value catch value

ex:

bool flg;

flg=cmd.ExecuteNonQuery(); 
if (flg)
{ msgbox("successfully inserted");}
else {msgbox("not inserted");}
senthilkumar2185
  • 2,536
  • 3
  • 22
  • 36
-1
bool flg;

flg=cmd.ExecuteNonQuery(); 
if (flg)
{ MessageBox.Show("successfully inserted");
}
else 
{
MessageBox.Show("not inserted");
}

This gives an error as:

Can not implicitly convert type 'int' to 'bool'

Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
ARJUN
  • 399
  • 1
  • 3
  • 15