- 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
2
votes
3 answers
C# ADO.NET - return sum of one columns in table
In my table in databse I have a column called "Cena" with the type of Integer. I need to SUM these columns to count the whole "Cena" of the table.
To do it I'm using SqlCommand and the method ExecuteScalar().
string sqlString = string.Format("Select…

CSharpBeginner
- 1,625
- 5
- 22
- 36
2
votes
3 answers
C# SQL Aggregate ExecuteScalar Return Query
An SQL aggregate function counting number of name enteries in DB.
string cnnStr = ConfigurationManager.ConnectionStrings["LGFConnectionString"].ConnectionString;
string mySQL = "SELECT COUNT(*) FROM " + which.table + " WHERE " + which.column + " =…

deDogs
- 739
- 1
- 8
- 24
1
vote
3 answers
SqlCommand.ExecuteReader and getting past the first row
I'm fairly new to ASP.NET and as a self-chosen assignment I am to create a simple blog. It's very nearly finished, but I've run into a problem. As you can see from the code I am first using a query to return the amount of blog entries from my SQL…

Siken
- 13
- 1
- 1
- 3
1
vote
1 answer
Looking for the best way to use ExecuteScalar()
This code works. It's based on some code I found on the internet.
Can you tell me if the coding is the best way to get a scalar value and if there is a better way can you show coding samples?
Dim objParentNameFound As Object
TextBoxParentsName.Text…

Emad-ud-deen
- 4,734
- 21
- 87
- 152
1
vote
2 answers
Executescalar query code issue
Im trying to return the member id in this query below. If i run the query as just as a query i get 20 but when i exectute the code it returns a zero. What am i doing wrong here?
public int GetMemberID(string guid)
{
string…

CsharpBeginner
- 1,753
- 8
- 38
- 68
1
vote
2 answers
how to count the number of records with ExecuteScalar if more than one then cannot insert into in the MS Access database on vb.net
I tried with the code below it doesn't work, is there something wrong with my code, please guide me.
or if there is another solution
Thanks
Private Sub Btnsave_Click(sender As Object, e As EventArgs) Handles Btnsave.Click
If txtWCSKILL.Text = ""…

roy
- 693
- 2
- 11
1
vote
1 answer
The first item in a list-expansion cannot be null
I am getting the error for The first item in a list-expansion cannot be null in the below mentioned code statement-
count += await connection.ExecuteScalarAsync(sqlCount, parameters);
Below are the error details-
System.NotSupportedException…

Anonymous
- 27
- 3
1
vote
4 answers
ExecuteScalar returns 0 executing a stored procedure
I'm refactoring a C# program that calls a stored procedure that ends with:
SELECT @ResultCode AS ResultCode
The C# code looks like this:
SqlDbCommand.CommandType = System.Data.CommandType.StoredProcedure;
SqlDbCommand.CommandText =…

systempuntoout
- 71,966
- 47
- 171
- 241
1
vote
0 answers
ExecuteScalar function fails. Must declare the scalar variable '@DeviceId'
I have a controller that receives a list of objects. First it checks if any of these objects already exists in the table by using ExecuteScalar. If rows already exist, we run the Update stored procedure. If not, we run the Insert stored procedure.…

Gabriel
- 346
- 5
- 24
1
vote
3 answers
Why am I getting "Specified Cast is Not Valid" casting an int return value to int?
I've got this code:
private int GetCombinedRankRank(int combinedRankValue)
{
try
{
conn = new MySqlConnection(connstr);
conn.Open();
try
{
DataTable dtStates = new DataTable();
…

B. Clay Shannon-B. Crow Raven
- 8,547
- 144
- 472
- 862
1
vote
4 answers
SqlCommand.ExecuteScalar - specify a particular data item
I have a Stored Procedure which returns 10 columns of data. Using cmd.ExecuteScalar() returns the value of the 1st column from the 1st record.
How can I change this so that I can return different columns, by specifying their alias/dataitem name?
I…

Curtis
- 101,612
- 66
- 270
- 352
1
vote
2 answers
Any chance of failing one query while using two queries in ExecuteScalar
I have written a query which is a composite query like one update query and one insert query is there.
I am using ExecuteScalar.
Is there a possibility that one query will fail and other will be succeeded.
It will be a whole failure or partial…

Deviprasad Das
- 4,203
- 7
- 36
- 51
1
vote
2 answers
npgsql executescalar() allways returns nothing
I'm using npgsql as a nuget package in visual studio 2017 with visual basic.
Various commands do work very well but an ExecuteScalar allways returns 'nothing' although it should give a result.
The command looks like this:
Dim ser As Integer
…

Rainer
- 21
- 4
1
vote
1 answer
How to handle ExcuteScalar exceptions on Null values?
I have a problem, this works if the value returned isn't null, however I get a System.NullreferenceException when the return is null. I have tried many things but cant seem to crack it. Any help here would be appreciated.
string HwPlts = "select…

MICHAEL
- 43
- 9
1
vote
0 answers
Display .sql output from OracleConnection C#
There's simple .sql file like this:-
helloworld.sql
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
I want to call the file in C# console app, catch and display the output in console. Supposedly,…

HNA
- 107
- 2
- 14