Why this query returning -1?

buddy

I'm trying to get the max id of the table category using this code

string maxid = "";
string query = "SELECT MAX(Cat_ID) + 1 FROM Category";

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["JokerCafe"].ConnectionString);

try
{
     conn.Open();
     SqlCommand cmd = new SqlCommand(query, conn);
     maxid = cmd.ExecuteNonQuery().ToString();
}
catch (Exception ex)
{
     MessageBox.Show(ex.Message);
}
finally
{
     conn.Close();
}

return maxid;

I run this query in sql it is returning exact value but when try to execute it from code it returns -1. Kindly guide me what's going wrong with it?

Shell

ExecuteNonQuery() will return the affected row count. For example if you are trying to execute any update statement or delete statement through ExecuteNonQuery() method then it will return the number of affected rows.

But, if you want to fetch a value from specific field then you need to try ExecuteScalar() method. It will return Object type value. Using this method you can fetch only a single value record.

object val = command.ExecuteScalar();
if (val != null)
{
   //Do your stuff here.
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why this query returning -1?

From Dev

Why query is not returning value

From Dev

Why is select() returning 1 but recv() returning 0?

From Dev

Why bind function is returning -1

From Dev

Why is this IndexOf call returning -1?

From Dev

Why is SELECT COUNT (*) returning 1

From Dev

Why is this install command returning 1?

From Dev

why is id(1) is id(1) returning False?

From Dev

why is id(1) is id(1) returning False?

From Dev

Why is my query returning incorrect results?

From Dev

Why is this SQL query not returning the correct rows?

From Dev

Why is this SPARQL query not returning any results?

From Dev

Why is ElasticSearch match query returning all results?

From Dev

Why is this regexp query not returning any results?

From Dev

Why is my ElasticSeach query returning zero document?

From Dev

Why is this query (potentially) only returning posts that are questions?

From Dev

Why is the "where" query in rails returning a different object?

From Dev

Why is this PHP/PDO sqlsrv query not returning results?

From Dev

Why this SQL query returning exact duplicate records?

From Dev

why query is successful but returning 0 rows?

From Dev

PDO query is always returning 1 or true

From Dev

PHP mysqli query returning result "1" on joins

From Dev

Query is always returning -1 in C#

From Dev

sangria graphql query returning 1 element list

From Dev

Why is SQL query returning 0? What will be the actual query?

From Dev

Why Codeigniter's Query Builder is returning this MySQL query wrong and twice?

From Dev

Why is JMH saying that returning 1 is faster than returning 0

From Dev

Why is mktime returning -1 for my std::tm

From Dev

perl: why not returning for case 0 or 1?

Related Related

HotTag

Archive