Questions tagged [executescalar]

- Executes a query, and returns the first column of the first row in the query-result set. - Additional columns and rows are ignored.

153 questions
33
votes
10 answers

Cannot implicitly convert type 'int?' to 'int'.

I'm getting the error "Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)" on my OrdersPerHour at the return line. I'm not sure why because my C# skills are not that advanced. Any help would be…
MaylorTaylor
  • 4,671
  • 16
  • 47
  • 76
29
votes
3 answers

What is a "Scalar" Query?

I am using LLBLGEN where there is a method to execute a query as a scalar query. Googling gives me a definition for scalar sub-query, are they the same ?
Cemre Mengü
  • 18,062
  • 27
  • 111
  • 169
28
votes
1 answer

ExecuteScalar vs ExecuteNonQuery when returning an identity value

Trying to figure out if it's best to use ExecuteScalar or ExecuteNonQuery if I want to return the identity column of a newly inserted row. I have read this question and I understand the differences there, but when looking over some code I wrote a…
techturtle
  • 2,519
  • 5
  • 28
  • 54
17
votes
7 answers

Int32.TryParse() or (int?)command.ExecuteScalar()

I have a SQL query which returns only one field - an ID of type INT. And I have to use it as integer in C# code. Which way is faster and uses less memory? int id; if(Int32.TryParse(command.ExecuteScalar().ToString(), out id)) { // use…
abatishchev
  • 98,240
  • 88
  • 296
  • 433
11
votes
3 answers

ExecuteScalar returns null or DBNull (development or production server)

I'm trying to add a column to an existing DataRow in C#. Afterwards the column will be filled with a single value from my database. DataRow dr already exists and column "COLNAME" also exists. comTBP is my SqlCommand. dr["COLNAME"] =…
Yoni
  • 325
  • 2
  • 7
  • 15
7
votes
4 answers

ExecuteScalar() returns null altough data was added to DB

I have a code as bellow where I try to insert a data into a table and return the ID (given by auto increment) of the new element. int newEquipmentID = new int(); query = database.ParameterizedQueryString("INSERT INTO Equipment (EquipmentTypeID)…
Guilherme Campos
  • 369
  • 7
  • 20
6
votes
5 answers

How to create array DbParameter[]

The manual says that the ExecuteScalar method should be used like: public T ExecuteScalar( string commandText, CommandType commandType, params DbParameter[] parameters ) But how do I create that array of parameters? I need to provide…
Tys
  • 3,592
  • 9
  • 49
  • 71
6
votes
2 answers

Run multiple commands in one ExecuteScalar in Oracle

I have a batch of sql statements such as ... insert into.... ; insert into.... ; delete .........; etc When i try to execute them against oracle it gives me this error (ORA-00911 Invalid Character) now i can understand that this is because of the…
Kostas Konstantinidis
  • 13,347
  • 10
  • 48
  • 61
6
votes
5 answers

casting ExecuteScalar() result c#

why would this work int collectionCharge = (int)cmdCheck.ExecuteScalar(); but this produces an exception double collectionCharge = (double)cmdCheck.ExecuteScalar(); System.InvalidCastException: Specified cast is not valid. why would it not be…
Stuart
  • 1,544
  • 5
  • 29
  • 45
5
votes
2 answers

Can you use cmd.ExecuteScalar when the sproc uses RETURN @value

Can you use int blah = Convert.ToInt32(cmd.ExecuteScalar()); When the sproc's last statement does: RETURN @value I can only get it to work if it does: SELECT @value Also, this gives me a object null exception: int blah =…
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
5
votes
1 answer

Does ExecuteScalar return immediately after SELECT?

Interesting behaviour has been noticed by me recently. When having MS SQL stored-procedure ran using SqlCommand.ExecuteScalar(), my application seems to be completely unaware to any SQL Errors or PRINTs which appear after SELECT is done. Most…
5
votes
1 answer

Connection Leak in C# DataBase.ExecuteScalar

The following method in a static class gives me a time out exception because the connection pool is maxed out. While in debug mode I looked in sql Management studio and saw there were 150 sleeping processes. I expected the connections to be closed…
Matt
  • 111
  • 2
  • 8
4
votes
3 answers

C# cmd.ExecuteScalar(): "Cannot continue the execution because the session is in the kill state."

Getting a weird exception from ExecuteScalar() that I cannot find any help for on the web: Cannot continue the execution because the session is in the kill state. I'm using SqlConnection/SqlCommand The command is a basic INSERT INTO... with 105…
William Madonna Jr.
  • 242
  • 1
  • 3
  • 12
4
votes
1 answer

Sqlite ExecuteScalar throwing NullReferenceExcpetion

I have a custom written DB provider. When I run my tests, they're breaking on the ExecuteScalar command with a NullReferenceException. What might I be missing here? I've read that some people have a MultiThreading issue, but I don't "think" that's…
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
3
votes
5 answers

Display SQL query result in a label in asp.net

I'm trying to display the SQL query result in a label but it's not showing. This is my code: string result = "SELECT ACTIVE FROM [dbo].[test] WHERE ID = '" + ID.Text + "' "; SqlCommand showresult = new SqlCommand(result, conn); …
Eximus
  • 31
  • 1
  • 2
  • 3
1
2 3
10 11