Please read your exception. It states that your table in the database accessed by your SqlDataReader
class has only 11 columns
Index outside of bounds. How many index items can string.format hold?
-
31-05-2022 - |
Question
I have the following code I'm using to build a fixed length text file.
TestFile.WriteLine(string.Format(
//Formats with negative numbers are left justified. e.g. {1,-10}
"{0,50}" + //SPACER 50
"{1,-10}" + //NCSBN ID
"{2,10}" + //SPACER 10
"{3,-50}" + //LAST NAME
"{4,-30}" + //FIRST NAME
"{5,-30}" + //MIDDLE NAME
"{6,90}" + //SPACER 90
"{7,-8}" + //DOB
"{8,-1}" + //GENDER
"{9,-9}" + //SOCIAL SECURITY NUMBER
"{10,41}" + //SPACER 41
"{11,-60}" + //ADDRESS 1
"{12,-60}", //ADDRESS 2
DbReader.GetString(0),DbReader.GetString(1),DbReader.GetString(2),
DbReader.GetString(3), DbReader.GetString(4),DbReader.GetString(5),
DbReader.GetString(6), DbReader.GetString(7), DbReader.GetString(8),
DbReader.GetString(9), DbReader.GetString(10), DbReader.GetString(11),
DbReader.GetString(12)));
Everything is fine until I add the 12th index then I get the following error:
System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."
Source="System.Data"
StackTrace:
at System.Data.Odbc.DbCache.AccessIndex(Int32 i)
at System.Data.Odbc.OdbcDataReader.internalGetString(Int32 i)
at System.Data.Odbc.OdbcDataReader.GetString(Int32 i)
at BuildFile_EXE.Program.Main(String[] args) in G:\IT Development\SSIS\Reports\NursysAPRN_ToNatCouncil\BuildFile_EXE\BuildFile_EXE\Program.cs:line 69
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Does string.format have an 11 index limit or am I missing something obvious?
Thanks.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow