# Execute a cypher query using .NET Driver

**URL:** <https://community.neo4j.com/t/execute-a-cypher-query-using-net-driver/61594>\
**Category:** Neo4j Graph Platform\
**Tags:** neo4j-driver, cypher\
**Created:** [March 22, 2023, 1:08am UTC](https://community.neo4j.com/t/execute-a-cypher-query-using-net-driver/61594 "2023-03-22T01:08:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hamed.metalgear](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/hamed.metalgear/32/27543_2.png) [@hamed.metalgear](https://community.neo4j.com/u/hamed.metalgear)\
**Post date:** [March 22, 2023, 1:08am UTC](https://community.neo4j.com/t/execute-a-cypher-query-using-net-driver/61594/1 "2023-03-22T01:08:06Z")

</div>

What is the correct way of executing a Cypher query from .NET?

After reading multiple forums and blog posts, I came to the following approach as "the" correct way.

```auto
var driver = GraphDatabase.Driver(getUri(), AuthTokens.Basic("username", "password"));

using var session = driver.AsyncSession(x => x.WithDefaultAccessMode(AccessMode.Write));

var queryResult = await session.ExecuteWriteAsync(async x =>
{
    IResultCursor cursor = await x.RunAsync(GetQuery());
    return await cursor.ToListAsync();
});

```

This approach works; however, if an error occurs executing the query, the program "halts" with no exceptions thrown.

I tried the following, but even in this case, no exception is caught if an error occurs executing the query.

```auto
var queryResult = await session.ExecuteWriteAsync(async x =>
{
    try
    {
        IResultCursor cursor = await x.RunAsync(GetQuery());
        return await cursor.ToListAsync();
    }
    catch(Exception e)
    { }
});

```

---

<div class="post-metadata">

**Author:** ![hamed.metalgear](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/hamed.metalgear/32/27543_2.png) [@hamed.metalgear](https://community.neo4j.com/u/hamed.metalgear)\
**Post date:** [March 25, 2023, 4:03pm UTC](https://community.neo4j.com/t/execute-a-cypher-query-using-net-driver/61594/2 "2023-03-25T16:03:32Z")

</div>

This is a hard-to-reproduce issue, as in some cases, I get the exception (e.g., if there was an issue with the syntax of the query), but sometimes the program simply halts (waits indefinitely) at executing this. Since there is no error message, I am unsure what causes the issue. **If I repeat executing a query with this approach** , it runs successfully in most cases but halts in a handful of runs with no error. Since the query does not change between the runs, I suppose the issue be related to the driver.
