Need Help with some C# code

I am new to the C# world and working on correcting and issue.  Our ERP system called Epicor uses this code to process tasks based on triggers we set in the system.  This code seems to compile ok but when running it bombs out.  Can someone take a quick look and see if there is something obvious that is causing it to error out?

Ice.Tables.UDCodes UDCodes;
Ice.Tables.UD01 UD01;

var ttJobHead_xRow = (from ttJobHead_Row in ttJobHead
                        select ttJobHead_Row).LastOrDefault();

if (ttJobHead_xRow != null)
{
    UDCodes = (from UDCodes_Row in Db.UDCodes
                where string.Compare(UDCodes_Row.Company, "FLW", true) == 0
                select UDCodes_Row).FirstOrDefault();
    UDCodes = new Ice.Tables.UDCodes();
    Db.UDCodes.Insert(UDCodes);
    UDCodes.CodeTypeID = "FL-Pallet";
    UDCodes.CodeID = ttJobHead_xRow.JobNum;
    UDCodes.Company = "FLW";
    UDCodes["ShortChar01"] = ttJobHead_xRow.JobNum;
    UDCodes["ShortChar02"] = "001";
    UDCodes["Number01"] = 1;
    UD01 = (from UD01_Row in Db.UD01
            where string.Compare(UD01_Row.Key1, "FL-Pallet", true) == 0 && UD01_Row.Key2 == ttJobHead_xRow.JobNum
            select UD01_Row).FirstOrDefault();

    if (UD01 == null)
    {
        UD01 = new Ice.Tables.UD01();
        Db.UD01.Insert(UD01);
        UD01.Key1 = "FL-Pallet";
        UD01.Key2 = ttJobHead_xRow.JobNum;
        UD01.Company = "FLW";
        UD01["ShortChar01"] = ttJobHead_xRow.JobNum;
        UD01["ShortChar02"] = "001";
        UD01["Number01"] = 1;
        Db.UD01.Update(UD01);
    }
}

Open in new window

LVL 3
Trygve ThayerIT DirectorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Inserting into code blocks:
Ice.Tables.UDCodes UDCodes;
Ice.Tables.UD01 UD01;
var ttJobHead_xRow = (from ttJobHead_Row in ttJobHead 
					select ttJobHead_Row).LastOrDefault();

if (ttJobHead_xRow != null)
{
	UDCodes = (from UDCodes_Row in Db.UDCodes 
				where string.Compare(UDCodes_Row.Company, "FLW", true) == 0
				select UDCodes_Row).FirstOrDefault();

	Db.UDCodes.Insert(UDCodes);
	UDCodes.CodeTypeID = "FL-Pallet";
	UDCodes.CodeID = ttJobHead_xRow.JobNum;
	UDCodes.Company = "FLW";
	UDCodes["ShortChar01"] = ttJobHead_xRow.JobNum;
	UDCodes["ShortChar02"] = "001";
	UDCodes["Number01"] = 1;
	UD01 = (from UD01_Row in Db.UD01
			where string.Compare(UD01_Row.Key1, "FL-Pallet", true) == 0 && UD01_Row.Key2 == ttJobHead_xRow.JobNum
			select UD01_Row).FirstOrDefault();

	if (UD01 == null)
	{
		UD01 = new Ice.Tables.UD01();
		Db.UD01.Insert(UD01);
		UD01.Key1 = "FL-Pallet";
		UD01.Key2 = ttJobHead_xRow.JobNum;
		UD01.Company = "FLW";
		UD01["ShortChar01"] = ttJobHead_xRow.JobNum;
		UD01["ShortChar02"] = "001";
		UD01["Number01"] = 1;
		Db.UD01.Update(UD01);
	}
}

Open in new window

-saige-
it_saigeDeveloperCommented:
There are a couple of potential troublespots that standout to me; In order to troubleshoot, I would recommend that you surround the main logic in a try...catch block; e.g. -
Ice.Tables.UDCodes UDCodes;
Ice.Tables.UD01 UD01;
try
{
	var ttJobHead_xRow = (from ttJobHead_Row in ttJobHead
					  select ttJobHead_Row).LastOrDefault();

	if (ttJobHead_xRow != null)
	{
		UDCodes = (from UDCodes_Row in Db.UDCodes
				 where string.Compare(UDCodes_Row.Company, "FLW", true) == 0
				 select UDCodes_Row).FirstOrDefault();

		Db.UDCodes.Insert(UDCodes);
		UDCodes.CodeTypeID = "FL-Pallet";
		UDCodes.CodeID = ttJobHead_xRow.JobNum;
		UDCodes.Company = "FLW";
		UDCodes["ShortChar01"] = ttJobHead_xRow.JobNum;
		UDCodes["ShortChar02"] = "001";
		UDCodes["Number01"] = 1;
		UD01 = (from UD01_Row in Db.UD01
			   where string.Compare(UD01_Row.Key1, "FL-Pallet", true) == 0 && UD01_Row.Key2 == ttJobHead_xRow.JobNum
			   select UD01_Row).FirstOrDefault();

		if (UD01 == null)
		{
			UD01 = new Ice.Tables.UD01();
			Db.UD01.Insert(UD01);
			UD01.Key1 = "FL-Pallet";
			UD01.Key2 = ttJobHead_xRow.JobNum;
			UD01.Company = "FLW";
			UD01["ShortChar01"] = ttJobHead_xRow.JobNum;
			UD01["ShortChar02"] = "001";
			UD01["Number01"] = 1;
			Db.UD01.Update(UD01);
		}
	}
}
catch (Exception ex)
{
	// Do something to record your exception; e.g. -
	// System.Console.WriteLine("Exception reported in {0} - {1} [{2}]", ex.Source, ex, ex.Message);
	// System.Diagnostics.Debug.WriteLine("Exception reported in {0} - {1} [{2}]", ex.Source, ex, ex.Message);
	// System.Windows.Forms.MessageBox.Show(string.Format("Exception reported in {0} - {1} [{2}]", ex.Source, ex, ex.Message));
}

Open in new window

-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Trygve ThayerIT DirectorAuthor Commented:
Ok I will try to do that.  I was just sticking one line at a time in there until it failed.  Will let you know.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Jacques Bourgeois (James Burger)PresidentCommented:
What line does cause the error, and what error message do you get if you get one?
Trygve ThayerIT DirectorAuthor Commented:
After a lot of trial and error I was able to get it working.  Below is the finished code that works.

Ice.Tables.UDCodes UDCodes;
Ice.Tables.UD01 UD01;
var ttJobHead_xRow = (from ttJobHead_Row in ttJobHead select ttJobHead_Row).FirstOrDefault();

if (ttJobHead_xRow != null)
{
      UDCodes = (from UDCodes_Row in Db.UDCodes where string.Compare(UDCodes_Row.Company, "FLW", true) == 0 select UDCodes_Row).FirstOrDefault();

      if (UDCodes == null)
      {
      UDCodes = new Ice.Tables.UDCodes();
      Db.UDCodes.Insert(UDCodes);
      UDCodes.CodeTypeID = "FL-Pallet";
      UDCodes.CodeID = ttJobHead_xRow.JobNum;
      UDCodes.Company = "FLW";
      UDCodes["ShortChar01"] = ttJobHead_xRow.JobNum;
      UDCodes["ShortChar02"] = "001";
      UDCodes["Number01"] = 1.0m;
      }

      UD01 = (from UD01_Row in Db.UD01 where string.Compare(UD01_Row.Key1, "FL-Pallet", true) == 0 && UD01_Row.Key2 == ttJobHead_xRow.JobNum select UD01_Row).FirstOrDefault();

      if (UD01 == null)
      {
            UD01 = new Ice.Tables.UD01();
            Db.UD01.Insert(UD01);
            UD01.Company = "FLW";
            UD01.Key1 = "FL-Pallet";
            UD01.Key2 = ttJobHead_xRow.JobNum;
            UD01["ShortChar01"] = ttJobHead_xRow.JobNum;
            UD01["ShortChar02"] = "001";
            UD01["Number01"] = 1.0m;
            //UD01.Update(UD01);
      }
}
Trygve ThayerIT DirectorAuthor Commented:
Got me started in the right direction.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.