Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 20th, 2003, 09:51 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default Execute Scalar method


I'm trying to use the ExecuteScalar() method for a SQLCommand object (through a data adapter). My code is below; please note that SQLConn is a drag-and-drop connection:

Dim objAdapter As New SqlDataAdapter()
        objAdapter.InsertCommand = New SqlCommand("CreateRole", SQLConn)
        objAdapter.InsertCommand.CommandType = CommandType.StoredProcedure

        objAdapter.InsertCommand.Parameters.Add("@RoleName", SqlDbType.VarChar).Value = "DBA"
        objAdapter.InsertCommand.Parameters.Add("@RoleDescription", SqlDbType.VarBinary).Value = "Database Admin"

        Dim objValue As Object

        objValue = objAdapter.InsertCommand.ExecuteScalar()

        lblID.Text = objValue.ToString()
I'm trying to return a value, so I have an insert parameter, retrieve the ID (as "set @RoleID = @@identity"), and select the role ID value ("select @RoleID"). When I run the code, I get the error:

Invalid cast from System.String to System.Byte[].

Any idea why?


Brian Mains
Old October 20th, 2003, 02:14 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts

For anybody that has read this, I've found the answer off of another technical resource. The problem was the output parameter as declared above. For it to work it has to be:

dim param as SQLParameter = objAdapter.InsertCommand.Parameters.Add(new SQLParameter("@RoleID", SQLDBType.Decimal, 18))
param.Direction = ParameterDirection.Output
Old October 11th, 2008, 11:41 PM
Posts: n/a

follow this url.


Similar Threads
Thread Thread Starter Forum Replies Last Post
scalar and reverse crmpicco Perl 2 February 16th, 2007 08:18 AM
"Must declare the scalar variable" Orchid85 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 1 September 22nd, 2006 10:56 AM
connection.execute and command.execute difference vinod_yadav1919 Classic ASP Databases 0 September 28th, 2005 06:34 AM
conn.execute,command.execute or rs.open vinod_yadav1919 Crystal Reports 0 January 3rd, 2005 10:26 AM
Execute method of web service, about database? hueduongit .NET Web Services 3 December 3rd, 2003 03:18 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.