sammySeltzer
asked on
IF Statement not working correctly
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.
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
line 3 you set the default which is the SAME as your else value - hence the behaviour you have.
put another way, you only have one value for chckval defined, totally independant of any if statements.
ASKER
line 3?
can you please pos that line here for me?
can you please pos that line here for me?
ASKER
did you mean this?
For x = 1 To 5 Step 1
dedval = Gridview1.FindControl("ded " & CStr(x))
chckval = Gridview1.FindControl("chc k" & 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.
For x = 1 To 5 Step 1
dedval = Gridview1.FindControl("ded
chckval = Gridview1.FindControl("chc
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.
line 3 in your supplied code is
chckval = Gridview1.FindControl("chc k" & 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
chckval = Gridview1.FindControl("chc
>>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
ASKER
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.FindC ontrol("ch ck" & CStr(x)), TextBox).Text
Else
chckval = Gridview1.FindControl("chc k" & CStr(x))
End If
But I will remove that and test again and post back.
Thanks alot
Assigning that value but followed up with this if then else statement:
If chckval.Text <> "" And Not checkboxval.Checked Then
cashval = DirectCast(Gridview1.FindC
Else
chckval = Gridview1.FindControl("chc
End If
But I will remove that and test again and post back.
Thanks alot
ASKER
*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("chc k" & CStr(x))
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("chc
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.
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.
ASKER
please, please if you don't mind, give me an example. I am really truly confused.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok, I see.
So, more like:
For x = 1 To 5 Step 1
dedval = Gridview1.FindControl("ded " & CStr(x))
chckval = Gridview1.FindControl("chc k" & CStr(x))
chcknumval = Gridview1.FindControl("chc knum" & CStr(x))
checkboxval = Gridview1.FindControl("chc kBox" & CStr(x))
onetimeval = Gridview1.FindControl("one time" & CStr(x))
multival = Gridview1.FindControl("mul ti" & CStr(x))
If chckval.Text <> "" And Not checkboxval.Checked Then
cashval = DirectCast(Gridview1.FindC ontrol("ch ck" & CStr(x)), TextBox).Text
chckval = "0" Else
chckval = Gridview1.FindControl("chc k" & CStr(x))
End If
Like this?
So, more like:
For x = 1 To 5 Step 1
dedval = Gridview1.FindControl("ded
chckval = Gridview1.FindControl("chc
chcknumval = Gridview1.FindControl("chc
checkboxval = Gridview1.FindControl("chc
onetimeval = Gridview1.FindControl("one
multival = Gridview1.FindControl("mul
If chckval.Text <> "" And Not checkboxval.Checked Then
cashval = DirectCast(Gridview1.FindC
chckval = "0" Else
chckval = Gridview1.FindControl("chc
End If
Like this?
I don't know what you want as an end result but basically yes. Somewhere you probably want it to have another value.
ASKER
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.
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.
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).
Why not use a string variable which is "" (ie. empty string) and only set the value when you have one (if statement).
ASKER
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?
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?
ASKER
That's precisely what i have done for others here, if you look at my profile.
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.
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.
ASKER
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("chc k" & 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.
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("chc
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.
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
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
ASKER
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
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
ASKER
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.
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.
ASKER
Ok it is working fine now.
I was able to get your initial suggestion of reseting chckval like:
Now, it works like I want it to work.
Thanks alot
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
...
...
Now, it works like I want it to work.
Thanks alot