- Executes a query, and returns the first column of the first row in the query-result set. - Additional columns and rows are ignored.
Questions tagged [executescalar]
153 questions
3
votes
3 answers
output clause returns value but executescalar returns null in c#
I need to get the updated row's first column value. However when I run query Update ClaimDetails set sStatus='False' OUTPUT inserted.slno as Slno where inVoiceNo='******' and sStatus='True'
in Management Studio it returns the right value. But…

mark
- 623
- 3
- 21
- 54
3
votes
3 answers
Execute scalar in T-SQL
What is the best way to do the equivalent of execute scalar in T-SQL? In other words, using SQL, how can I get the first column of the first row of the first result set when I call a stored procedure?
Edit:
Just to clarify, this is only using SQL,…

Nelson Rothermel
- 9,436
- 8
- 62
- 81
3
votes
2 answers
ExecuteScalar() always return NULL
I am trying to return an integer from the database using ExecuteScalar(). However, when I run the query on the database itself, I get the correct answer, and c# gives me a 0 (Null) all the time. I know it returns a null because before i added id =…

Albin Vinoy
- 381
- 1
- 5
- 14
3
votes
1 answer
ExecuteScalar() sometimes returns empty but not null object. Why?
ExecuteScalar() sometimes returns empty object -not null- although the record is exists. When I analyze this object with quickwatch, I see that object.GetType() is equal to DbNull.
I can handle this empty object but I need to know why it is returns…

cihadakt
- 3,054
- 11
- 37
- 59
3
votes
2 answers
C# Scope_Identity Issue
Browsed some answers and doesn't appear to be working for me.
I need the ID field of a table to be returned so I can use it in a different part of the program, I've tried using
Convert.ToInt32(sqlComm.ExecuteScalar());
But no luck, and same…

ZeeeeeV
- 361
- 3
- 11
- 25
3
votes
4 answers
Returning List from Database
I have a simple problem that I have not been able to find an answer to despite much Googling. Perhaps posting this question up will help the next poor soul who looks for the same answer.
I have a query that returns a collection of distinct ID's.
I…

Simon Kiely
- 5,880
- 28
- 94
- 180
3
votes
2 answers
The parameterized query is truncated and parameters are lost
I have a piece of code:
command.Parameters.Clear();
command.Parameters.Add(ownerUserIDParam);
command.Parameters.Add(buddyUserIDParam);
command.Parameters.Add(timestampParam);
command.Parameters.Add(new SqlParameter("@GiftID",…

Алексей Медведев
- 137
- 1
- 1
- 7
2
votes
3 answers
Object cannot be assigned to other types from DBNull
I get minimum number in my database. But when no data in my database I get this error.
System.InvalidCastException: 'The object cannot be assigned to other types from DBNull.'
Code:
SqlCommand cmd = new SqlCommand("SELECT MAX(GidenEvrakSira)…

kozyalcin
- 83
- 10
2
votes
1 answer
C# ExecuteScalar() null COUNT vs SELECT
I noticed some odd behavior and hoped one of the experts could explain the difference. My UI requires an image is unique before presenting it to the user for their task. I store checksums in the database and query those for unique values. I…

JoeBob_OH
- 417
- 3
- 10
2
votes
3 answers
How to use ExecuteScalar() to get a value and give it to your textbox.text?
this is my table:
CREATE TABLE [dbo].[InvoiceTable] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[CodeColumn] NVARCHAR (50) NOT NULL,
[NameColumn] NVARCHAR (50) NOT NULL,
[QTYColumn] INT …

Daniel_Ranjbar
- 161
- 11
2
votes
1 answer
Inserting null value into DateTime field using DBValue.null
Next code is giving me an exception:
cmd.CommandText = @"insert Table ";
cmd.CommandText += @"(StartTime,
EndTime)
values(@StartTime,
@EndTime)
SELECT…

niksrb
- 459
- 7
- 25
2
votes
1 answer
ADO.NET ExecuteScalar() not throwing exception
I am testing ExecuteScalar(), in particular, how it deals with thrown errors in SPs.
When executing a SP with this shape:
throw 50001, '', 1;
select 1;
the exception is raised by ADO.NET (expected behavior), but, if I execute a SP with this shape: …

Mauricio
- 43
- 5
2
votes
3 answers
ADO.NET: Does ExecuteScalar close the connection automatically?
Does ExecuteScalar close the connection automatically?

Rookian
- 19,841
- 28
- 110
- 180
2
votes
2 answers
command.ExecuteScalar is not returning the value I know exists
I have tried various ways of doing this, but none seems to work. The routine keeps returning 0 as result is null.
This is my code:
string strSql = "SELECT [ID] FROM [PurchaseOrder].[dbo].[FMSSupplier] where (ExternalRef = @ExternalRef) and…

Steve Staple
- 2,983
- 9
- 38
- 73
2
votes
2 answers
ExecuteScalar "Specified cast is not valid" exception
The following line of code sometimes results in "Specified cast is not valid" exception:
public static object Select(string sql, OleDbTransaction dbt)
{
try
{
OleDbCommand cmd = new OleDbCommand(sql, lib.dbc, dbt);
object obj =…

Marko
- 45
- 6