Solved

IF Statement not working correctly

Posted on 2011-09-09
22
179 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
[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
  • 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 29

Author Comment

by:sammySeltzer
ID: 36510224
line 3?

can you please pos that line here for me?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 29

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 29

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 29

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 29

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 29

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 29

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 29

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 29

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 29

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 29

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 29

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 29

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 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