Solved

IF Statement not working correctly

Posted on 2011-09-09
22
178 Views
Last Modified: 2012-05-12
Greetings mates,

This one should be simple, I hope.

If textBox is not empty but the associated checkbox is, then insert into cashval field.

If however, textbox is not empty AND checkbox is checked, insert into the chckval field.

The problem is that Yes, it is inserting into the cashval field if the first condition is True but at the same time, inserting the SAME value into chckval field.

Can you please see what i am doing wrong?

I will be glad to provide more info if needed.

Thanks a bunch.

        For x = 1 To 5 Step 1
            dedval = Gridview1.FindControl("ded" & CStr(x))
            chckval = Gridview1.FindControl("chck" & CStr(x))
            chcknumval = Gridview1.FindControl("chcknum" & CStr(x))
            checkboxval = Gridview1.FindControl("chckBox" & CStr(x))
            onetimeval = Gridview1.FindControl("onetime" & CStr(x))
            multival = Gridview1.FindControl("multi" & CStr(x))
            If chckval.Text <> "" And Not checkboxval.Checked Then
                cashval = DirectCast(Gridview1.FindControl("chck" & CStr(x)), TextBox).Text
            Else
                chckval = Gridview1.FindControl("chck" & CStr(x))
            End If
            If dedval.Text <> "-1" And donatechoice.SelectedItem.Value <> "No" Then
                sql += "INSERT INTO employee_ded_amts (employee_id, charity_code, check_amt, chcknum, one_time, bi_weekly, cash, donate_choice, date_stamp) "
                sql += "VALUES ('" & Replace(employee_idLabel.Text, "'", "''") & "','" & Replace(dedval.SelectedValue, "'", "''") & "','" & Replace(chckval.Text, "'", "''") & "','" & Replace(chcknumval.Text, "'", "''") & "','" & Replace(onetimeval.Text, "'", "''") & "','" & multival.Text & "','" & Replace(cashval, "'", "''") & "','" & Replace(donatechoice.SelectedItem.Value, "'", "''") & "','" & Replace(datestamp, "'", "''") & "');"
            End If
            If donatechoice.SelectedItem.Value = "No" Then
                x = 6
                sql += "INSERT INTO employee_ded_amts (employee_id, charity_code, check_amt, chcknum, one_time, bi_weekly, cash, donate_choice, date_stamp) "
                sql += "VALUES ('" & Replace(employee_idLabel.Text, "'", "''") & "','" & Replace(dedval.SelectedValue, "'", "''") & "','" & Replace(chckval.Text, "'", "''") & "','" & Replace(chcknumval.Text, "'", "''") & "','" & Replace(onetimeval.Text, "'", "''") & "','" & Replace(multival.Text, "'", "''") & "','" & Replace(cashval, "'", "''") & "','" & Replace(donatechoice.SelectedItem.Value, "'", "''") & "','" & Replace(datestamp, "'", "''") & "');"
            End If
        Next

Open in new window

0
Comment
Question by:sammySeltzer
  • 13
  • 9
22 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36510175
line 3 you set the default which is the SAME as your else value - hence the behaviour you have.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36510184
put another way, you only have one value for chckval defined, totally independant of any if statements.
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 36510224
line 3?

can you please pos that line here for me?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 28

Author Comment

by:sammySeltzer
ID: 36510238
did you mean this?

        For x = 1 To 5 Step 1
            dedval = Gridview1.FindControl("ded" & CStr(x))
           chckval = Gridview1.FindControl("chck" & CStr(x))

if that's the line, then I can begin to see how different .net is from classic asp because that's how I do it in classic asp.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36510265
line 3 in your supplied code is
chckval = Gridview1.FindControl("chck" & CStr(x))

>>then I can begin to see how different .net is from classic asp because that's how I do it in classic asp.
I'm surprised that assigning a value in classic asp results in it then being ignored
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 36510292
No, sorry I wasn't clear.

Assigning that value but followed up with this if then else statement:
            If chckval.Text <> "" And Not checkboxval.Checked Then
                cashval = DirectCast(Gridview1.FindControl("chck" & CStr(x)), TextBox).Text
            Else
                chckval = Gridview1.FindControl("chck" & CStr(x))
            End If


But I will remove that and test again and post back.

Thanks alot
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 36510329
*NOW*, it is generating an error that says:

Object reference not set to an instance of an object.

on this line below If chckval.Text <> "" And Not checkboxval.Checked Then

*after* I removed this

chckval = Gridview1.FindControl("chck" & CStr(x))

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36510356
That I can understand but look at your code.
You NEVER change the value of the variable chkval after you set it in line 3 - which is why you get the same value inserted independantly of which branch of the if statement is executed.
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 36510585
please, please if you don't mind, give me an example. I am really truly confused.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 36510617
       For x = 1 To 5 Step 1
            dedval = Gridview1.FindControl("ded" & CStr(x))
            chckval = Gridview1.FindControl("chck" & CStr(x))
            chcknumval = Gridview1.FindControl("chcknum" & CStr(x))
            checkboxval = Gridview1.FindControl("chckBox" & CStr(x))
            onetimeval = Gridview1.FindControl("onetime" & CStr(x))
            multival = Gridview1.FindControl("multi" & CStr(x))
            If chckval.Text <> "" And Not checkboxval.Checked Then
                cashval = DirectCast(Gridview1.FindControl("chck" & CStr(x)), TextBox).Text

At this point chkval is Gridview1.FindControl("chck" & CStr(x))
            Else
                chckval = Gridview1.FindControl("chck" & CStr(x))
At this point chkval is Gridview1.FindControl("chck" & CStr(x))
            End If
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 36510655
Ok, I see.

So, more like:

       For x = 1 To 5 Step 1
            dedval = Gridview1.FindControl("ded" & CStr(x))
            chckval = Gridview1.FindControl("chck" & CStr(x))
            chcknumval = Gridview1.FindControl("chcknum" & CStr(x))
            checkboxval = Gridview1.FindControl("chckBox" & CStr(x))
            onetimeval = Gridview1.FindControl("onetime" & CStr(x))
            multival = Gridview1.FindControl("multi" & CStr(x))
            If chckval.Text <> "" And Not checkboxval.Checked Then
                cashval = DirectCast(Gridview1.FindControl("chck" & CStr(x)), TextBox).Text
                chckval = "0"               Else
                chckval = Gridview1.FindControl("chck" & CStr(x))
           End If

Like this?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36510743
I don't know what you want as an end result but basically yes.  Somewhere you probably want it to have another value.
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 36511019
Yes, that what I want to do. If it is cash, put the amount into cash field and put a zero into chckval field.

But now, I am getting 'expression expected' error after I tried to reset chckval to 0 or ''.

I guess this is not as simple as I was hoping.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36511080
I'm not certain what you are doing but maybe you are making it difficult for yourself.
Why not use a string variable which is "" (ie. empty string) and only set the value when you have one (if statement).
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 36511186
maybe you are making it difficult for yourself

Perhaps I am but I am not asking anyone to write it from scratch. I have done that.

Just give me the snip that you think I am missing.

Your suggestions aren't helping me much. I am trying them but they are bumming out.

If you have the answer, which I am sure you do, why not just give me that one or 2 line of code?
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 36511202
That's precisely what i have done for others here, if you look at my profile.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36511391
You could look at my profile as well.

The problem is I don't know what you want to do precisely under which conditions so I can't supply any code that way.  I've tried to explain multiple times what is wrong with your current code in that the contents of the variable are unchanged.  You seem to have grasped that.

Basically it is something like:

somevar = ""
...
if(xxx)
  somevar = "some value"

but as I say I don't know what condition is required for somevar to be set to "some value" nor what the actual value of "some value"   is.
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 36511535
I have looked at your profile and it is stellar and that's why I said that I am confident you can help me, rather than point in directions that isn't working so far.

Let me try to explain (again).

If the money is check, then insert that check into check fieldname and ensure that cash fieldname has 0 (zero) values.

This part is working fine.

If the money is cash, insert that cash amount into the cash fieldname and insert 0 (zero) into the check fieldname.

This part is not working because it inserts the same amount into check fieldname that is also inserted into cash fieldname.

I hope this clear.

I have tried your suggestion but I keep getting 'expression expected' error message.

If you think it is the right syntax, where should I place the code given the code I posted above?

I have tried it here:
chckval = Gridview1.FindControl("chck" & CStr(x))

by changing the above code to:
chckval = ""

but I get 'expression expected' error message.

I tried it where chckval was declared, outside the loop:
 Dim chckval As TextBox
chckval = ""

I get same error message.

Thank you very much help. I am just a bit frustrated but I appreciate your help, nonetheless.






0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36511610
OK, use an extra string value for simplicity:


Dim chckval As TextBox, strChckVal as string

...  and in your loop
strChckVal = "0"

if(IsCash)
  ...
else
  strChckVal = "supply the value here"

...  now write the strChckVal into the database instead of your current chckVal
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 36511779
I was actually trying something very similar to your current suggestion.

I dimmed chckvalFlag as String
Dim chckValFlag AS String = ""

Then I kept chckval as TextBox just like it is currently.

Then I did the loop.

Something strange happened .

Again, the correct value for check was inserted into check and 0 inserted into cash (If the amount is check).

However, this time, the correct value was inserted into cash( if the amount is cash) but 3 was inserte into check as well.

Let me review my code and try to see what happened.

Maybe, just maybe I assigne and empty string to chckvalFlag. Let me try with a 0.

Then
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 36511945
Bummer!

Stupid code still isn't working right.

It is inserting the LAST inserted value into the spaces where cash was inserted into cash fieldname.

For instance, if I have 3 different amounts of 203 and 240 and 125. Of these, 125 is cash.

SO, I insert 203 into the first check Amount textbox. I insert 240 into the second check Amount textbox.

Finally, I insert 125 into the Cash Amount textbox.

What is happening is that instead of the corresponding checkamount field being 0 on the Db, since the amount is cash, the last inserted check amount of 240 is inserted into this space on the db for check fieldname.

DId I confuse?

If you have any ideas why this is happening, pls share with me.

I appreciate your help so far.
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 36511986
Ok it is working fine now.

I was able to get your initial suggestion of  reseting chckval like:

            If chckval.Text <> "" And Not checkboxval.Checked Then
                cashval = DirectCast(Gridview1.FindControl("chck" & CStr(x)), TextBox).Text
                chckval = "0"     
          Else
...
...

Open in new window


Now, it works like I want it to work.

Thanks alot
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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