是否有修改dim函数值的代码?



我想通过vb.net在两列Txn_AmountPost_Amount中放置数据

wheretextbox3 = Txn_Amount

发帖金额=Textbox4 - textbox3

但是我想如果textbox4 = ">

这是我的代码:

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)

当我在两个框中都有值时,它工作得很好,例如:- textbox3。文本= 25000和textbox4。text = 50000 then Post_Amount is 25000

但是如果textbox3。文本= 25000和textbox4。Text = "然后在post_amount中显示-25000但是我想让textbox4 = "那么发帖金额应该是";&;或"0">

我试过了

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)

但它现在工作,请帮助我这个

如果初始化一个变量为"Post_Amount"为零,那么您可以在设置相应的值之前检查相应的TextBox是否有条目,如:

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

我强烈建议您为文本框和变量使用有意义的名称。"tbAmountB"是你的"TextBox4",但它仍然需要一个更好的名字。

严格来说,不需要String。IsNullOrWhiteSpace test作为这样一个字符串会导致解析失败,但它确实使意图清晰。

另外,为了使你的代码更容易让其他人阅读,习惯上使用camelCase来表示变量名:

相关内容

  • 没有找到相关文章

最新更新