Stringbuilder question

J

JenHu

Hi experts,

I am using stringbuilder in a funtion 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
*-----------------------*
 
H

Herfried K. Wagner [MVP]

JenHu said:
I am using stringbuilder in a funtion 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)

Make sure the buffer is long enough. To do this, check the 'StringBuilder'
object's 'Capacity' property and make the buffer larger in order to use
'Insert' on a certain position.
 

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