I know this is a long shot but i have been trying to debug this issue for the night and yet i couldn't find what is wrong with it.I am wondering maybe someone is able to spot what i might have screwed up. Basically when i run the program, i am prompt the following error. The reason why the index is out of range is because the value is null thus i was not able to call the value.
Index was out of range. Must be non-negative and less than the size of the collection.
What i am doing is as suggested by a kind gentleman on this forum is to create a table and store my values inside to allow easy access in future. There is 2 text file involve, each with a common integer within them. I am to search the first file for"first" and "last" match and return the integer. The integer is then used for a second query within the table to return related values.
The file format would look exactly as below
#apple&red&sweet&juice&3200#pear&green&sweet&juice&1200
public Job GetJobInfo(string first, string last)
{
string line;
string line1;
// creating new table
DataTable table = new DataTable();
table.Columns.Add("firstName", typeof(string));
table.Columns.Add("lastName", typeof(string));
table.Columns.Add("dateOfBirth", typeof(DateTime));
table.Columns.Add("email", typeof(string));
table.Columns.Add("streetAddress", typeof(string));
table.Columns.Add("suburb", typeof(string));
table.Columns.Add("state", typeof(string));
table.Columns.Add("postcode", typeof(string));
table.Columns.Add("positionNumber", typeof(int));
//positionNumer and positionNumber1 are the common integer value
table.Columns.Add("positionNumber1", typeof(int));
table.Columns.Add("positionTitle", typeof(string));
table.Columns.Add("positionDescription", typeof(string));
table.Columns.Add("companyName", typeof(string));
//reading text file
StreamReader reader = new StreamReader(@"C:\Users\user\Person.txt");
line = reader.ReadToEnd();
string input = line;
string captureformat = @"\#(?'firstName'\w+)&(?'lastName'\w+)&(?'dateOfBirth'\w+)&(?'email'\w+)&(?'streetAddress'\w+)&(?'suburb'\w+)&(?'state'\w+)&(?'postcode'\w+)&(?'positionNumber'\d+)\s*";
//matching captured format
Regex expr = new Regex(captureformat);
MatchCollection matches = expr.Matches(input);
foreach (Match match in matches)
{//storing captured format into table
DataRow newRow = table.Rows.Add();
newRow["firstName"] = match.Groups["firstName"].Value;
newRow["lastName"] = match.Groups["lastName"].Value;
newRow["dateOfBirth"] = DateTime.Parse(match.Groups["dateOfBirth"].Value);
newRow["email"] = match.Groups["email"].Value;
newRow["streetAddress"] = match.Groups["streetAddress"].Value;
newRow["suburb"] = match.Groups["suburb"].Value;
newRow["state"] = match.Groups["state"].Value;
newRow["postcode"] = match.Groups["postcode"].Value;
newRow["positionNumber"] = int.Parse(match.Groups["positionNumber"].Value);
}
//reading second text file
StreamReader reader1 = new StreamReader(@"C:\Users\user\Job.txt");
line1 = reader.ReadToEnd();
string input1 = line1;
string newcaptureformat = @"\#(?'positionNumber1'\w+)&(?'positionTitle'\w+)&(?'positionDescription'\w+)&(?'companyName'\d+)\s*";
//matching capture format
Regex expr1 = new Regex(newcaptureformat);
MatchCollection matches1 = expr.Matches(input);
foreach (Match match in matches)
{ //inserting value into table
DataRow newRow = table.Rows.Add();
newRow["positionNumber1"] = int.Parse(match.Groups["positionNumber1"].Value);
newRow["positionTitle"] = match.Groups["positionTitle"].Value;
newRow["positionDescription"] = match.Groups["positionDescription"].Value;
newRow["companyName"] = match.Groups["companyName"].Value;
}
// query table for first and last
List<DataRow> queryResults = table.AsEnumerable()
.Where(row => (row.Field<string>("firstName") == first) && (row.Field<string>("lastName") == last))
.ToList();
Console.WriteLine(queryResults);
//storing result into int result
int result = queryResults[8].Field<int>("positionNumber");
/**
Index was out of range. Must be non-negative and less than the size of the collection.
*/
//perform second query
List<DataRow> queryResults1 = table.AsEnumerable()
//matching results from the first result
.Where(row => (row.Field<int>("positionNumber1") == result))
.ToList();
//storing related matches into final
Job final = new Job();
final.positionNumber = queryResults1[9].Field<int>("positionNumber1");
final.positionTitle = queryResults1[10].Field<string>("positionTitle");
final.positionDescription = queryResults1[11].Field<string>("positionDescription");
final.companyName = queryResults1[12].Field<string>("companyName");
return final;
}