Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 20th, 2003, 09:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default Execute Scalar method

Hello,

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:

Code:
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

        SQLConn.Open()
        objValue = objAdapter.InsertCommand.ExecuteScalar()
        SQLConn.Close()

        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?

Thanks,

Brian Mains
__________________
Brian
Reply With Quote
  #2 (permalink)  
Old October 20th, 2003, 02:14 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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:

Code:
dim param as SQLParameter = objAdapter.InsertCommand.Parameters.Add(new SQLParameter("@RoleID", SQLDBType.Decimal, 18))
param.Direction = ParameterDirection.Output
Reply With Quote
  #3 (permalink)  
Old October 11th, 2008, 11:41 PM
hu_yang
Guest
 
Posts: n/a
Default

follow this url.

http://vb.net-informations.com/ado.n...sqlcommand.htm



Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 05:44 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.