problem converting a regular query to parametrized query - vb.net

I came across this old piece of code that looks like this:
Dim sql As String = "exec wa_vterm_Insert_upd_Purchase_Card #merchant_id = '" & db.SqlSafe(sMerchantID) & "'" &
", #reference = '" & db.SqlSafe(reference) & "' " &
", #national_tax = " & nationalTax & " " &
", #customer_vat_number = '" & db.SqlSafe(customerVatNumber) & "' " &
", #discount_amount = " & discountAmount & " " &
", #duty_amount = " & dutyAmount & " " &
", #vat_invoice_number = '" & db.SqlSafe(vatInvoiceNumber) & "' " &
", #vat_tax_amount = " & vatTaxAmount & " " &
", #vat_tax_rate = " & vatTaxRate & " " &
", #destination_country_code = '" & db.SqlSafe(destCountryCode) & "' "
Trace.WriteLine(sql, LogLevel.Debug)
db.ExecuteNonQuery(sql)
I am converting this to parameterized query and I coded this:
Dim cmd As New SqlCommand("wa_vterm_Insert_upd_Purchase_Card", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("#merchant_id", sqlDbType:=SqlDbType.VarChar).Value = sMerchantID
cmd.Parameters.Add("#reference", sqlDbType:=SqlDbType.VarChar).Value = reference
cmd.Parameters.Add("#national_tax", sqlDbType:=SqlDbType.VarChar).Value = nationalTax
cmd.Parameters.Add("#customer_vat_number", sqlDbType:=SqlDbType.VarChar).Value = customerVatNumber
cmd.Parameters.Add("#discount_amount", sqlDbType:=SqlDbType.VarChar).Value = discountAmount
cmd.Parameters.Add("#duty_amount", sqlDbType:=SqlDbType.VarChar).Value = dutyAmount
cmd.Parameters.Add("#vat_invoice_number", sqlDbType:=SqlDbType.VarChar).Value = vatInvoiceNumber
cmd.Parameters.Add(" #vat_tax_amount", sqlDbType:=SqlDbType.Decimal).Value = vatTaxAmount
cmd.Parameters.Add(" #vat_tax_rate", sqlDbType:=SqlDbType.Decimal).Value = vatTaxRate
cmd.Parameters.Add(" #destination_country_code", sqlDbType:=SqlDbType.VarChar).Value = destCountryCode
Try
cmd.Connection.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
Trace.WriteLine(ex.Message & ex.Source, LogLevel.Debug)
Finally
cmd.Connection.Close()
End Try
however with the new piece of code I am getting this exception:
"Procedure or function 'wa_vterm_Insert_upd_Purchase_Card' expects parameter '#vat_tax_amount', which was not supplied."
I don't understand why??

As per the suggestion above the answer was a space in front of the #var_tax_amount

Related

How concatenate recordset variables to use filter method - Vba

I would like to concatenate multiple variables on my recordset filter. Here more information :
rs is recordset
titre can be M. or Mme or S. (come from rs)
Nom is of the form (come from rs) : FirstName LastName (with space between)
but I can't. I tried :
space = " "
rs.Filter = "[titre+ space + Nom] = '" & oLookFullName & "' and nomEntreprise = '" & objContact.CompanyName & "'"
Concatenation = rs!titre + " " + rs!Nom
rs.Filter = "Concatenation = '" & oLookFullName & "'"
Any ideas ?
EDIT
#Gustav, I tried your code with split and it seems the filter contains the correct value but just after I have this if loop and in this case rs.EOF is true while the contact exists... why ?
If rs.EOF Then 'Or (rs.BOF = True) Then
Debug.Print oLookFullName & " is not found."
End If
Try with:
rs.Filter = "[titre] & ' ' & [Nom] = '" & oLookFullName & "' And [nomEntreprise] = '" & objContact.CompanyName & "'"
or:
rs.Filter = "[titre] = '" & Split(oLookFullName, " ")(0) & "' And [Nom] = '" & Split(oLookFullName, " ")(1) & "' And [nomEntreprise] = '" & objContact.CompanyName & "'"

Need Help for Updating database in vb.net

Someone knows why I got an exception when the code running at cmd.ExecuteNonQuery()? when I update Date, it runs perfectly but when I update the Finish date, I got that error.
This is the code
Call connection()
If txt_UpCom.Text = "" Or cbCom.SelectedItem = "" Then
If txt_ComNew.Enabled = True Then
MessageBox.Show("Please fill the entire data!")
End If
Else
Try
Dim str As String
If cbCom.SelectedItem = "Date" Or cbCom.SelectedItem = "Finish Date" Then
Dim comdate As String = dateCom.Value.ToString("yyyy-MM-dd")
str = "UPDATE complain SET " & cbCom.SelectedItem & "= '" & comdate & "' WHERE ComplainID = '" & txt_UpCom.Text & "'"
Else
str = "UPDATE complain SET " & cbCom.SelectedItem & " = '" & txt_ComNew.Text & "' WHERE ComplainID = '" & txt_UpCom.Text & "'"
End If
cmd = New MySql.Data.MySqlClient.MySqlCommand(str, conn)
cmd.ExecuteNonQuery()
msgBoxSuccess.ShowDialog()
conn.Close()
Catch ex As Exception
MessageBox.Show("Unable to Update")
End Try
End If
This is the table in database
As #Jacek Wróbel mentioned in the comments, you should keep the column names between [] (square brackets) or "" (double quots). The same thing goes for table names (or any other SQL objects for that matter).
Long story short, change this part of your code:
If cbCom.SelectedItem = "Date" Or cbCom.SelectedItem = "Finish Date" Then
Dim comdate As String = dateCom.Value.ToString("yyyy-MM-dd")
str = "UPDATE complain SET " & cbCom.SelectedItem & "= '" & comdate & "' WHERE ComplainID = '" & txt_UpCom.Text & "'"
Else
str = "UPDATE complain SET " & cbCom.SelectedItem & " = '" & txt_ComNew.Text & "' WHERE ComplainID = '" & txt_UpCom.Text & "'"
End If
to this:
If cbCom.SelectedItem = "Date" Or cbCom.SelectedItem = "Finish Date" Then
Dim comdate As String = dateCom.Value.ToString("yyyy-MM-dd")
str = "UPDATE `complain` SET `" & cbCom.SelectedItem & "` = '" & comdate & "' WHERE `ComplainID` = '" & txt_UpCom.Text & "'"
Else
str = "UPDATE `complain` SET `" & cbCom.SelectedItem & "` = '" & txt_ComNew.Text & "' WHERE `ComplainID` = '" & txt_UpCom.Text & "'"
End If
Note: I prefer to keep things consistant, so I added [] to each object whether it contains spaces or not.

How to fix update code using short text as data type

Hello I just want to ask about my code. Why its showing error if I change the data type of ID_NO in short text? Btw database that I'm using is MS ACCESS
Dim i As Integer
i = dgMembers.CurrentRow.Index
currentid = dgMembers.Item(1, i).Value.ToString()
ds = New DataSet
adapter = New OleDbDataAdapter("update [FASA_MembersAccount] set ID_No = '" & txtMemberIDNo.Text & "',[FirstName] = '" & txtMemberFirstName.Text & "',[LastName] ='" & txtMemberLastName.Text & "',[Mobile_No] = '" & txtMemberMobileNo.Text & "',[Gender] = '" & cbMemberGender.Text & "',[Birthday] = '" & dtpMember.Text & "',[Password] = '" & txtMemberPassword.Text & "',[Address] = '" & txtMemberAddress.Text & "' where ID_No = " & currentid, conn)
adapter.Fill(ds, "FASA_MembersAccount")
Can somebody help me?
Here's an example of how to get your SQL code so you can run it in your database to check for syntax errors. Does this help?
Dim i As Integer
Dim strSQL as string
i = dgMembers.CurrentRow.Index
currentid = dgMembers.Item(1, i).Value.ToString()
ds = New DataSet
strSQL = "update [FASA_MembersAccount] set ID_No = '" & txtMemberIDNo.Text & "',[FirstName] = '" & txtMemberFirstName.Text & "',[LastName] ='" & txtMemberLastName.Text & "',[Mobile_No] = '" & txtMemberMobileNo.Text & "',[Gender] = '" & cbMemberGender.Text & "',[Birthday] = '" & dtpMember.Text & "',[Password] = '" & txtMemberPassword.Text & "',[Address] = '" & txtMemberAddress.Text & "' where ID_No = " & currentid
'--- Set your breakpoint here and use the immediate window to ?strSQL then copy/paste your command into ms-access
adapter = New OleDbDataAdapter(strSQL, conn)
adapter.Fill(ds, "FASA_MembersAccount")

Save variable to session in VB.net

This is the code inside of my Handler.ashx at the end of this loop I want to save the variable whereclause to the session so it will know what the previous where clause was.
I am open to any way of doing it, but I believe saving to the session would help me complete this.
I tried: Session["whereclause"] = whereclause but it gave me an error
'4.3 Filtering implementation
If Not (FilterTerm = "" Or FilterCol = "") Then
If Not (FilterCol = "[Study Time]") And InStr(whereclause, "WHERE", vbTextCompare) = 0 Then
whereclause = whereclause & " WHERE " & FilterCol & " = '" & FilterTerm & "'"
ElseIf InStr(whereclause, "WHERE", vbTextCompare) = 0 And FilterCol = "[Study Time]" Then
whereclause = whereclause & " WHERE " & FilterCol & " = '12/30/1899 " & FilterTerm & "'"
ElseIf (FilterCol = "[Study Time]") Then
whereclause = whereclause & " AND " & FilterCol & " = '12/30/1899 " & FilterTerm & "'"
Else
whereclause = whereclause & " AND " & FilterCol & " = '" & FilterTerm & "'"
End If
End If

looping with SqlDataAdapter vb.net

I'm trying to loop according to the count of the gridview but it's giving me timeout at the second iteration.
Here's my code
Try
For i = 0 To Charges_GridView.Rows.Count - 2
'check unit price
'here is the error at the second iteration
Dim quantity As String = "SELECT QUANTITY FROM [dbo].[SERVICEITEMCHARGES] WHERE SERVICEITEMFID ='" & txt_FID.Text & "' AND FEESID = " & Charges_GridView.Rows(i).Cells(0).Value & " AND CURRENCY = '" & Charges_GridView.Rows(i).Cells(2).Value & "'"
Dim quantity_Adap As New SqlClient.SqlDataAdapter(quantity, conn)
Dim quantity_dt As New DataTable
quantity_Adap.Fill(quantity_dt)
If quantity_dt(0)(0) = 0 Then
'Update ServiceItemCharges
cmd.CommandText = "Update [dbo].[SERVICEITEMCHARGES] SET [AMOUNT] = " & Math.Ceiling(Charges_GridView.Rows(i).Cells(1).Value * 100) / 100 & ", UNIT_PRICE = 0 WHERE SERVICEITEMFID ='" & txt_FID.Text & "' AND FEESID = " & Charges_GridView.Rows(i).Cells(0).Value & " AND CURRENCY = '" & Charges_GridView.Rows(i).Cells(2).Value & "'"
cmd.Connection = connection
cmd.ExecuteNonQuery()
Else
'Update ServiceItemCharges
cmd.CommandText = "Update [dbo].[SERVICEITEMCHARGES] SET [AMOUNT] = " & Math.Ceiling(Charges_GridView.Rows(i).Cells(1).Value * 100) / 100 & ", UNIT_PRICE = " & (Math.Ceiling(Charges_GridView.Rows(i).Cells(1).Value * 100) / 100) / quantity_dt(0)(0) & " WHERE SERVICEITEMFID ='" & txt_FID.Text & "' AND FEESID = " & Charges_GridView.Rows(i).Cells(0).Value & " AND CURRENCY = '" & Charges_GridView.Rows(i).Cells(2).Value & "'"
cmd.Connection = connection
cmd.ExecuteNonQuery()
End If
Next
thanks for your help