Solved

Simple IF statement in C# not working for me

Posted on 2011-09-29
9
272 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 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

930 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

10 Experts available now in Live!

Get 1:1 Help Now