473,385 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help me clean up my DB connection (trying to use parameterized query)

I'm tring to modify an existing DB connection/query into a parameterized
one. But, in doing so, I get an error stating that I am not defining the
variable I am using in the query.

This is what I have:

================================================== ===============

Dim DS As New DataSet

Dim strConnect As String
strConnect =
System.Configuration.ConfigurationSettings.AppSett ings("DBConn")

Dim strChk As String
strChk = "SELECT CAT.categoryID, CAT.categoryParentID, CAT.categoryName,
PARENTCAT.categoryName AS parentCategoryName FROM categories CAT LEFT OUTER
JOIN categories PARENTCAT ON CAT.categoryParentID = PARENTCAT.categoryID
WHERE(CAT.categoryID = @categoryID)"

Dim objConnect As New System.Data.OleDb.OleDbConnection(strConnect)
objConnect.Open()

Dim objCommand As New System.Data.OleDb.OleDbCommand(strChk, objConnect)
objCommand.Parameters.Add("@categoryID", categoryID)

Dim objOleDbAdapter As New System.Data.OleDb.OleDbDataAdapter(strChk,
strConnect)
objOleDbAdapter.Fill(DS, "webPages")

================================================== ===============

I'm guessing it has to do with my Connection not passing the command to the
DB?

-Darrel
Feb 23 '06 #1
5 1125
try:

objCommand.Parameters.Add("@categoryID", OldDb.Numeric).Value = categoryId
karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"darrel" <no*****@nowhere.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
I'm tring to modify an existing DB connection/query into a parameterized
one. But, in doing so, I get an error stating that I am not defining the
variable I am using in the query.

This is what I have:

================================================== ===============

Dim DS As New DataSet

Dim strConnect As String
strConnect =
System.Configuration.ConfigurationSettings.AppSett ings("DBConn")

Dim strChk As String
strChk = "SELECT CAT.categoryID, CAT.categoryParentID, CAT.categoryName,
PARENTCAT.categoryName AS parentCategoryName FROM categories CAT LEFT
OUTER JOIN categories PARENTCAT ON CAT.categoryParentID =
PARENTCAT.categoryID WHERE(CAT.categoryID = @categoryID)"

Dim objConnect As New System.Data.OleDb.OleDbConnection(strConnect)
objConnect.Open()

Dim objCommand As New System.Data.OleDb.OleDbCommand(strChk, objConnect)
objCommand.Parameters.Add("@categoryID", categoryID)

Dim objOleDbAdapter As New System.Data.OleDb.OleDbDataAdapter(strChk,
strConnect)
objOleDbAdapter.Fill(DS, "webPages")

================================================== ===============

I'm guessing it has to do with my Connection not passing the command to
the DB?

-Darrel

Feb 23 '06 #2
> objCommand.Parameters.Add("@categoryID", OldDb.Numeric).Value = categoryId

Karl, thanks. Alas, OldDb isn't declared in my app. What is that referring
to?

-Darrel
Feb 24 '06 #3
It's a typo :)

OleDbType is what it should say.

sorry about that

karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"darrel" <no*****@nowhere.com> wrote in message
news:OD****************@TK2MSFTNGP15.phx.gbl...
objCommand.Parameters.Add("@categoryID", OldDb.Numeric).Value =
categoryId


Karl, thanks. Alas, OldDb isn't declared in my app. What is that referring
to?

-Darrel

Feb 24 '06 #4
OleDbType is what it should say.


Oh...ha! I should have figured that one out. I was thinking Old Database?
;o)

objCommand.Parameters.Add("@categoryID",
System.Data.OleDb.OleDbType.Numeric).Value = categoryID

I assume that this adds one more step to the data validation process,
telling the parameter what format it is in?

Unfortunately, I am still getting a "Must declare the variable
'@categoryID'. Microsoft OLE DB Provider for SQL Server" error. Is the
problem perhaps in my SQL query syntax?

strChk = "SELECT CAT.categoryID, CAT.categoryParentID, CAT.categoryName,
PARENTCAT.categoryName AS parentCategoryName FROM We_about_categories CAT
LEFT OUTER JOIN We_about_categories PARENTCAT ON CAT.categoryParentID =
PARENTCAT.categoryID WHERE(CAT.categoryID = @categoryID)"

-Darrel
Feb 24 '06 #5
it might..

if you don't specify it, .NET goes through quite a bit of trouble infering
the type from the value. I've tried to following the internal code from the
SqlCommand and it gets quite complicated (and in some cases slow)..so that's
why i mostly include it.

Karl
--
http://www.openmymind.net/
http://www.fuelindustries.com/
"darrel" <no*****@nowhere.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
OleDbType is what it should say.


Oh...ha! I should have figured that one out. I was thinking Old Database?
;o)

objCommand.Parameters.Add("@categoryID",
System.Data.OleDb.OleDbType.Numeric).Value = categoryID

I assume that this adds one more step to the data validation process,
telling the parameter what format it is in?

Unfortunately, I am still getting a "Must declare the variable
'@categoryID'. Microsoft OLE DB Provider for SQL Server" error. Is the
problem perhaps in my SQL query syntax?

strChk = "SELECT CAT.categoryID, CAT.categoryParentID, CAT.categoryName,
PARENTCAT.categoryName AS parentCategoryName FROM We_about_categories CAT
LEFT OUTER JOIN We_about_categories PARENTCAT ON CAT.categoryParentID =
PARENTCAT.categoryID WHERE(CAT.categoryID = @categoryID)"

-Darrel

Feb 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gary b | last post by:
Hello When I use a PreparedStatement (in jdbc) with the following query: SELECT store_groups_id FROM store_groups WHERE store_groups_id IS NOT NULL AND type = ? ORDER BY group_name
2
by: moosedeja | last post by:
I have an ASP (IIS 4.0) based website fronting a SQL Server 2000 database. I am trying to avoid using dynamic SQL for queries, and I am considering prepared statements as an alternative... All...
7
by: William Gill | last post by:
I have been trying to pass parameters as indicated in the api. when I use: sql= 'select * from %s where cusid = %s ' % name,recID) Cursor.execute(sql) it works fine, but when I try : sql=...
2
by: deko | last post by:
Is it possible to build a parameterized query from another parameterized query? I've tried two variations of this and can't seem to get it to work (using DAO). Any suggestions welcome! I...
4
by: Chuck Haeberle | last post by:
I have an interesting regular expression challenge for someone more experienced with them than I for a data layer class... I need an expression to search a SQL statement (any type, SELECT INSERT...
15
by: DavidS | last post by:
Have Visual Studio.NET installed on MS 2000 Professional OS laptop. No issue ever with web development and SQL connections. Purchased new laptop with XP Professional SP2!!!!!!!! & Visual...
7
by: Steve Franks | last post by:
Using VS.NET 2005 RC - Am I understanding correctly from the docs that connection pooling for my ADO.NET db connections will happen automatically? Do I need to do anything to enable this feature? ...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.