user6504302
user6504302

Reputation:

SQL parameters with if statement vb.net

So I am developing an application which is based on deposit and withdrawal money from an account(saving account and checking account). To do that, I've created a database where the values will be recorded with SQL Parameters.

But now I got stuck in one of the parameters .Add("@descriptionTransaction", SqlDbType.Char).Value = cmdDeposit.Text, because if I click on Deposit button it will be recorded in descriptionTransaction column as Deposit. In the other hand, if I do the same for the Withdrawal button of course it does not work because the cmdWithdrawal.Text is not declared. I should create an if statement whenever the user click on cmdDeposit it will write Deposit or cmdWithdrawal it will write Withdrawal.

This is the code that I am actually using for the sql parameters:

    With SqlCmd.Parameters
         .Add("@tpAccount", SqlDbType.Char).Value = cbTpAccount.SelectedItem
         .Add("@dateTransaction", SqlDbType.DateTime).Value = txtDate.Text
         .Add("@descriptionTransaction", SqlDbType.Char).Value = cmdDeposit.Text
         .Add("@amountTransaction", SqlDbType.Char).Value = txtWithdrawal.Text
         .Add("@balanceTransaction", SqlDbType.Money).Value = txtBalance.Text
    End With

There is any way to make this If statement work?

Upvotes: 0

Views: 360

Answers (2)

Chetan Sanghani
Chetan Sanghani

Reputation: 2111

With SqlCmd.Parameters
     .Add("@tpAccount", SqlDbType.Char).Value = cbTipoConta.SelectedItem
     .Add("@dateTransaction", SqlDbType.DateTime).Value = txtDate.Text
     .Add("@descriptionTransaction", SqlDbType.Char).Value = iif(String.IsNullOrEmpty(cmdDepositar.Text), "", cmdDepositar.Text)
     .Add("@amountTransaction", SqlDbType.Char).Value = txtDeposito.Text
     .Add("@balanceTransaction", SqlDbType.Money).Value = txtBalance.Text
End With

Try this

Upvotes: 0

Leprechaun
Leprechaun

Reputation: 349

With SqlCmd.Parameters
     .Add("@tpAccount", SqlDbType.Char).Value = cbTipoConta.SelectedItem
     .Add("@dateTransaction", SqlDbType.DateTime).Value = txtDate.Text
     .Add("@descriptionTransaction", SqlDbType.Char).Value = if(cmdDepositar.Text="", cmdWithdrawal.Text, cmdDepositar.Text)
     .Add("@amountTransaction", SqlDbType.Char).Value = txtDeposito.Text
     .Add("@balanceTransaction", SqlDbType.Money).Value = txtBalance.Text
End With

Is this what you are looking for?

Upvotes: 1

Related Questions