Is there any code to change value of dim function

170 Views Asked by At

I want to put data in SQL table through vb.net in two columns which are Txn_Amount and Post_Amount

where textbox3 = Txn_Amount

Post Amount = Textbox4 - textbox3

but I want if textbox4 = "" than Post amount should be 0

This is my code:

Call Get_TxnID()
        
        Dim Txn_Amount As String = TextBox3.Text
          Dim Post_Amount As String = Val(TextBox4.Text) - Val(TextBox3.Text)
       

        Dim query As String = "Insert into Txn_Master values (@Txn_Amount,  @Post_Amount)"
        Using cmd As New SqlCommand(query, Connection)
            cmd.Parameters.AddWithValue("Txn_Amount", Txn_Amount)
            cmd.Parameters.AddWithValue("Post_Amount", Post_Amount)
            
            Connection.Open()
            cmd.ExecuteNonQuery()
            Connection.Close()

        End Using
        MsgBox("Transaction Success", MsgBoxStyle.Information)

It work well when i have value in both boxes For example :- textbox3.text = 25000 and textbox4.text = 50000 then Post_Amount is 25000

but if textbox3.text = 25000 and textbox4.text = "" then it shows -25000 in post_amount but i want if textbox4 = "" then post amount should be "" or "0"

I have tried

Dim Txn_Amount As String = TextBox3.Text
      If textbox4.text="" then
          Dim Post_Amount As String = ""
      Else
          Dim Post_Amount As String = Val(TextBox4.Text) - Val(TextBox3.Text)
      endif
       
        Dim query As String = "Insert into Txn_Master values (@Txn_Amount,  @Post_Amount)"
        Using cmd As New SqlCommand(query, Connection)

         
            cmd.Parameters.AddWithValue("Txn_Amount", Txn_Amount)
            cmd.Parameters.AddWithValue("Post_Amount", Post_Amount)
            
            Connection.Open()
            cmd.ExecuteNonQuery()
            Connection.Close()
        End Using
        MsgBox("Transaction Success", MsgBoxStyle.Information)

But it is now working, please help me with this

1

There are 1 best solutions below

0
On

If you initialise a variable for "Post_Amount" to zero, then you can check if the appropriate TextBox has an entry before setting its value, something like this:

Dim txnAmount As Integer = 0

If Not Integer.TryParse(tbTxnAmount.Text, txnAmount) Then
    ' Prompt user to enter an appropriate value in the TextBox.
    ' Exit Sub
End If

Dim postAmount As Integer = 0

'TODO Use sensible names for tbAmountA and tbAmountB.
If Not String.IsNullOrWhiteSpace(tbAmountB.Text) Then
    'TODO: Use sensible names for these variables.
    Dim a = 0
    Dim b = 0
    If Integer.TryParse(tbAmountA.Text, a) AndAlso Integer.TryParse(tbAmountB.Text, b) Then
        postAmount = b - a
    End If
End If

Using conn As New SqlConnection("your connection string")
    Dim sql = "INSERT INTO [Txn_Master] VALUES (@Txn_Amount, @Post_Amount)"
    Using cmd As New SqlCommand(sql, conn)
        cmd.Parameters.Add(New SqlParameter With {.ParameterName = "@Txn_Amount",
                                                  .SqlDbType = SqlDbType.Int,
                                                  .Value = txnAmount})

        cmd.Parameters.Add(New SqlParameter With {.ParameterName = "@Post_Amount",
                                                  .SqlDbType = SqlDbType.Int,
                                                  .Value = postAmount})

        conn.Open()
        cmd.ExecuteNonQuery()
        cmd.Clone()

    End Using

End Using

I strongly recommend that you use meaningful names for the TextBoxes and variables. "tbAmountB" is your "TextBox4", but it still needs a better name.

Strictly speaking, it doesn't need the String.IsNullOrWhiteSpace test as such a string would fail the parsing, but it does leave the intent clear.

Also, to make your code easier for others to read, it is convention to use camelCase for variable names: Capitalization Conventions.