ADO Parameter Error in VBScript - sql-server

I have tried many, many things, but keep getting error 3001 (Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another) when trying to add parameters to a command object.
Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = con
cmd.CommandText = "ProcName"
cmd.CommandType = 4 'adCmdStoredProc
MsgBox("0")
'cmd.Parameters.Append(cmd.CreateParameter("#InvoiceNumber", adVarChar, adParamInput, 100, sInvoice))
Set pInvoiceNumber = cmd.CreateParameter("#InvoiceNumber", adVarChar, adParamInput, 100, sInvoice)
cmd.Parameters.Append(pInvoiceNumber)
The connection object is valid and open at the time that this code runs. The #InvoiceNumber parameter of the stored procedure is a varchar(100). What am I missing here?

I'm not able to replicate the issue, but I'm working in Classic ASP. I found this post which sounds like it may be relevant though.
VBA: Run-time error 3001 Arguments Are Of The Wrong Type... when setting ADODB.Command object members
It seems the adVarChar and adParamInput constants may be the problem due to late binding. The resolution was to add the constants to the Sub header.
If that's not feasible, try using Oracle Certified Professional's suggestion of Refresh
Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = con
cmd.CommandText = "ProcName"
cmd.CommandType = 4 'adCmdStoredProc
cmd.Parameters.Refresh
cmd.Parameters(0).Value = sInvoice

I solved the problem with include library ADOLib.inc on top of the asp.page

Related

How to code find and replace button using sql server 2008 & VB.Net

My app is using datagridview to display data from sqlserver.
I am working on a button code that will enable the user to find and replace the entire column. Eg find Class1 and replace with Class2.
something like UPDATE students SET class = combo1.text with combo2,text WHERE%
I am feeling too far from reality. How can this code work?
You want to set your class to the value of Combo2, where the value already matches the value of Combo1.
"UPDATE students
SET class = "+combo2.text+"
where class = "+combo1.text+" "
Copy paste this code (taken from msdn)
Public Sub ExecuteQuery(ByVal queryString As String, _
ByVal connectionString As String)
Using connection As New SqlConnection(connectionString)
Dim command As New SqlCommand(queryString, connection)
command.Connection.Open()
command.ExecuteNonQuery()
End Using
End Sub
.......
and now when you need to execute you query write this
Dim cn as String
Dim UpdateQry as String
cn = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\eDataba‌​se.mdf;Integrated Security=True;User Instance=True"
UpdateQry = "Update students SET class = '" + combo2.text + "' WHERE class = '" + combo1.text + "'"
ExecuteQuery(UpdateQry, cn)
hope this helps you.
regards

public function to create and open a database connection

I have a client/server desktop application that I am having some database connection issues with on some of my clients pc's. When I wrote the app, I didn't know any better so I created and opened 1 database connection on application startup, and used that same connection all throughout the app. I know realize this is a bad idea since shaky network connections and it seems antivirus programs are causing these connection to be dropped at times, leading to some errors. I have hundreds of places in code where I need to go back and create/open/close the connection at the time they are being used.
The question is, is there any way to create a public function in which I can do just that, and then do a global find and replace to replace the connection name with the new function name?
something like:
Dim qry As NpgsqlCommand
sqlUpdateItem = "update table set field = value where id = 1"
qry = New NpgsqlCommand(sqlUpdateItem, con)
qry.ExecuteNonQuery()
to
Dim qry As NpgsqlCommand
sqlUpdateItem = "update table set field = value where id = 1"
qry = New NpgsqlCommand(sqlUpdateItem, newCon())
qry.ExecuteNonQuery()
public function newCon()
Dim con As New NpgsqlConnection(connectionString)
con.Open()
Return tcon
End Function
I tried this but no luck. I'm just looking for any possible solutions that don't involve me updating several lines of code in hundreds of places throughout my app. The nice thing is I would only need to do this for all commands, since I can pass a brand new connection into a data adapter and it will handle the opening/closing.
Here's an example of how I'd recommend you attempt it.
Enable option strict in your project. It's better to have your errors at compile time than at runtime.
Use a using statement to safely dispose of the database classes even if you get an exception.
Private _connectionString As String = "blah"
Public Function GetDbConnection() As NpgsqlConnection
Dim con As New NpgsqlConnection(_connectionString)
con.Open()
Return con
End Function
Public Sub DoMyQuery()
Using conn = GetDbConnection()
Using qry = New NpgsqlCommand("update table set field = value where id = 1", conn)
qry.ExecuteNonQuery()
End Using
End Using
End Sub

what is the best way to add DateTimePicker data to database

Below is the code i am using for my bindings but is giving me an error and its to do with the datatimepicker as if i remove this and add a textbox i get no error!! So obviously its something i am doing, so what is the best way to get the data from the datatimepicker and insert it into the database and with that said, on form load what is the value i need to grab the new value from that row to the datatimepicker.
Insert code:
Private Sub AddOrder()
If con.State = ConnectionState.Closed Then con.Open()
cmd = New OleDbCommand
cmd.Connection = con
cmd.CommandText = "INSERT INTO tblo(SDate, EDate)VALUES(?, ?);"
cmd.Parameters.Add("?", OleDbType.Date).Value = DTS.Value
cmd.Parameters.Add("?", OleDbType.Date).Value = DTE.Value
cmd.ExecuteNonQuery()
con.Close()
End Sub
bindings:
DTS.DataBindings.Add("value", bsorder, "SDate")
DTE.DataBindings.Add("value", bsorder, "EDate")
Is this the only way to do this? It gives me a error when i use this way, if i just use textboxes for adding time it works so its something to do with my way of doing this DataTimePicker,
Please help thanks!
It seems that when trying to databind to a DateTimePicker it didnt like the whole Value even though it is a valid property as there is no "Text" within this. Anyway, when i change it to:
DTS.DataBindings.Add("Text", bsorder, "SDate")
DTE.DataBindings.Add("Text", bsorder, "EDate")
My error/issue goes away! It use to be "Value" instead of text but some reason it likes text now Value. I dont have a detailed report as to why or how this works? I just trial and error it and worked a charm now!

ASP Classic Getting and Setting the Conn to another Conn object

I am using this way to connect and use the Conn object for connections
<!-- #include file="dataconnections.asp" -->
In one of my ASP pages, I have a VBScript Onclick event that needs me to execute a stored procedure and pass the variables in the textboxes.
Right now, the Connection String is hardcoded in the ASP File itself and is not using the Conn object from the include file. Is there any proper way to execute the SP without using a new Conn, or how can I pass the current Conn from the include file to the one being used in the VBScript Onclick event?
I don't know what your include dataconnections.asp contains (would have been nice if you had posted the source code in your question) but a stored procedure is best run using the ADODB.Command object which exposes the ActiveConnection property.
This allows you to either Set your ADODB.Connection object or pass your connection string and let the ADODB.Command instantitate a new ADODB.Connection using that connection string.
Here are examples of using the two options
Use existing ADODB.Connection
Dim cmd
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
'Use existing object from include file to set connection.
Set .ActiveConnection = YourConnObject
End With
IMPORTANT: When using this method make sure the YourConnObject (ADODB.Connection) should have been opened using
'Make sure connection has been opened.
Call YourConnObject.Open()
before assigning to the ADODB.Command object.
Use connection string variable (probably defined in your include) to set the connection that will be exclusively used by the ADODB.Command object.
Dim cmd
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
'Use connection string to instantiate a new connection to be used
'exclusively by the ADODB.Command object.
.ActiveConnection = YourConnectionString
End With
NOTE: Unlike option 1 calling
'Close ADODB.Command object
Call cmd.Close()
Will instantaneously destroy the ADODB.Connection (that was assigned while setting the ActiveConnection property).
Useful Links
Using Stored Procedure in Classical ASP .. execute and get results

delete record from database in listview in vb

I have a problem. In the properties of listview which is checkboxes = "True". Using this checkbox, I want to delete the data in listview and in the database.
Below is the code:
If MessageBox.Show("Do you really want to DELETE this record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation cancel", MsgBoxStyle.Information, "Information")
End If
dbSource = "Data Source=LAILATUL-PC\SERVER;Initial Catalog=HotelManagementSystem;Integrated Security=True"
Dim sql As String = "DELETE FROM [Room] WHERE Room_Code = #code"
Using con = New SqlConnection(dbSource)
Using cmd = New SqlCommand(sql, con)
con.Open()
For Each lvItem As ListViewItem In ListViewRoom.Items
If lvItem.Checked Then
cmd.Parameters.AddWithValue("#code", ColumnRoomCode.Text)
cmd.ExecuteNonQuery()
lvItem.Remove()
End If
Next
End Using
End Using
Using above code, only the data in listview is deleted. The data in the database not deleted.
The interface for listviewitem:
Thank you if you all can help me. :)
A Command should be executed to have any effect on the database. You need to add this
cmd.ExecuteNonQuery()
in every loop.
Also the connection could be opened just before entering the loop and should be closed afterward. (Using Statement is recommended here)
Said that, please take a look on Parameterized queries because your code is open to Sql Injections and parsing problems. Also the sql command to delete a record doesn't need a list of fields after the FROM table part.
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
dbSource = "Data Source=LAILATUL-PC\SERVER;Initial Catalog=HotelManagementSystem;Integrated Security=True"
' Parameterized query
Dim sql As String = "DELETE FROM [Room] WHERE Room_Code = #code"
' Using statement to ensure proper closing and disposing
' of the objects SqlConnection and SqlCommand
using con = New SqlConnection(dbSource)
using cmd = New SqlCommand(sql, con)
con.Open()
' Add a parameter just one time before the loop with an empty string
cmd.Parameters.AddWithValue("#code", "")
For Each lvItem As ListViewItem In ListViewRoom.Items
If lvItem.Checked Then
' Set the parameter value with the value extracted from the ListViewItem
Dim RoomCode = lvItem.Text
cmd.Parameters("#code").Value = RoomCode
cmd.ExecuteNonQuery()
lvItem.Remove()
End If
Next
End Using
End Using
End Sub
One last note. The ColumnRoomCode textbox (?) is always the same, so calling delete one time is enough, but I suppose that this should be changed with some value extrated by you current ListViewItem

Resources