Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / March 2008

Tip: Looking for answers? Try searching our database.

Must declare the scalar variable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
m1c - 19 Mar 2008 02:31 GMT
Crap - getting the SOS. This is an old post, so I'll try to recycle it
as new with hopefully enough details.
Here's the relevant code causing this error
SQLOfficeData.ExecuteNonQuery()<-----ERROR [42000] [Microsoft][SQL
Native Client][SQL Server]Must declare the scalar variable
"@dbOfficeName".
at the end of all of this:

Dim dbConn = New
System.Data.Odbc.OdbcConnection("Dsn=Election;description=Election;trusted_­
connection=Yes;app=Microsoft®
Visual Studio® 2008;wsid=M1CVISTA1;database=Election")

       Dim CString As String
       Dim OfficeName = New OdbcParameter("@dbOfficeName",
OdbcType.Char)
       Dim CandidateOfficeName = New
OdbcParameter("@dbCandidateOfficeName", OdbcType.Char)
       Dim CandidateName = New OdbcParameter("@dbCandidateName",
OdbcType.Char)
       Dim CandidateNumeric = New
OdbcParameter("@dbCandidateNumeric", OdbcType.Int)
       Dim CandidateOfficeParty = New
OdbcParameter("@dbCandidateOfficeParty", OdbcType.Char)
       Dim CandidatePicture = New
OdbcParameter("@dbCandidatePicture", OdbcType.Char)

       Dim CCount As Integer = 0
       Dim SQLReturn As Integer = 0

       OfficeName.Value = "."
       CandidateOfficeName.Value = "."
       CandidateName.Value = "."
       CandidateNumeric.Value = 0
       CandidateOfficeParty.Value = "."
       CandidatePicture.Value = "."

       dbConn.Open()

       Dim SQLOfficeData = New OdbcCommand("DECLARE OfficeData
CURSOR
FOR SELECT @dbOfficeName FROM Office ORDER BY @dbOfficeName", dbConn)
       Dim SQLOfficeDataOpen = New OdbcCommand("OPEN OfficeData",
dbConn)
       Dim SQLOfficeDataFetch = New OdbcCommand("FETCH OfficeData
INTO @dbOfficeName", dbConn)
       Dim SQLOfficeDataClose = New OdbcCommand("CLOSE OfficeData",
dbConn)

       Dim SQLCandidateData = New OdbcCommand("DECLARE CandidateData
CURSOR FOR SELECT * FROM Candidate ORDER BY CandidateOfficeName",
dbConn)
       Dim SQLCandidateDataFetch = New OdbcCommand("FETCH
CandidateData INTO @dbCandidateName, @dbCandidateOfficeName,
@dbCandidateNumeric, @dbCandidateOfficeParty, @dbCandidatePicture",
dbConn)
       Dim SQLCandidateDataOpen = New OdbcCommand("OPEN
CandidateData", dbConn)
       Dim SQLCandidateDataClose = New OdbcCommand("CLOSE
CandidateData", dbConn)

       SQLOfficeData.Parameters.Add(OfficeName)

       SQLCandidateData.Parameters.Add(CandidateOfficeName)
       SQLCandidateData.Parameters.Add(CandidateName)
       SQLCandidateData.Parameters.Add(CandidateNumeric)
       SQLCandidateData.Parameters.Add(CandidateOfficeParty)
       SQLCandidateData.Parameters.Add(CandidatePicture)

SQLOfficeData.ExecuteNonQuery()<-----ERROR [42000] [Microsoft][SQL
Native Client][SQL Server]Must declare the scalar variable
"@dbOfficeName".
Cor Ligthert[MVP] - 19 Mar 2008 05:53 GMT
m1c,

Why are you showing us a piece of program filled with all kind of crap not
belonging to the problem and then ask us to investigate that what is your
problem.

Remove all the code that is not for the    SQLOfficeData command and then
try it again, as it still does now work here, then try it again

Cor

Crap - getting the SOS. This is an old post, so I'll try to recycle it
as new with hopefully enough details.
Here's the relevant code causing this error
SQLOfficeData.ExecuteNonQuery()<-----ERROR [42000] [Microsoft][SQL
Native Client][SQL Server]Must declare the scalar variable
"@dbOfficeName".
at the end of all of this:

Dim dbConn = New
System.Data.Odbc.OdbcConnection("Dsn=Election;description=Election;trusted_­
connection=Yes;app=Microsoft®
Visual Studio® 2008;wsid=M1CVISTA1;database=Election")

       Dim CString As String
       Dim OfficeName = New OdbcParameter("@dbOfficeName",
OdbcType.Char)
       Dim CandidateOfficeName = New
OdbcParameter("@dbCandidateOfficeName", OdbcType.Char)
       Dim CandidateName = New OdbcParameter("@dbCandidateName",
OdbcType.Char)
       Dim CandidateNumeric = New
OdbcParameter("@dbCandidateNumeric", OdbcType.Int)
       Dim CandidateOfficeParty = New
OdbcParameter("@dbCandidateOfficeParty", OdbcType.Char)
       Dim CandidatePicture = New
OdbcParameter("@dbCandidatePicture", OdbcType.Char)

       Dim CCount As Integer = 0
       Dim SQLReturn As Integer = 0

       OfficeName.Value = "."
       CandidateOfficeName.Value = "."
       CandidateName.Value = "."
       CandidateNumeric.Value = 0
       CandidateOfficeParty.Value = "."
       CandidatePicture.Value = "."

       dbConn.Open()

       Dim SQLOfficeData = New OdbcCommand("DECLARE OfficeData
CURSOR
FOR SELECT @dbOfficeName FROM Office ORDER BY @dbOfficeName", dbConn)
       Dim SQLOfficeDataOpen = New OdbcCommand("OPEN OfficeData",
dbConn)
       Dim SQLOfficeDataFetch = New OdbcCommand("FETCH OfficeData
INTO @dbOfficeName", dbConn)
       Dim SQLOfficeDataClose = New OdbcCommand("CLOSE OfficeData",
dbConn)

       Dim SQLCandidateData = New OdbcCommand("DECLARE CandidateData
CURSOR FOR SELECT * FROM Candidate ORDER BY CandidateOfficeName",
dbConn)
       Dim SQLCandidateDataFetch = New OdbcCommand("FETCH
CandidateData INTO @dbCandidateName, @dbCandidateOfficeName,
@dbCandidateNumeric, @dbCandidateOfficeParty, @dbCandidatePicture",
dbConn)
       Dim SQLCandidateDataOpen = New OdbcCommand("OPEN
CandidateData", dbConn)
       Dim SQLCandidateDataClose = New OdbcCommand("CLOSE
CandidateData", dbConn)

       SQLOfficeData.Parameters.Add(OfficeName)

       SQLCandidateData.Parameters.Add(CandidateOfficeName)
       SQLCandidateData.Parameters.Add(CandidateName)
       SQLCandidateData.Parameters.Add(CandidateNumeric)
       SQLCandidateData.Parameters.Add(CandidateOfficeParty)
       SQLCandidateData.Parameters.Add(CandidatePicture)

SQLOfficeData.ExecuteNonQuery()<-----ERROR [42000] [Microsoft][SQL
Native Client][SQL Server]Must declare the scalar variable
"@dbOfficeName".
Jack Jackson - 19 Mar 2008 07:11 GMT
I have never used ODBC with .NET (why not use the native SQL driver,
it should be faster), but I think ODBC uses ? instead of @ to mark
parameters.

I'm not sure you can use parameters for anything other than WHERE
clause values.  You are using them for SELECT items, INTO and ORDER
BY.  Besides which the values you are specifying for those parameters
don't make any sense to me.

>Crap - getting the SOS. This is an old post, so I'll try to recycle it
>as new with hopefully enough details.
[quoted text clipped - 68 lines]
>Native Client][SQL Server]Must declare the scalar variable
>"@dbOfficeName".
Patrice - 19 Mar 2008 13:26 GMT
IMO forget about all that. In particular :

- You are using ODBC instead of the SQLServer provider. I believe ODBC
doesn't support the @notation (uses unamed ? placeholders instead)...
- Here it looks like you are trying to send a single line (?!) of a whole
cursor based procedure that likely won't even produce what you expect
- It looks like you would like just to produce the SQL statement on the fly
i.e. : New SqlCommand("SELECT [" & ColumnName & "] FROM Office ORDER BY 1");
ColumnName should be something that is *not* user defined (it's bad for
safety reasons).

So i would suggest :
- follow a basic ADO.NET tutorial and sends first a basic SQL statement
(SELECT)
- switch from ODBC to the SQL provider if you are targetting only SQL Server
(if odbc you won't be able to use SQL Server specific syntax with anything
else than SQL Server anyway)
- if doing long server side procedures wrap them in a stored procedure and
call them from client side
- avoid to get all the data from the DB if you have no use for them (do you
really need ALL the lines stored in the DB ?)
- etc...

Once you are more familiar with this stuff, come back to ask about the
additional thing you would like to implement...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.