Solved

Simple IF statement in C# not working for me

Posted on 2011-09-29
9
279 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

832 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