> This page location: PostgreSQL C# > Call PostgreSQL Functions using C#
> Full Neon documentation index: https://neon.com/docs/llms.txt

# PostgreSQL C#: Call a PostgreSQL Function

**Summary**: in this tutorial, you will learn to call a PostgreSQL function from a C# program using ADO.NET

## How to call a PostgreSQL function in C#

Here are the steps for calling a PostgreSQL function in C#:

First, create a data source that represents the PostgreSQL database:

```cs
await using var dataSource = NpgsqlDataSource.Create(connectionString);
```

Second, create a new `NpgsqlCommand` object from the statement that calls a PostgreSQL function:

```cs
await using var cmd = dataSource.CreateCommand("SELECT fn(?,?)");
```

Third, optionally, bind values to the query's parameters:

```cs
cmd.Parameters.AddWithValue(value1);
cmd.Parameters.AddWithValue(value2);
```

Fourth, execute the function call by calling the `ExecuteReaderAsync()` method of the `NpgsqlCommand` object:

```cs
await using var reader = await cmd.ExecuteReaderAsync();
```

The `ExecuteReaderAsync()` returns a `NpgsqlDataReader` object.

Finally, read the return values of the function by calling the `ReadAsync()` method of the `NpgsqlDataReader` object:

```cs
while (await reader.ReadAsync())
{
    var result = reader.GetInt32(0);
    // ...
}
```

We'll create a new function in the PostgreSQL server and call it from a C# program.

## Creating a PostgreSQL function

First, open a terminal and connect to the `elearning` database using the `ed` user:

```bash
psql -U ed -d elearning
```

It'll prompt you to enter a password for the `ed` user. Input the valid password and press Enter to connect to the PostgreSQL server.

Second, create a stored procedure in PostgreSQL, which enrolls a student in a course and creates a corresponding invoice:

```sql
create function get_student_count(begin_date date, end_date date)
returns int
language plpgsql as $$
declare
   student_count integer;
begin
   select count(*)
   into student_count
   from students
   where registration_date between begin_date and end_date;

   return student_count;
end;
$$;
```

## Calling the PostgreSQL function in C#

The following C# program invokes the `enroll_student_and_create_invoice` function from the PostgreSQL database:

```cs
using Npgsql;

var beginDate = new DateOnly(2024, 5, 10);
var endDate = new DateOnly(2024, 5, 15);

string connectionString = ConfigurationHelper.GetConnectionString("DefaultConnection");

try
{
    await using var dataSource = NpgsqlDataSource.Create(connectionString);

    await using var cmd = dataSource.CreateCommand("SELECT get_student_count($1,$2)");

    cmd.Parameters.AddWithValue(beginDate);
    cmd.Parameters.AddWithValue(endDate);

    await using var reader = await cmd.ExecuteReaderAsync();

    if (await reader.ReadAsync())
    {
        var studentCount = reader.GetInt32(0);

        Console.WriteLine($"Students who registered between {beginDate} and {endDate}: {studentCount} ");
    }

}
catch (NpgsqlException ex)
{
    Console.WriteLine($"Error: {ex.Message}");
}
```

How it works.

First, declare and initialize variables `beginDate` and `endDate` to May 10 2024 and May 15 2024:

```cs
var beginDate = new DateOnly(2024, 5, 10);
var endDate = new DateOnly(2024, 5, 15);
```

Second, get the connection string from the configuration file using the `ConfigurationHelper` class:

```cs
string connectionString = ConfigurationHelper.GetConnectionString("DefaultConnection");
```

Third, create a data source that represents the PostgreSQL database:

```cs
await using var dataSource = NpgsqlDataSource.Create(connectionString);
```

Fourth, create a new `NpgsqlCommand` object that will execute a function call to the `get_student_count()` function:

```cs
await using var cmd = dataSource.CreateCommand("SELECT get_student_count($1,$2)");
```

Notice that `$1` and `$2` are the parameter placeholders you need to bind values when executing the command.

Fifth, bind the `beginDate` and `endDate` to the parameters of the `NpgsqlCommand` object:

```cs
cmd.Parameters.AddWithValue(beginDate);
cmd.Parameters.AddWithValue(endDate);
```

Sixth, execute the command function call:

```cs
await using var reader = await cmd.ExecuteReaderAsync();
```

Seventh, read the student count and display it in the console:

```cs
if (await reader.ReadAsync())
{
    var studentCount = reader.GetInt32(0);
    Console.WriteLine($"Students who registered between {beginDate} and {endDate}: {studentCount} ");
}
```

Finally, display the error message if any exceptions occur:

```cs
// ...
} catch (NpgsqlException ex)
{
    Console.WriteLine($"Error: {ex.Message}");
}
```

## Summary

- Call the `ExecuteReaderAsync()` method of the `NpgsqlCommand` object to execute a call to a PostgreSQL function from a C# program.

---

## Related docs (PostgreSQL C#)

- [Connect to Database Using C#](https://neon.com/postgresql/postgresql-csharp/postgresql-csharp-connect)
- [Call a Stored Procedure using C#](https://neon.com/postgresql/postgresql-csharp/postgresql-csharp-call-a-stored-procedure)
- [Connect to PostgreSQL using C#](https://neon.com/postgresql/postgresql-csharp/postgresql-csharp-connect)
- [Create Tables using C#](https://neon.com/postgresql/postgresql-csharp/postgresql-csharp-create-table)
- [Delete Data from a Table using C#](https://neon.com/postgresql/postgresql-csharp/postgresql-csharp-delete)
- [Import Data from CSV using C#](https://neon.com/postgresql/postgresql-csharp/postgresql-csharp-import-csv-file)
- [Insert Data Into Tables using C#](https://neon.com/postgresql/postgresql-csharp/postgresql-csharp-insert)
- [Select Data in a Table using C#](https://neon.com/postgresql/postgresql-csharp/postgresql-csharp-select)
- [PostgreSQL Transactions in C#](https://neon.com/postgresql/postgresql-csharp/postgresql-csharp-transaction)
- [Update Data in a Table in C#](https://neon.com/postgresql/postgresql-csharp/postgresql-csharp-update)
