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

System.ArgumentOutOfRangeException on ".ToString()" of SqlException #677

Closed
BoBiene opened this issue Aug 7, 2020 · 3 comments · Fixed by #698
Closed

System.ArgumentOutOfRangeException on ".ToString()" of SqlException #677

BoBiene opened this issue Aug 7, 2020 · 3 comments · Fixed by #698
Labels
🐛 Bug! Issues that are bugs in the drivers we maintain.

Comments

@BoBiene
Copy link

BoBiene commented Aug 7, 2020

Describe the bug

Unable to call ".ToString()" on SqlException

Exception message:
System.ArgumentOutOfRangeException: "Der Index lag außerhalb des Bereichs. Er darf nicht negativ und kleiner als die Sammlung sein.
Parametername: index"

Stack trace:
 	mscorlib.dll!System.Collections.ArrayList.this[int].get(int index)	Unbekannt
	Microsoft.Data.SqlClient.dll!Microsoft.Data.SqlClient.SqlException.Number.get()	Unbekannt
 	Microsoft.Data.SqlClient.dll!Microsoft.Data.SqlClient.SqlException.ToString()	Unbekannt
 	mscorlib.dll!System.Text.StringBuilder.AppendFormatHelper(System.IFormatProvider provider, string format, System.ParamsArray args)	Unbekannt
 	mscorlib.dll!string.FormatHelper(System.IFormatProvider provider, string format, System.ParamsArray args)	Unbekannt
 	mscorlib.dll!string.Format(string format, object[] args)	Unbekannt

image

@JRahnama
Copy link
Contributor

JRahnama commented Aug 7, 2020

@BoBiene , Thanks for bringing this to our attention. Can you kindly provide us a sample code resulting in this issue that we can start looking into it?

@JRahnama JRahnama added ⏳ Waiting for Customer Issues/PRs waiting for user response/action. ℹ️ Needs more Info Issues that have insufficient information to pursue investigations labels Aug 7, 2020
@cheenamalhotra
Copy link
Member

cheenamalhotra commented Aug 12, 2020

@BoBiene

Just to add context from what I observe, your error message is "The index was out of range. It must not be negative and smaller than the collection." which seems to occur when this is called: https://github.com/dotnet/SqlClient/blob/master/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlException.cs#L114 and does your application targets .NET Framework? Kindly confirm.

        public int Number
        {
            get { return this.Errors[0].Number; }
        }

As I see in .NET Core this is handled: https://github.com/dotnet/SqlClient/blob/master/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlException.cs#L104

        public int Number
        {
            get { return Errors.Count > 0 ? Errors[0].Number : default; }
        }

Never-the-less I think this should be fixed, along with other places where null checks are missing. cc @JRahnama

@cheenamalhotra cheenamalhotra added 🐛 Bug! Issues that are bugs in the drivers we maintain. and removed ⏳ Waiting for Customer Issues/PRs waiting for user response/action. ℹ️ Needs more Info Issues that have insufficient information to pursue investigations labels Aug 12, 2020
@BoBiene
Copy link
Author

BoBiene commented Aug 18, 2020

Hi @cheenamalhotra,

i can confirm that this issue is only when I target .NET 472, with .NET Core 3.1 it is working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug! Issues that are bugs in the drivers we maintain.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants