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

Fix | Perform Null check for SqlErrors in SqlException #698

Merged
merged 2 commits into from
Aug 21, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,43 +99,43 @@ private bool ShouldSerializeErrors()
/// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlException.xml' path='docs/members[@name="SqlException"]/Class/*' />
public byte Class
{
get { return this.Errors[0].Class; }
get { return Errors.Count > 0 ? this.Errors[0].Class : default; }
}

/// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlException.xml' path='docs/members[@name="SqlException"]/LineNumber/*' />
public int LineNumber
{
get { return this.Errors[0].LineNumber; }
get { return Errors.Count > 0 ? Errors[0].LineNumber : default; }
}

/// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlException.xml' path='docs/members[@name="SqlException"]/Number/*' />
public int Number
{
get { return this.Errors[0].Number; }
get { return Errors.Count > 0 ? Errors[0].Number : default; }
}

/// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlException.xml' path='docs/members[@name="SqlException"]/Procedure/*' />
public string Procedure
{
get { return this.Errors[0].Procedure; }
get { return Errors.Count > 0 ? Errors[0].Procedure : default; }
}

/// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlException.xml' path='docs/members[@name="SqlException"]/Server/*' />
public string Server
{
get { return this.Errors[0].Server; }
get { return Errors.Count > 0 ? Errors[0].Server : default; }
}

/// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlException.xml' path='docs/members[@name="SqlException"]/State/*' />
public byte State
{
get { return this.Errors[0].State; }
get { return Errors.Count > 0 ? Errors[0].State : default; }
}

/// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlException.xml' path='docs/members[@name="SqlException"]/Source/*' />
override public string Source
{
get { return this.Errors[0].Source; }
get { return Errors.Count > 0 ? Errors[0].Source : default; }
}

/// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlException.xml' path='docs/members[@name="SqlException"]/ToString/*' />
Expand All @@ -146,7 +146,7 @@ public override string ToString()
sb.AppendFormat(SQLMessage.ExClientConnectionId(), _clientConnectionId);

// Append the error number, state and class if the server provided it
if (Number != 0)
if (Errors.Count > 0 && Number != 0)
{
sb.AppendLine();
sb.AppendFormat(SQLMessage.ExErrorNumberStateClass(), Number, State, Class);
Expand All @@ -169,12 +169,12 @@ public override string ToString()
return sb.ToString();
}

static internal SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion)
internal static SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion)
{
return CreateException(errorCollection, serverVersion, Guid.Empty);
}

static internal SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion, SqlInternalConnectionTds internalConnection, Exception innerException = null)
internal static SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion, SqlInternalConnectionTds internalConnection, Exception innerException = null)
{
Guid connectionId = (internalConnection == null) ? Guid.Empty : internalConnection._clientConnectionId;
var exception = CreateException(errorCollection, serverVersion, connectionId, innerException);
Expand All @@ -195,11 +195,10 @@ static internal SqlException CreateException(SqlErrorCollection errorCollection,
return exception;
}

static internal SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion, Guid conId, Exception innerException = null)
internal static SqlException CreateException(SqlErrorCollection errorCollection, string serverVersion, Guid conId, Exception innerException = null)
{
Debug.Assert(null != errorCollection && errorCollection.Count > 0, "no errorCollection?");

// concat all messages together MDAC 65533
StringBuilder message = new StringBuilder();
for (int i = 0; i < errorCollection.Count; i++)
{
Expand All @@ -219,7 +218,7 @@ static internal SqlException CreateException(SqlErrorCollection errorCollection,

exception.Data.Add("HelpLink.ProdName", "Microsoft SQL Server");

if (!ADP.IsEmpty(serverVersion))
if (!string.IsNullOrEmpty(serverVersion))
{
exception.Data.Add("HelpLink.ProdVer", serverVersion);
}
Expand Down