dotnet / dotnet/docs

Add Db.Null section for nullable parameters

Open
#40,860 0 comments 0 reactions 0 assignees View on GitHub
:watch: Not Triaged dotnet-data/svc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
15h 21m
Merged PRs (30d)
370

Description

### Type of issue

Missing information

### Description

Add info about Db.Null to the parameter documentation for nullable parameters. The following code throws an exception `System.InvalidOperationException: Value must be set.`.
```c#
var command = _db.CreateCommand();
command.CommandText = @"
INSERT INTO users (key, user)
VALUES ($key, $user);
";
command.Parameters.AddWithValue("$key", 1)
command.Parameters.AddWithValue("$user", null)
command.ExecuteNonQuery();
```

Instead of null one must set System.DBNull. Add this to the documentation.
Corrected code:
```c#
var command = _db.CreateCommand();
command.CommandText = @"
INSERT INTO users (key, user)
VALUES ($key, $user);
";
command.Parameters.AddWithValue("$key", 1)
command.Parameters.AddWithValue("$user", System.DBNull.Value)
command.ExecuteNonQuery();
```

Referenced Issue: https://github.com/dotnet/efcore/issues/19775

### Page URL

https://learn.microsoft.com/en-us/dotnet/standard/data/sqlite/parameters

### Content source URL

https://github.com/dotnet/docs/blob/main/docs/standard/data/sqlite/parameters.md

### Document Version Independent Id

3b84b19f-3d58-5a7d-90a7-0e35f6578fcf

### Article author

@ajcvickers

### Metadata

* ID: 0e9c919d-061c-c668-04d2-5f04cfabb587
* Service: **dotnet-data**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.