尝试在使用发票时 vb.net btnsubmit_click功能上显示警告消息



sql 脚本正在验证发票类型和发票的使用。我需要在 If (Me.txtMasterId.Text <> "0"( 方法中添加检查和消息。

Protected Sub btnsubmit_Click(sender As Object, e As EventArgs) Handles btnsubmit.Click
If (Not DataForm.ValidateForm) Then
TransRepo.ModifyStatusName(gUser.CompanyId, Me.txtMasterId.Text, Me.txtStatusName.Text, chkIsPmtPlanFee.Checked, chkIsAttorneyFee.Checked, Me.cboStatus.SelectedValue, Me.txtId.Text, ParseDec(Me.txtExpenseLimit.Text), chkUserSpecPayTo.Checked)
ShowPanel(Me.pnlItemList, Me.Form)
ListOptions(Me.txtMasterId.Text)
SetPageHeader("List Status Name")
If (Me.txtId.Text = "0") Then
ShowConfirmation(Me.lblItemFrmMsg, "New name Added")
If (Me.txtMasterId.Text <> "0") Then
ShowConfirmation(Me.lblItemFrmMsg, "Status Name Saved")
Else
DataForm.DisplayError(Me.lblFrmErr)
End If
End If
End If
End Sub
Dim cn As MySqlConnection
Dim rdr As MySqlDataReader
Try
Dim Sql = "SELECT invoice_type.id
FROM invoice_type_fee
INNER JOIN invoice_name ON invoice_type.id = invoice_type_fee.`id`
WHERE invoice_name.`is_active` = 1
AND invoice_type_fee.`is_active` = 1
AND (invoice_type_fee.`fee_invoice_id` = @invoiceTyID
OR invoice_type_fee.`fee_late_invoicetype_id` = @invoiceTypeID LIMIT 1"
rdr.Read()
If (rdr.HasRows) Then
Dim message As String = "....."
Status.Visible = False
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & message & "');", True)
'If (invoicetype = "1") Then
End If
Catch ex As Exception
Throw
End Try
End Sub

没有读者。你从不打电话给ExecuteReader所以没有读者可以阅读。您似乎认为执行SQL的是Read,但事实并非如此。ExecuteReader执行 SQL,然后Read用于从结果集中获取数据。我刚刚意识到在你的情况下甚至没有命令。您需要创建一个命令来执行,执行它,然后读取结果

但是,获取任何数据都没有意义,因为您关心的只是是否有数据,而不是它是什么。这意味着在这种情况下,您只需要HasRows,例如

Using connection As New MySqlConnection("connection string here"),
command As New MySqlCommand(sql, connection)
connection.Open()
Using reader = command.ExecuteReader()
If reader.HasRows Then
'The query produced a non-empty result set.
End If
End Using
End Using

更好的选择是向 SQL 添加一个COUNT,调用ExecuteScalar然后检查结果是否为零。

Dim Sql = "SELECT COUNT(invoice_type.id)
FROM invoice_type_fee
INNER JOIN invoice_name ON invoice_type.id = invoice_type_fee.`id`
WHERE invoice_name.`is_active` = 1
AND invoice_type_fee.`is_active` = 1
AND (invoice_type_fee.`fee_invoice_id` = @invoiceTyID
OR invoice_type_fee.`fee_late_invoicetype_id` = @invoiceTypeID LIMIT 1"
Using connection As New MySqlConnection("connection string here"),
command As New MySqlCommand(sql, connection)
connection.Open()
If CInt(command.ExecuteScalar()) > 0 Then
'There are matching records.
End If
End Using

最新更新