-2

If there is no result in the table it should return zero, but currently, it is returning -1. I build my logic that if there is zero do this and if it is not zero mean record exist to do this. I am new to C# and SQL Server. Kindly let me know if there is any mistake in my code.

int UserExist = 0;

using (SqlCommand cmd1 = new SqlCommand(@"select DISTINCT max(billno) AS bill from sale where ([date]='" + bildt.Text + "')  ", con)) {
    UserExist = (int)cmd1.ExecuteNonQuery();

    if (UserExist > 0) { 
        do this
    } else {   
        do some thing   
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MC3
  • 19
  • 6

4 Answers4

1

You need to use ExecuteScalar instead of ExecuteNonQuery.

ExecuteScalar executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.

ExecuteNonQuery executes a Transact-SQL statement against the connection and returns the number of rows affected. For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.

int UserExist = 0;
using (SqlCommand cmd1 = new SqlCommand(@"select DISTINCT max(billno) AS bill from sale where ([date]='" + bildt.Text + "')  ", con))
{
     UserExist = (int)cmd1.ExecuteScalar();
     if (UserExist > 0)
     { 
        do this
     }
     else
     {   
       do some thing   
     }

}

Also some additional points.

  1. Rather then using the Parameters as String, try to use Parameterized Query. For more information read here.
Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41
0

After search on internet i found solution.

int UserExist = 0;
                using (SqlCommand cmd1 = new SqlCommand(@"select COUNT(*) from sale where ([date]=@date)  ", con))
                {   cmd1.Parameters.AddWithValue("date", bildt.Text);
                    UserExist = (int)cmd1.ExecuteScalar();
                   if (UserExist > 0)
                    {using (SqlCommand cmd = new SqlCommand(@"select DISTINCT max(billno) AS bill from sale where ([date]='" + bildt.Text + "')  ", con))
                        { using (SqlDataReader dr = cmd.ExecuteReader())
                            {while (dr.Read())
                                {
                                    String x = dr["bill"].ToString();
                                    txttemp.Text = x;
                                    int a;
                                    int.TryParse(txttemp.Text, out a);
                                    billno.Text = (a + 1).ToString();
                                    txttemp.Text = "";
                                }
                                dr.Close(); dr.Dispose();
                            }
                        }
                        }
                    else {do some thing }
Filburt
  • 17,626
  • 12
  • 64
  • 115
MC3
  • 19
  • 6
  • You should use a Parameter for the second `cmd` as well - especially if you need to cast a TextBox text value to a Date. – Filburt May 14 '16 at 09:26
0

To obtain Single value result from tsql query in C# you need to call ExecuteScalar.

You can read more HERE

Community
  • 1
  • 1
Vahid Farahmandian
  • 6,081
  • 7
  • 42
  • 62
-2

I am not familiar with C#, but I believe that ExecuteNonQuery() is used when you want to do UPDATE, INSERT, or DELETE

I think you need to use ExecuteReader

I hope that helps

Mocas
  • 1,403
  • 13
  • 20