Open Form and Create New Record

I’m using Launch Form Options to open a form. Depending on the options, I want it to either load a current record or create a new one. It currently pulls up the old records, or if told to create a new one, it successfully creates the new one, however, it does not load it up to be edited. What I want is it to create the record and then load it up. Not sure why it’s not working as I’m using similar code in another location without issue (although it is not on a form load, it’s within the customization) I know it creates the record as I can then search and pull it up and was using message boxes to show the keys and it creates everything correctly, just refuses to load it up. I’m sure I’m missing something simple so if anyone can point it out, it would be greatly appreciated.

private void UD26Form_Load(object sender, EventArgs args)
	{
			if (UD26Form.LaunchFormOptions != null && UD26Form.LaunchFormOptions.Sender != null && UD26Form.LaunchFormOptions.ValueIn != null)
			{
				string key1 = UD26Form.LaunchFormOptions.ValueIn.ToString();
				this.oTrans_adapter.ClearData();
				if (key1 != "NEW")
					{
						this.oTrans_adapter.GetByID(key1,"Item","","","");
						oTrans.NotifyAll();
						oTrans.Update();
					}
				else
					{
						UD26Adapter adapterUD26 = new UD26Adapter(oTrans);
						adapterUD26.BOConnect();
					    adapterUD26.GetaNewUD26();
						var nr = adapterUD26.UD26Data.UD26.Rows[adapterUD26.UD26Data.UD26.Rows.Count-1];
						string key = nr["Key1"].ToString();
						adapterUD26.Update();
						adapterUD26.Dispose();
						this.oTrans.GetByID(key,"Item","","","");
						this.oTrans.NotifyAll();				
					}
			}
	}

Hey Todd,

It’s been awhile but I seem to recall that making sure I set all of the keys (Key1…Key5) when writing and reading the record fixed that problem for me. Might want to give it a shot.

Mark W.

Just to clarify, you are saying I should set variables Key 2 = “Item” Key3 = “” Key 4="" Key5="" and then use the variables instead of just the “”?

1 Like

Sorry, just make sure when writing the records, you set Key3-Key5 to “” and not leave them null. They’ll come up on a search but not in a GetById. Again, this is if this is the same issue I was having.

Using a BPM to set the keys when it is created and I changed it to write Key3-5 as “” rather than before where I was not setting them. I then changed the code to set the call to use variables of “” and it still works the same. Creates the record but does not load it.

But if I then check, the record has been created and I can load it using the search option.

I then tried changing it to use “x” instead of “” for keys 3 to 5 on both creation (setting the initial keys) and then calling using “x” for those keys. Same exact behavior. Creates the record but does not load it into the form.

And we’re sure that Key1 and Key2 are exact matches? (Watch for trailing spaces, etc.)

So I added a button to do the same code. It works but not on form load only on the button


	private void UD26Form_Load(object sender, EventArgs args)
	{
			if (UD26Form.LaunchFormOptions != null && UD26Form.LaunchFormOptions.Sender != null && UD26Form.LaunchFormOptions.ValueIn != null)
			{
				MessageBox.Show(UD26Form.LaunchFormOptions.ValueIn.ToString());
				string key = UD26Form.LaunchFormOptions.ValueIn.ToString();
				this.oTrans_adapter.ClearData();
				if (key != "NEW")
					{
						this.oTrans_adapter.GetByID(key,"Item","","","");
						oTrans.NotifyAll();
						oTrans.Update();
					}
				else
					{
						UD26Adapter adapterUD26 = new UD26Adapter(oTrans);
						adapterUD26.BOConnect();
					    try {adapterUD26.GetaNewUD26();}catch{MessageBox.Show("Failed to create");}
						var nr = adapterUD26.UD26Data.UD26.Rows[adapterUD26.UD26Data.UD26.Rows.Count-1];
						Key1 = nr["Key1"].ToString();
						Key2 = "Item";
						Key3 = "x";
						Key4 = "x";
						Key5 = "x";
						adapterUD26.Update();
						adapterUD26.Dispose();
						this.oTrans.GetByID(Key1, Key2, Key3, Key4, Key5);
						this.oTrans.NotifyAll();
				
					}
			}
	}

	private void epiButtonC1_Click(object sender, System.EventArgs args)
	{
		// ** Place Event Handling Code Here **
						this.oTrans.GetByID(Key1, Key2, Key3, Key4, Key5);
						this.oTrans.NotifyAll();
	}

Maybe trim this? Do a little display to make sure it is passed in correctly.

The passing is working correctly.
If I pass a key, it loads the record and displays correctly 100% of the time.
If I pass the word “NEW” as that key, and I add a MessageBox.Show it will display KEY and if I nest MessageBoxes within the if statement, they display at the appropriate times.

The issue comes in that when I pass the word NEW I want it to create a new record and load that record to edit. It successfully creates the record every time. It does not then load the record to edit it. I set the keys upon creation of this new record (it’s a BPM on the UD26.GetaNewUD26 BO) . The problem is that when I run this.oTrans.GetByID(Key1, Key2, Key3, Key4, Key5); after creating the record it does not load it. It will however load it if I click a button afterwards. The button click event is a duplicate of the last two lines that are running on the form load with no changes so why is the click needed and how can I remove the need for that button?

After adding a couple more message boxes, it does load the record correctly after creating it, but it then immediately clears it. If I put a message box at the end of the form load event, the record is on the form until I click OK and then it immediately clears. Anyone have any idea why it does this and only on the newly created record and not on previously created ones?

Todd,

Not sure if I’m looking at the correctly, but when you’re setting the values of Key1 to Key5, are you setting it on the new row you just got? When you do the “GetaNew” it returns a blank dataset. Shouldn’t you have code like:

nr["Key1"] = key;
nr["Key2"] = "Item";
...
adapterUD26.Update();

I don’t see where Key1 through Key5 are part of the adapter dataset.

Kevin Simon

Those values are set on the GetANewUD26 BO via BPM. The keys are set and work because I can pull up the record via those, it just requires a button click. I found a terrible workaround in which on the form_shown event I am popping up a MessageBox that displays a message and when the user click ok, then it loads the record. If I remove the Message Box it loads and then clears immediately. The problem is that if I don’t have the user interaction it loads and then clears faster than it can be seen.

Seems like we’re fighting the EpiMagic here at load time… :thinking:

I believe that’s exactly what I’m fighting. Just not sure why it’s different than just coming in and pulling up a record (that works great). If I use a button to load the newly created record, works fine. If I add a message box, it works fine as well.

	private void UD26Form_Shown(object sender, EventArgs args)
	{
		if (UD26Form.LaunchFormOptions != null && UD26Form.LaunchFormOptions.Sender != null && UD26Form.LaunchFormOptions.ValueIn != null)
			{
				if (key == "NEW")
				{
					MessageBox.Show("Check that meeting type and item type are correct");
					this.oTrans.GetByID(Key1, "Item", "", "", "");
					this.oTrans.NotifyAll();
				}
			}
	}

This code “works” but if I remove the MessageBox, it does not. It technically loads the record then immediately clears it. Had the same issues trying it on EpiViewNotifications as well. I also tried using Sleep but it just clears as well. The only way that seems to work is forcing user interaction. I hate using workarounds but not really seeing any other way to get this to work.