Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExecuteScalarOrDefault: Sequence contains no matching element #97

Open
Deantwo opened this issue Mar 5, 2024 · 1 comment · Fixed by Deantwo/tik4net#2 · May be fixed by #98
Open

ExecuteScalarOrDefault: Sequence contains no matching element #97

Deantwo opened this issue Mar 5, 2024 · 1 comment · Fixed by Deantwo/tik4net#2 · May be fixed by #98

Comments

@Deantwo
Copy link
Contributor

Deantwo commented Mar 5, 2024

When calling /snmp/print with a ExecuteScalarOrDefault method and the /snmp location value is empty string, the API throws an exception.

System.InvalidOperationException: Sequence contains no matching element
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
   at tik4net.Api.ApiCommand.ExecuteScalarInternal(String target, Boolean allowReturnDefault, String defaultValue)
   at tik4net.Api.ApiCommand.ExecuteScalarOrDefault()
   at ...

According to the stacktrace, it looks like it is happening in one of the .Single calls in:

private string ExecuteScalarInternal(string target, bool allowReturnDefault, string defaultValue = null)

It probably means that the code isn't checking for empty string output correctly and or should be using SingleOrDefault instead of Single when allowReturnDefault is true.

RouterOS version: 7.9.2
MikroTik config:

/snmp set location=""

Example code:

TikConnection con = ConnectionFactory.OpenConnection(Type.Api, _host, _username, _password);
ITikCommand cmd = conn.CreateCommand("/snmp/print");
System.Diagnostics.Debug.WriteLine("reading SNMP Location");
string address = cmd.ExecuteScalarOrDefault("location");
System.Diagnostics.Debug.WriteLine("SNMP Location read");
@Deantwo
Copy link
Contributor Author

Deantwo commented Mar 6, 2024

>/snmp/print
>=.proplist=location
<!re
<=location=
<
<!done
<

Deantwo added a commit to Deantwo/tik4net that referenced this issue Mar 6, 2024
Changed RegEx to allow for sentences with empty values.
Fixes danikf#97
@Deantwo Deantwo linked a pull request Mar 6, 2024 that will close this issue
@Deantwo Deantwo reopened this Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant