Answered by:
Select Statement

Question
-
User-1499457942 posted
Hi
I have below code . it is returning -1 in norecords
cmd = new SqlCommand("select [Empcode],[EmpCode1] from [Kamla Retail Limited$Customer] where [EmpCode]=@ST or [EmpCode1]=@ST", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ST", txtST.Text.Trim());
int norecords = cmd.ExecuteNonQuery();{
}
Thanks
Thursday, June 28, 2018 9:34 AM
Answers
-
User-1768369891 posted
I have below code . it is returning -1 in norecordsTry this instead of ExecuteNonQuery
cmd = new SqlCommand("select Count(*) from [Kamla Retail Limited$Customer] where [EmpCode]=@ST or [EmpCode1]=@ST", con); cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@ST", txtST.Text.Trim()); int norecords = Convert.ToInt32(cmd.ExecuteScalar());
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, June 28, 2018 10:42 AM
All replies
-
User632428103 posted
Hello,
normally the executeNonQuery isn't for a SELECT statement ... try a executeScalar or anything else
cmd.CommandText = "SELECT COUNT(*) FROM YOURTABLE WHERE .... OR ..."; Int32 count = (Int32) cmd.ExecuteScalar();
Also execute the code into SSMS (sql server management object) and see the result
Thursday, June 28, 2018 10:12 AM -
User-1171043462 posted
You must use ExecuteScalar if you want to return a Scalar value i.e. on Row one Column. in simple words a cell.
Using ADO.Net ExecuteScalar method in ASP.Net with examples in C# and VB.Net
And to understand difference between ExecuteNonQuery and ExceuteScalar, refer
Difference between ExecuteReader ExecuteScalar and ExecuteNonQuery
Thursday, June 28, 2018 10:23 AM -
User-1499457942 posted
Hi
I have written like this but it says Specified cast is not valid
Int32 norecords = (Int32) cmd.ExecuteScalar();
Thanks
Thursday, June 28, 2018 10:27 AM -
User-1171043462 posted
int norecords = Convert.ToInt32(cmd.ExecuteScalar());
Thursday, June 28, 2018 10:30 AM -
User-1768369891 posted
I have below code . it is returning -1 in norecordsTry this instead of ExecuteNonQuery
cmd = new SqlCommand("select Count(*) from [Kamla Retail Limited$Customer] where [EmpCode]=@ST or [EmpCode1]=@ST", con); cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@ST", txtST.Text.Trim()); int norecords = Convert.ToInt32(cmd.ExecuteScalar());
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, June 28, 2018 10:42 AM