0

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?

2 Answers 2

1
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?

Sign up to request clarification or add additional context in comments.

4 Comments

Yes. It is something like that. Thanks mate
Actually this is not working, this is always return me "Deposit" instead of "Withdrawal" when it is the case
@Leprechaun: Your Answer is right but use the IIF function in vb.net for reduce the code.
@IGottaGo I guess you should use ... = If(string.IsNullOrEmpty(cmdDepositar.Text) ... or even string.IsNullOrWhiteSpace to check if there is a text in your box.
0
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

3 Comments

I like that method too. Thanks mate
This is not the valid VB .NET syntax for conditional operator.
@IGottaGo: Please use iif() for check the condition for write full If statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.