0

I have following global variables

Dim cardNumber As String
Dim txnAmount As String
Dim TerminalID As String
Dim CurrencyCode As String

And Values are assigned on a click Event from the result set returned from SP

dsCards = Utilities.GetVirtualResultNew(txtCardNumber.Text.Trim)
grdTransactionResultSearch.DataSource = dsCards
grdTransactionResultSearch.DataBind()
cardNumber = IIf(IsDBNull(dsCards.Tables(0).Rows(0).Item("pan")), "", dsCards.Tables(0).Rows(0).Item("pan"))
txnAmount = IIf(IsDBNull(dsCards.Tables(0).Rows(0).Item("TotalAmount")), "", dsCards.Tables(0).Rows(0).Item("TotalAmount"))
TerminalID = IIf(IsDBNull(dsCards.Tables(0).Rows(0).Item("TerminalID")), "", dsCards.Tables(0).Rows(0).Item("TerminalID"))
CurrencyCode = IIf(IsDBNull(dsCards.Tables(0).Rows(0).Item("CurrencyCode")), "", dsCards.Tables(0).Rows(0).Item("CurrencyCode"))

I Debugged the code and I can see the values are assigned to them but when I try to access them in another button click event, They are empty

Here is my button click event where These variables are empty

Protected Sub btnAuthorize_Click(sender As Object, e As EventArgs) Handles btnAuthorize.Click
  Utilities.InsertAuthorizedTransactions(cardNumber, txnAmount, TerminalID, CurrencyCode)
  Label1.Visible = True
END Sub   

Whats the problem with my code?

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
Nuke
  • 1,169
  • 5
  • 16
  • 33

1 Answers1

0

When a button is clicked, then a postback is performed (What is a postback?) which in this case will re-initliase your variables.

The preferred option is probably to store your variables in ViewState (What is ViewState?) in your first button click:

ViewState("cardNumber") = "foo"
ViewState("txnAmount") = "bar"
'etc.

And then access them in your second click.

Note also that you should not use the IIF function in VB.NET but you should instead use the If Operator

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143