Thursday, September 19, 2013

Performing query on a table, return value is null

Hey,


There were so many things, what i can say you need to be careful when copying and pasting the code, and when naming your variables.


anyway, I created a sample based on your code and it prints the table to the console (just test it in console and then update yours)


Also, I don't recommend loading it in a datatable, I would used classes for employee and job.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
GetJobInfo("FirstName3", "LastName3");
}

private static void GetJobInfo(string FirstName, string LastName)
{
// 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));
table.Columns.Add("positionNumber1", typeof(int));
table.Columns.Add("positionTitle", typeof(string));
table.Columns.Add("positionDescription", typeof(string));
table.Columns.Add("companyName", typeof(string));

StringBuilder sbEmps = new StringBuilder();

sbEmps.AppendLine("#Andy&Lau&9/17/2013&andy@hotmail.com&commonwealth&commonwealth&africa&128000&99");

string captureformat = @"^#(?'firstName'\w+)&(?'lastName'\w+)&(?'DateOfBirth'\d+/\d+/\d+).*?&(?'email'\w+@\w+.\w+)&(?'streetAddress'\w+)&(?'suburb'\w+)&(?'state'\w+)&(?'postcode'\w+)&(?'positionNumber'\d+)\s*";

//matching captured format
Regex expr = new Regex(captureformat);

MatchCollection matches = expr.Matches(sbEmps.ToString());

foreach (Match match in matches)
{
//storing captured format into table
DataRow newRow = table.NewRow();

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);

int PositionNumber = int.Parse(match.Groups["positionNumber"].Value);

StringBuilder sbJobs = new StringBuilder();

sbJobs.AppendLine("#99&manager&Research&male");
string newcaptureformat = @"#(?'positionNumber1'\d+)&(?'positionTitle'\w+)&(?'positionDescription'\w+)&(?'companyName'\w+)";


Regex expr1 = new Regex(newcaptureformat);
MatchCollection matches1 = expr1.Matches(sbJobs.ToString());

foreach (Match JobMatch in matches1)
{ //inserting value into table
if (int.Parse(JobMatch.Groups["positionNumber1"].Value) == PositionNumber)
{
newRow["positionNumber1"] = int.Parse(JobMatch.Groups["positionNumber1"].Value);
newRow["positionTitle"] = JobMatch.Groups["positionTitle"].Value;
newRow["positionDescription"] = JobMatch.Groups["positionDescription"].Value;
newRow["companyName"] = JobMatch.Groups["companyName"].Value;
}
}

table.Rows.Add(newRow);
}

for (int i=0;i<table.Columns.Count;i++)
{
Console.WriteLine(table.Rows[0][i].ToString());
}

Console.ReadLine();

}
}
}





Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


No comments:

Post a Comment