6 Replies - 888 Views - Last Post: 02 March 2014 - 10:37 PM Rate Topic: -----

#1 joshklewis   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-February 10

C# and Entity Framework

Posted 01 March 2014 - 06:48 PM

I am having a slight issue that I cannot seem to figure out. I have a form with a text box and some combo boxes. If a barcode is entered into the text box and enter is pressed, it should select the proper values in the combo boxes to match the item. If the barcode is not found in the database then it should show a messagebox saying the item was not found. When the barcode is found in the database, the values are selected in the comboboxes as they should be; however, if the barcode is not found in the database, the messagebox does not appear. I've been stuck on this for a few hours now and cannot seem to figure it out. Any help would be appreciated.

private void txtBarcode_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                var search =
                    (from items in context.StockItems
                     where items.Barcode == txtBarcode.Text.Trim()
                     select items);

                foreach (StockItem item in search)
                {
                    if (item != null)
                    { 
                        cmboBrand.Text = item.Brand;
                        if (item.Detail != null)
                            cmboDetail.Text = item.Detail;
                        cmboUnit.Text = item.Unit;   
                    }
                    else
                    {
                        MessageBox.Show("Item not found");
                    }
                }
            }
        }



Is This A Good Question/Topic? 0
  • +

Replies To: C# and Entity Framework

#2 andrewsw   User is online

  • Entwickler
  • member icon

Reputation: 6605
  • View blog
  • Posts: 26,918
  • Joined: 12-December 12

Re: C# and Entity Framework

Posted 01 March 2014 - 07:00 PM

None of item will be null so the MessageBox will never display.

You need to check if the query returned any rows; one way is to use Any():
if (search.Any()) {
    // there are rows..
} else {
    // D'oh, there are none..
}

Was This Post Helpful? 2
  • +
  • -

#3 joshklewis   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-February 10

Re: C# and Entity Framework

Posted 01 March 2014 - 07:13 PM

So item will not be null even if there is not a row with that barcode? The following is code for a similar situation in the same program only it works as I want it to, including if the search result is null.

private void btnSave_Click(object sender, EventArgs e)
        {
            InventoryModelContainer context = new InventoryModelContainer();

            var search = (from items in context.StockItems
                         where items.Barcode == txtBarcode.Text
                         select items);

            foreach (StockItem items in search)
            {
                StockItem result = items;

                if (result != null)
                {
                    MessageBox.Show("Item already in inventory!", "Unable to add", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.DialogResult = DialogResult.Cancel;
                    return;
                }
                else
                {
                    this.DialogResult = DialogResult.OK;
                }
            }
        }


Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6455
  • View blog
  • Posts: 22,095
  • Joined: 05-May 12

Re: C# and Entity Framework

Posted 01 March 2014 - 09:06 PM

Now would be a great time to learn how to use a debugger. Visual Studio has an amazingly capable debugger built into it. Set a breakpoint and step through your code, or using Trace messages to see what is happening to your variables.

andrewsw is quite correct in his post.

What happens when you add ".Trim()" to line 6 on your post #3 so that it is like line 7 on post #1?
Was This Post Helpful? 0
  • +
  • -

#5 joshklewis   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-February 10

Re: C# and Entity Framework

Posted 02 March 2014 - 06:44 PM

andrewsw was correct. Using search.Any() in my if statement fixed the problem and it works now. I appreciate it very much. My second post was really just asking why it worked in one situation and not in the other when they were so similar. Again, thank you for the help.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6455
  • View blog
  • Posts: 22,095
  • Joined: 05-May 12

Re: C# and Entity Framework

Posted 02 March 2014 - 07:45 PM

I suspect that the reason why the second code was working was because if there was a match, the for loop would be entered. If there was no match, the for loop would not even be entered.

Again, knowing how to use a debugger would help you understand what is happening because you can set a breakpoint and observe what is happening.
Was This Post Helpful? 0
  • +
  • -

#7 z10   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 08-September 13

Re: C# and Entity Framework

Posted 02 March 2014 - 10:37 PM

Just as a general point, I would recommend using a disconnected model for your context rather than having a single connection that you are passing around in your methods and event handlers (as seems to be the case above).
I believe this is the recommended use of the entity framework context class by Microsoft as well. This way you don't have to track the state of all of your entities because you are opening and closing distinct connections to your database.
The only thing to bear in mind is to attach any entities that you have previously saved to your database rather than adding them in many-to-many relationships.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1