Error: ArgumentOutOfRangeException on stringbuilder

J

JenHu

Hi experts,

I wrote a function to use stringbuilder for a fix length text
delimited file for banking stuff.
I received an error on strResult.Insert(141, "".PadLeft(21, " "))

Error: System.ArgumentOutOfRangeException
Message: "Index was out of range. Must be non-negative and
less than the size of the collection.
StackTrace: "at System.Text.StringBuilder.Insert(Int32 index,
String value, Int32 count)
at System.Text.StringBuilder.Insert(Int32 index, String value)
at EPay_Enroller.JenniferFunct.GenerateA(String& strEmpID,
String& strClientAcct, String& strAcctStatus, String&
strAddressFlag, String& strNameFlag)

Any help?
-------------------------------------------------------------------------------------------
Public Function GenerateA(ByRef strEmpID As String, ByRef
strClientAcct As String, ByRef strAcctStatus As String, ByRef
strAddressFlag As String, ByRef strNameFlag As String)
Dim strResult As New System.Text.StringBuilder
Dim GPConnection As SqlConnection
Dim dr As SqlDataReader
Dim GPDataset As New DataSet
GPConnection = New
SqlConnection("...................................................")

Dim selectCMD As SqlCommand = New SqlCommand("SELECT DDACTNUM,
LASTNAME,FRSTNAME,BRTHDATE FROM UPR00102 where EmployID=' " +
strEmpID + "'", GPConnection)
Dim GPDataAdapter As SqlDataAdapter = New SqlDataAdapter
GPDataAdapter.SelectCommand = selectCMD

Dim EfundAcct As String
Dim LName As String
Dim FName As String
Dim MName As String
Dim DOB As String
Dim Loc As String

Try
GPConnection.Open()
Dim DS As DataSet = New DataSet
GPDataAdapter.Fill(DS, "AcctInfoTble")
EfundAcct = DS.Tables("AcctInfoTble").Rows(0)("DDACTNUM")
LName = DS.Tables("AcctInfoTble").Rows(0)("LASTNAME")
FName = DS.Tables("AcctInfoTble").Rows(0)("FASTNAME")

'Position 1 2A A
strResult.Insert(0, "A ")
'Position 3 10A Employee Number
strResult.Insert(2, strEmpyID.ToString.PadRight(10, " "))
..........codes

'Position 142 21A Filler
strResult.Insert(141, "".PadLeft(21, " "))
.......more codes
--------------------------------------------------------------------------------------------

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
J

Jon Skeet [C# MVP]

JenHu said:
I wrote a function to use stringbuilder for a fix length text
delimited file for banking stuff.
I received an error on strResult.Insert(141, "".PadLeft(21, " "))

Error: System.ArgumentOutOfRangeException
Message: "Index was out of range. Must be non-negative and
less than the size of the collection.
StackTrace: "at System.Text.StringBuilder.Insert(Int32 index,
String value, Int32 count)
at System.Text.StringBuilder.Insert(Int32 index, String value)
at EPay_Enroller.JenniferFunct.GenerateA(String& strEmpID,
String& strClientAcct, String& strAcctStatus, String&
strAddressFlag, String& strNameFlag)

Just as it says, basically. You're trying to insert into the
StringBuilder at a position which isn't part of it.

A few other things to note:

1) As far as I can see, you don't need to be passing in those strings
by reference.

2) You should be using a SqlParameter for the parameter to the query,
for various reasons. (Performance, avoiding the need to quote,
protection against SQL injection attacks.)

3) Your first two calls to StringBuilder.Insert could be calls to
StringBuilder.Append instead. It could well be that the rest can be
calls to Append instead, avoiding this problem in the first place.

4) Instead of calling "".PadLeft, you can call Append (' ', 21)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top