Solved

Simple IF statement in C# not working for me

Posted on 2011-09-29
9
286 Views
Last Modified: 2012-05-12
I have a feeling this is an easy one for those with experience. The code is posted below; the very first IF statement [if ((company == "S") | (company == "K") | (company == "J"))] is not working. There are only 4 possible values for 'company', they are "S","K","T" or "J". If company for a given record is S, K or J then it writes the record to the first DataGridView. If company is T, it writes the record to the 2nd DGV. The problem is that any records where company is J do not show up on either DGV. S and K records show up just fine.

I also tried replacing the first IF statement with [if (company != "T")] but STILL it only shows records from S and K companies in the DGV. What am I doing wrong here?
private void EnterEditMode(int RowIndex, string company)
        {        

            updaterowID = -999; //critical reset
            int rowID = 0;
            if ((company == "S") | (company == "K") | (company == "J"))
            {
                DataGridViewRow dgvRow = dgvEvents.Rows[RowIndex];

                txtTruck.Text = dgvRow.Cells["tractor"].Value.ToString();
                txtTrailer.Text = dgvRow.Cells["trailer"].Value.ToString();
                txtLoadStatus.Text = dgvRow.Cells["loadstatus"].Value.ToString();
                txtAction.Text = dgvRow.Cells["action"].Value.ToString();
                txtCompany.Text = dgvRow.Cells["company"].Value.ToString();
                txtTime.Text = ((DateTime)dgvRow.Cells["event_time"].Value).ToShortTimeString();
                txtDate.Text = ((DateTime)dgvRow.Cells["event_time"].Value).ToShortDateString();
                rowID = (int)dgvRow.Cells["row_id"].Value;
                frmMain.ActiveForm.BackColor = Color.FromKnownColor(KnownColor.Wheat);
                dgvRow.Dispose();
                
            }

            else if (company != "S" && company != "K" && company != "J")
            {

                DataGridViewRow dgvRow = dgvTAEvents.Rows[RowIndex];

                txtTruck.Text = dgvRow.Cells["tractor"].Value.ToString();
                txtTrailer.Text = dgvRow.Cells["trailer"].Value.ToString();
                txtLoadStatus.Text = dgvRow.Cells["loadstatus"].Value.ToString();
                txtAction.Text = dgvRow.Cells["action"].Value.ToString();
                txtCompany.Text = dgvRow.Cells["company"].Value.ToString();
                txtTime.Text = ((DateTime)dgvRow.Cells["event_time"].Value).ToShortTimeString();
                txtDate.Text = ((DateTime)dgvRow.Cells["event_time"].Value).ToShortDateString();
                rowID = (int)dgvRow.Cells["row_id"].Value;
                frmMain.ActiveForm.BackColor = Color.FromKnownColor(KnownColor.Wheat);
                dgvRow.Dispose();

            }

Open in new window

0
Comment
Question by:TheGorby
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36815690
Single bar ( | ) is a BITWISE OR; double bar ( || ) is logical OR. Try changing line 6 to:

if ((company == "S") || (company == "K") || (company == "J"))

Open in new window

0
 
LVL 11

Author Comment

by:TheGorby
ID: 36815741
Logical OR also does not display any records where company is J, but does display records where company is S or K. Why would it only exclude 'J' records? That's what seems the most odd to me.
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 36815762
Try removing the dgvRow.Dispose() statements.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 175 total points
ID: 36815784
Dumb question time:  Are you sure you're passing "J" records? Have you tried stepping through the code at that point to see why a "J" might fail?
0
 
LVL 11

Author Comment

by:TheGorby
ID: 36815790
Removing the dgvRow.Dispose(); statements produces the same unwanted results.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36815819
As a side note, your "else if" is really redundant. If none of:

((company == "S") || (company == "K") || (company == "J"))

Open in new window


return true, then:

(company != "S" && company != "K" && company != "J")

Open in new window


would trivially be true. In short, a simple else should suffice  : )
0
 
LVL 34

Assisted Solution

by:sarabande
sarabande earned 75 total points
ID: 36816661
your own experience that when replacing the first if condition by (company != "T") also wouldn't show any "J" records is a strong evidence that the query result set actually doesn't contain any "J" companies.

can you show how you retrieved the records passed to the EnterEditMode?
0
 
LVL 11

Author Comment

by:TheGorby
ID: 36816859
Great suggestions, I will look into this right away. I suspect it is an issue with J records not being available in the query result. I didn't write this program, and the person that did no longer works at our company. The program worked fine, and then earlier this week I was tasked with modifying it so that records could also be entered that had "J" as the company. It works, I can retrieve the records using my own queries, but this program will not retrieve them.

Kaufmed - I saw that too, the 'else if' was from the original writer
0
 
LVL 11

Author Closing Comment

by:TheGorby
ID: 36817002
The program retrieved SQL results by passing parameters to and executing a stored procedure in the DB. For some reason the stored procedure specified that only records with company in S,K,T would be returned, even though the C# code specified them as well and those are the only options anyway - no nulls/blanks allowed either. I assumed, after seeing the C# code, that he was retrieving all records and filtering them at the program level.

This is probably a rookie mistake, but this is only the second time I've attempted C# coding ever, so thanks a bunch for the help!
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question