0

I am using following code to check if record exist.

SqlCommand check_Campaign_Name = new SqlCommand("SELECT * FROM Campaign_Summary  WHERE ([Compaign_Name] = @user) ", conn);
check_Campaign_Name.Parameters.AddWithValue("@user", txtBox_LastClick_Campaign.Text);

int CampaignExist = check_Campaign_Name.ExecuteNonQuery();

richTextBox1.Text = CampaignExist.ToString();

But I am always getting -1 in the integer CampaignExist. Don't know where I am doing wrong.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kamran
  • 4,010
  • 14
  • 60
  • 112
  • Look here http://stackoverflow.com/questions/4269548/executenonquery-for-select-sql-statement-returning-no-rows – Plue Jan 23 '14 at 08:48
  • are you sure your query itself is correct `SELECT * FROM Campaign_Summary WHERE ([Compaign_Name] = @user)`? please test it first substituting real username at `@user` – Tun Zarni Kyaw Jan 23 '14 at 08:49
  • 1
    You wrote `[Compaign_Name]` in the query, shouldn't that be `[Campaign_Name]`? – Henk Jansen Jan 23 '14 at 08:50

5 Answers5

6

ExecuteNonQuery is not supposed to return the number of rows SELECTED, but the number of rows modified by an INSERT/UPDATE/DELETE command. You should use a SqlDataReader and check with its property HasRows or use an aggregate function like COUNT and ExecuteScalar
(Probably the best choiche if you want to just retrieve the number of rows)

SqlCommand check_Campaign_Name = new SqlCommand("SELECT COUNT(*) FROM Campaign_Summary " + 
                                 "WHERE Compaign_Name = @user", conn);
check_Campaign_Name.Parameters.AddWithValue("@user", txtBox_LastClick_Campaign.Text);
int rowCount = Convert.ToInt32(check_Campaign_Name.ExecuteScalar());

However, if you want only to know if the row exists or not, then the COUNT approach is considered less efficient than using the EXISTS statement.

string cmdText = @"IF EXISTS (SELECT Compaign_Name FROM Campaign_Summary 
                   WHERE Compaign_Name = @user)
                   SELECT 1 ELSE SELECT O";
SqlCommand check_Campaign_Name = new SqlCommand(cmdText, conn);
int rowExists = Convert.ToInt32(check_Campaign_Name.ExecuteScalar());

This second approach just allows to know if there are rows that fits the WHERE statement or not.
So it is not exactly like COUNT(*) where you get the exact number of rows.

Steve
  • 213,761
  • 22
  • 232
  • 286
1

Use executescalar method and count on query

SqlCommand check_Campaign_Name = new SqlCommand("SELECT COUNT(1) FROM Campaign_Summary  WHERE ([Compaign_Name] = @user) ", conn);
check_Campaign_Name.Parameters.AddWithValue("@user", txtBox_LastClick_Campaign.Text);

int CampaignExist = Convert.ToInt32(check_Campaign_Name.ExecuteScalar());

richTextBox1.Text = CampaignExist.ToString();
hkutluay
  • 6,794
  • 2
  • 33
  • 53
1

From MSDN,

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.

As your query is a SELECT and not one of the mentioned UPDATE, INSERT or DELETEs, it will always return -1.

Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
1

Try with Execute scalar MSDN

int CampaignExist = Convert.ToInt(check_Campaign_Name.ExecuteScalar());

SqlCommand check_Campaign_Name = new SqlCommand("SELECT COUNT(1) FROM Campaign_Summary  WHERE ([Compaign_Name] = @user) ", conn);
check_Campaign_Name.Parameters.AddWithValue("@user", txtBox_LastClick_Campaign.Text);

int CampaignExist = Convert.ToInt32(check_Campaign_Name.ExecuteScalar());

richTextBox1.Text = CampaignExist.ToString();
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
0

ExecuteNonQuery is not used to retrieve results

You can use one of the following methods:

  1. SqlCommand.ExecuteScalar (I would prefer this)

  2. DataAdapter.Fill

analyticalpicasso
  • 1,993
  • 8
  • 26
  • 45