Solved

Simple IF statement in C# not working for me

Posted on 2011-09-29
9
267 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
9 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
Try removing the dgvRow.Dispose() statements.
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 175 total points
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 11

Author Comment

by:TheGorby
Comment Utility
Removing the dgvRow.Dispose(); statements produces the same unwanted results.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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 32

Assisted Solution

by:sarabande
sarabande earned 75 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Build JSON from table records 17 38
How to read XML file attributes... 17 39
How do I get the id from URL? 19 46
asp.net bundle 8 32
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

763 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now