-1

I have the following code:

let getByIdAsync (connectionString : string) (id : int) = 
        let sql = @"SELECT * FROM [User] WHERE [Id] = @id"
        let args = [ "id" => id ]

        async {
            use connection = new SqlConnection(connectionString)
            do! connection.OpenAsync() |> Async.AwaitTask

            try
                use! reader = connection.ExecuteReaderAsync(sql, args) |> Async.AwaitTask
                return reader |> mapRowsToRecords |> Seq.head
            with
            | e -> return Error e.Message
        }

This code throws MultiExec is not supported by ExecuteReader and I have no idea why. Other queries (like my insert for example) work just fine. What am I doing wrong?

vladek
  • 577
  • 1
  • 4
  • 17
  • Possible [dup](https://stackoverflow.com/questions/52410794/getting-multiexec-is-not-supported-by-executereader-when-running-an-spr). – Dan Guzman Jun 19 '21 at 22:19
  • Nope, I already looked at that one, don't get it, I don't see how I got multiple exec in my case. I only have one query. – vladek Jun 19 '21 at 22:22
  • 1
    I'm not sure this is the issue, but you invoke `OpenAsync` and then ignore the result, meaning that `ExecuteReaderAsync` could, in principle, be started before the connection is (fully) open. You should use `do!` with `OpenAsync`. – Tomas Petricek Jun 19 '21 at 23:26
  • @TomasPetricek Thank you, nice catch! I'm learning F# and was looking for a way to do `await OpenAsync()` so I was trying all sorts of hacks. Unfortunately, this does not solve the issue, but will update the code in the question. – vladek Jun 19 '21 at 23:36

1 Answers1

1

I know what's wrong, missing dict before the dictionary:

let getByIdAsync (connectionString : string) (id : int) = 
        let sql = @"SELECT * FROM [User] WHERE [Id] = @id"
        let args = dict [ "id" => id ] // added dict

        async {
            use connection = new SqlConnection(connectionString)
            do! connection.OpenAsync() |> Async.AwaitTask

            try
                use! reader = connection.ExecuteReaderAsync(sql, args) |> Async.AwaitTask
                return reader |> mapRowsToRecords |> Seq.head
            with
            | e -> return Error e.Message
        }

Check this article for more info.

vladek
  • 577
  • 1
  • 4
  • 17