"SELECT into statement" inside loop not working - sql

I have a select into statement that works, but when i put it inside a Loop, it fails.
Does anyone have any idea why this is happening??
Thank you!
Here's the code:
For o = 0 To x
tables = ListBox1.Items.Item(o).ToString
sqlstr = "SELECT * into dbo." & tables & " FROM OPENROWSET('MSDASQL', 'Driver=Microsoft Visual FoxPro Driver; SourceDB=C:\exports\DBF; SourceType = DBF ', 'SELECT * FROM " & tables & "')"
mycmd = New SqlCommand
sqlcon = New SqlConnection(con)
mycmd.CommandText = sqlstr
sqlcon.Open()
mycmd.Connection = sqlcon
mycmd.ExecuteNonQuery()
sqlcon.Close()
Next
When i try to execute this it gives me following error
Cannot execute the query "SELECT * FROM AFED" against OLE DB provider "MSDASQL"
for linked server "(null)". OLE DB provider "MSDASQL"
for linked server "(null)" returned message
"[Microsoft][ODBC Visual FoxPro Driver]File 'afed.dbf' does not exist.".

Please check your foxpro db .. I think afed table does not exist in foxpro DB.and that is why you are not getting the error when put static value instead of o.

Related

Delete query in MS Access throws error data type mismatch

What's wrong with my query below?
sqlquery = "DELETE FROM tblProductTransDetail WHERE (InvoiceNo) = #inv"
With sqlcommand
.CommandText = sqlquery
.Parameters.AddWithValue("#inv", Val(TextBoxNoNota.Text))
.Connection = FormMain.conn
.ExecuteNonQuery()
End With
also tried :
"DELETE FROM tblProductTransDetail WHERE InvoiceNo = #inv"
and some other but none worked.
My InvoiceNo is number type in database (MS Access 2016).
Can you please give this a shot instead? Also assuming you have an import to System.Data.OleDb at the top.
The Convert.ToInt32 makes the text a integer (assuming its a number and not text). The invParam is enforced to have a data type of integer.
sqlquery = "DELETE FROM tblProductTransDetail WHERE (InvoiceNo) = ?"
With sqlcommand
.CommandText = sqlquery
Dim invParam as new OleDbParameter("InvoiceNo", OleDbType.Integer)
invParam.Value = Convert.ToInt32(TextBoxNoNota.Text)
.Parameters.Add(invParam)
.Connection = FormMain.conn
.ExecuteNonQuery()
End With
You used the SQL query not the MS ACCESS query.
You should try this
sqlquery = "DELETE * FROM tblProductTransDetail WHERE (InvoiceNo) = #inv"
Delete query in SQL doesn't have *, But in Access has an * in delete query
however i can make it run properly by using standard delete query without parameter
sqlquery = "DELETE FROM tblProductTransDetail WHERE InvoiceNo = " & cleansql(TextBoxNoNota.Text)
Note: cleansql is my function to clean text and prevent injection.

Inserting Records Into Access Database Using Sql in Vb.net [duplicate]

I'm writing a simple page in ASP.net to update a password in a Access table. Here's syntax for a SELECT query that works:
Dim dbConn As OleDbConnection
Dim dbCommand As OleDbCommand
Dim dbReader As OleDbDataReader
'Connect to db
dbConn = New OleDbConnection(Application("strConnectionString"))
dbConn.Open()
'Get user info
strSQL = "SELECT * FROM users WHERE Username = '" & strUsername & "';"
dbCommand = New OleDbCommand(strSQL, dbConn)
dbReader = dbCommand.ExecuteReader()
And my connection string:
Application("strConnectionString") = _
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strDBPath & _
"; Jet OLEDB:Database Password=" & strDBPassword & ";"
The SELECT works fine, so I know my connection is OK. But this query gives me a syntax error:
strSQL = "UPDATE users SET Password = '1'"
With everything else the same, the ASP error says there is an error with my syntax. But when I response.write the strSQL line, it gives me this:
UPDATE users SET Password = '1'
and when I paste this into a query editor in Access, the query updates all 'Password' field in the table to '1', so I know the syntax is OK. I tried it without the datareader and using dbCommand.ExecuteNonQuery(), same result.
I've got the permissions on the Access file set so that everybody has full control, so I don't think it's a permission problem.
Can anybody see my mistake? I'm really stuck. Thanks.
PASSWORD is a reserved word in Access SQL so you need to enclose it in square brackets if it is a column name.
strSQL = "UPDATE users SET [Password] = '1'"

ERROR [HY000] [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt in VB.net

Hello I have problem with vb.net, when I click that button, it always show error
ERROR [HY000] [Microsoft][ODBC SQL Server Driver]Connection is busy
with results for another hstmt
Anyone can help me?
Private Sub cmbposting_Click(sender As Object, e As EventArgs) Handles cmbposting.Click
cmd = New OdbcCommand("select * from stok_master where stok_cd='" & txtstok_cd.Text & "'", conn)
rd = cmd.ExecuteReader
rd.Read()
If rd.HasRows Then
Dim kurangistok As String = "Update stok_master set " & _
"qty='" & rd.Item(6) - Val(txtreqqty.Text) & "' " & _
"where stok_cd='" & txtstok_cd.Text & "'"
cmd = New OdbcCommand(kurangistok, conn)
**cmd.ExecuteNonQuery()**
End If
End Sub
As per Microsoft
When accessing an SQL Server database as an ODBC data source in Visual
Basic versions 3.0 or 4.0 with the Microsoft Access version 2.0
Compatibility Layer installed, error message 3146 may occur:
ODBC-call failed. [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt [#0]
This error is the result of the SQL Server ODBC driver. The driver can
only handle one active statement at a time. The statement remains
active until all the rows are fetched.
You can possibly complete reading all the rows, load it to a DataTable and then iterate through it to update the table again.
Something like
var dataReader = cmd.ExecuteReader();
var dataTable = new DataTable();
dataTable.Load(dataReader);
And then iterate through the dataTable like
foreach(DataRow row in dataTable.Rows)
{
.......
}

Configuring server connection with database in vb.net

I am connecting to the MS Access server with the following code.
Dim cnn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=162.222.225.78;Database=CRM.mdb;Integrated Security=SSPI;User ID=corpopef;Password=********;")
Dim cmd As New OleDb.OleDbCommand
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
cmd.Connection = cnn
cmd.CommandText = "INSERT INTO Addressbook(srno) " & _
"VALUES('" & Me.TextBox1.Text & "')"
cmd.ExecuteNonQuery()
But it results in the following error:
"Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done."
Please suggest to me some methods to work around this problem.
Thanks in advance...!
The connection string you used seems like an SQL client connection string. For an access database, you should use this format:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=path_to_mdb_file\CRM.MDB;User Id=user_id; Password=password;

Error executing sql expression ASP

I'm attempting to use a variable for the column field in the SQL statement however it shoots back the following error at me:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC Text Driver] Syntax error (missing operator) in query expression '= 'Yes''.
/junk/dbresults.htm, line 31
THE CODE:
<%
Dim connectString, connect, conDB, con
connectString = "Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=" & Server.MapPath("data")
src_abn = Request.QueryString("abn")
src_cat = Request.QueryString("cat")
set connect = Server.CreateObject("ADODB.connection")
connect.open connectString
if src_abn = "all" then
conDB = "SELECT * FROM cont.csv WHERE " & src_cat & " = 'Yes'"
else
conDB = "SELECT * FROM cont.csv WHERE ucase(abn) LIKE ucase('%"+src_abn+"%')"
end if
set con = connect.execute(conDB)
%>
Looks like the value of src_cat contains spaces or other characters that create an invalid query. Try changing that line to this (with brackets):
conDB = "SELECT * FROM cont.csv WHERE [" & src_cat & "] = 'Yes'"
Fixed it, the variables were not delcared.