473,408 Members | 2,477 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,408 software developers and data experts.

HELP: GetSqlBinary conversion in C# and VB.NET

Anyone know what the VB.NET equivalent of this C# statement is...

byte [] segBody = (byte []) sqlReader.GetSqlBinary(3);

Tried as hard as I could to do it in VB.NET but I cannot seem to do it.
Nov 15 '05 #1
5 1910
Hi Gareth,

|| byte [] segBody = (byte []) sqlReader.GetSqlBinary(3);

GetSqlBinary returns a SqlBinary structure rather than an array of bytes,
so a straight conversion to VB fails, as you have found. Does that C#
statement actually work?

These fail:
Dim segBody1 As Byte() _
= DirectCast (sqlReader.GetSqlBinary(3), Byte()) 'Fails
Dim segBody2 As Byte() _
= CType (sqlReader.GetSqlBinary(3), Byte()) 'Fails

See if this code [untested] gives you some joy:
(Imports System.Data.SqlTypes)
Dim sqlBinValue As SqlBinary = sqlReader.GetSqlBinary(3)
Dim segBody As Byte() = SqlBinary.op_Explicit (sqlBinValue)

Regards,
Fergus
Nov 15 '05 #2
Thanks Fergus. Funnily enough the C# code works perfectly. I'll try your VB
example right now.

Cheers,
Gareth

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
Hi Gareth,

|| byte [] segBody = (byte []) sqlReader.GetSqlBinary(3);

GetSqlBinary returns a SqlBinary structure rather than an array of bytes, so a straight conversion to VB fails, as you have found. Does that C#
statement actually work?

These fail:
Dim segBody1 As Byte() _
= DirectCast (sqlReader.GetSqlBinary(3), Byte()) 'Fails Dim segBody2 As Byte() _
= CType (sqlReader.GetSqlBinary(3), Byte()) 'Fails

See if this code [untested] gives you some joy:
(Imports System.Data.SqlTypes)
Dim sqlBinValue As SqlBinary = sqlReader.GetSqlBinary(3)
Dim segBody As Byte() = SqlBinary.op_Explicit (sqlBinValue)

Regards,
Fergus

Nov 15 '05 #3
Fergus. Excellant stuff that worked nicely!

"Gareth Parris" <gc******@hotmail.comm> wrote in message
news:uE**************@TK2MSFTNGP12.phx.gbl...
Thanks Fergus. Funnily enough the C# code works perfectly. I'll try your VB example right now.

Cheers,
Gareth

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
Hi Gareth,

|| byte [] segBody = (byte []) sqlReader.GetSqlBinary(3);

GetSqlBinary returns a SqlBinary structure rather than an array of

bytes,
so a straight conversion to VB fails, as you have found. Does that C#
statement actually work?

These fail:
Dim segBody1 As Byte() _
= DirectCast (sqlReader.GetSqlBinary(3), Byte())

'Fails
Dim segBody2 As Byte() _
= CType (sqlReader.GetSqlBinary(3), Byte()) 'Fails

See if this code [untested] gives you some joy:
(Imports System.Data.SqlTypes)
Dim sqlBinValue As SqlBinary = sqlReader.GetSqlBinary(3)
Dim segBody As Byte() = SqlBinary.op_Explicit (sqlBinValue)

Regards,
Fergus


Nov 15 '05 #4
Hi Gareth,

That op_ Explicit was niggling away in my mind and I've just remembered.

C# provides the ability to define explicit conversions which take place
when a variable is cast from one form to another. op_Explicit is the function
that allows other languages to access that convesion.

So
(byte []) sqlReader.GetSqlBinary(3);
and
SqlBinary.op_Explicit (sqlReader.GetSqlBinary(3));
are equivalent.

Regards,
Fergus
Nov 15 '05 #5
I was not aware of that. That makes more sense now.

Many thanks again,
Gareth

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:u%****************@TK2MSFTNGP09.phx.gbl...
Hi Gareth,

That op_ Explicit was niggling away in my mind and I've just remembered.
C# provides the ability to define explicit conversions which take place when a variable is cast from one form to another. op_Explicit is the function that allows other languages to access that convesion.

So
(byte []) sqlReader.GetSqlBinary(3);
and
SqlBinary.op_Explicit (sqlReader.GetSqlBinary(3));
are equivalent.

Regards,
Fergus

Nov 15 '05 #6

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

Similar topics

2
by: Thames | last post by:
Hi, I have to repost my question for help. Yesterday I set up two DB UDB ESE V8.2 server on linux and windows platform, respectively. Both servers have been connected without any problems...
27
by: Chuck Grimsby | last post by:
(Repost, due to lack of submissions...) The Microsoft Access Product Group (the people who build Microsoft Access) want your help! One of the main things we're working on for the near future...
11
by: Marcus Jacobs | last post by:
Dear Group I have written a file conversion program that uses strtof to convert text strings to floats. It works as I intended except for my error messages. It is my understanding that strtof...
6
by: Gareth Parris | last post by:
Anyone know what the VB.NET equivalent of this C# statement is... byte segBody = (byte ) sqlReader.GetSqlBinary(3); Tried as hard as I could to do it in VB.NET but I cannot seem to do it.
3
by: ASP .NET Newbie | last post by:
I am trying to insert a new record into my database, and have it return a uniqueidentifier as the "newcatid". I don't use integers as my "id"'s, but rather uniqueidentifiers. Here's my Stored...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
4
by: Gregory Edigaroff | last post by:
Hello, Everybody! Ok, I urgently need the solution for a following task in C++. This is the task from programmers contest, so I believe somebody have a solution for it. I need either a full...
0
by: k1ckthem1dget | last post by:
I need to display the unsorted list of names and display the sorted list of names. My program is getting a bunch of errors though, and i dont know why. I am getting the following errors. 28:...
2
by: k1ckthem1dget | last post by:
I need to display the unsorted list of names and display the sorted list of names. My program is getting a bunch of errors though, and i dont know why. I am getting the following errors. 28:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.