473,693 Members | 2,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

object to a byte[]: revisit

Hello All,

While replying to my post many lost the track of what I was asking but it was nice to know few extra things. Well coming back to my problem --

My field is varbinary in SQL db. Do you think converting it into ToString()
might create problem. "FullName" col is just an example, actually I am
serializing a 3rd party control which it is doing via byte[].

My original question was (with some update) --

How can I change object returned from database to a byte array
e.g.
byte[] b = row["SomeColNam e"];

also

how can I save a byte[] to database?
Thank you,
Po
Jan 11 '06 #1
5 2228
Pohihihi <no*****@hotmai l.com> wrote:
While replying to my post many lost the track of what I was asking
but it was nice to know few extra things. Well coming back to my
problem --

My field is varbinary in SQL db. Do you think converting it into ToString()
might create problem. "FullName" col is just an example, actually I am
serializing a 3rd party control which it is doing via byte[].

My original question was (with some update) --

How can I change object returned from database to a byte array
e.g.
byte[] b = row["SomeColNam e"];

also

how can I save a byte[] to database?


Well, have you tried casting row["SomeColNam e"] to a byte array? I
don't know offhand if that'll work, but it's worth a try. If it
doesn't, look in the debugger at what row["SomeColNam e"] actually
returns - what type of object.

As for saving - I would expect that once you've worked out how to get
it out, you could reverse the process to put it back in, the same as
any other type.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 11 '06 #2
I guess I miss the part of casting as array (?)

you mean

byte[] b = (byte[])row["SomeColNam e"];

never did this before but surely will try if this works, until now I was
only doing (byte) and never tried (byte[]). For the return type I am
positive that it will return byte[] or DBNull in case empty col.

Thank you,
Po


"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Pohihihi <no*****@hotmai l.com> wrote:
While replying to my post many lost the track of what I was asking
but it was nice to know few extra things. Well coming back to my
problem --

My field is varbinary in SQL db. Do you think converting it into
ToString()
might create problem. "FullName" col is just an example, actually I am
serializing a 3rd party control which it is doing via byte[].

My original question was (with some update) --

How can I change object returned from database to a byte array
e.g.
byte[] b = row["SomeColNam e"];

also

how can I save a byte[] to database?


Well, have you tried casting row["SomeColNam e"] to a byte array? I
don't know offhand if that'll work, but it's worth a try. If it
doesn't, look in the debugger at what row["SomeColNam e"] actually
returns - what type of object.

As for saving - I would expect that once you've worked out how to get
it out, you could reverse the process to put it back in, the same as
any other type.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 11 '06 #3
Pohihihi <no*****@hotmai l.com> wrote:
I guess I miss the part of casting as array (?)

you mean

byte[] b = (byte[])row["SomeColNam e"];
Yup.
never did this before but surely will try if this works, until now I was
only doing (byte) and never tried (byte[]). For the return type I am
positive that it will return byte[] or DBNull in case empty col.


Right. Casting to byte certainly wouldn't work.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 11 '06 #4
KJ
Responding to the latter question:

byte[] b = new byte[100];
SqlParameter sp = new SqlParameter("p ", SqlDbType.VarBi nary, 100);
sp.Value = b;

Jan 11 '06 #5
Hi,

Find below the code to store an binary value (in this case a file) in the DB.
Also you see how I get a file back from the DB with disregard of its type.

//To save:
SqlCommand com = new SqlCommand();
com.CommandText = "SaveDocume nt";
com.CommandType = CommandType.Sto redProcedure;
com.Parameters. Add("@OrigName" , SqlDbType.VarCh ar).Value = Path.GetFileNam e(physicalname) ;
SqlParameter param = com.Parameters. Add("@data", SqlDbType.Image );
FileStream file = new FileStream( physicalname, FileMode.Open);
byte[] buff = new byte [ file.Length];
file.Read(buff, 0, Convert.ToInt32 (file.Length));
file.Close();
param.Value = buff;
id = Convert.ToInt32 ( DataProvider.Ex ecuteScalar( com) );

//To retrieve:
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "LoadDocume nt";
cmd.CommandType = CommandType.Sto redProcedure;
cmd.Parameters. Add("@dID", SqlDbType.Int). Value = id;
SqlDataReader reader = DataProvider.Ex ecuteReader( cmd);
this.name = Guid.NewGuid(). ToString() + "." + Path.GetExtensi on( reader["OrigName"].ToString() );
physicalname = path + @"\" + name;
FileStream file = new FileStream( physicalname, FileMode.Create );
file.Write( (byte[])reader["Data"], 0, ((byte[])reader["Data"]).GetUpperBound (0)+1 );
file.Close();


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Pohihihi" <no*****@hotmai l.com> wrote in message news:um******** ******@TK2MSFTN GP10.phx.gbl...
Hello All,

While replying to my post many lost the track of what I was asking but it was nice to know few extra things. Well coming back to my problem --

My field is varbinary in SQL db. Do you think converting it into ToString()
might create problem. "FullName" col is just an example, actually I am
serializing a 3rd party control which it is doing via byte[].

My original question was (with some update) --

How can I change object returned from database to a byte array
e.g.
byte[] b = row["SomeColNam e"];

also

how can I save a byte[] to database?
Thank you,
Po
Jan 11 '06 #6

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

Similar topics

15
5078
by: Lou | last post by:
How do I get the data in the clipbaord fro a registered data type. The code snippet below doesn't work? But the VB6 example does. What am I doing wrong. private void lstItems_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
4
399
by: Moe Sizlak | last post by:
Hi There, I have a user control that has 2 listmenus populated from a database, I want the form to submit when the listmenu are changed which seems to happen no problem when the page is on it's own but when I have it as user control I get the error "Object reference not set to an instance of an object. " what am I doing wrong? Moe <><
9
2173
by: Moe Sizlak | last post by:
Hi There, I am trying to write the selected value of a listcontrol when a button is clicked and I keep getting the error "object not set to a reference of an object". The libox itself is in a usercontrol and all i'm really needing to do is get the selected value when the button is clicked on the form. Can someone tell what I need to include in my page to get this working ? Moe <><
3
2437
by: Adriano | last post by:
Hello, when I try to print something, either DataGrid or from Crystal Report viever the folowing error message appears and cancels printing: Object reference not set to an instance of an object :(((( Anyone pls help me to solve this problem!!! thanks in advance,
4
1409
by: DazedAndConfused | last post by:
I encryted a serialized binary formatted object. Now I can't figure out how to deserialize it so that I can decrypt it. I used this code encrypt and write it out: Dim fe As New MortgageFileWriter.FileEncrypt Dim myBuffer As New IO.MemoryStream Dim OutBuffer As New IO.MemoryStream
1
3214
by: DazedAndConfused | last post by:
Can you encrpt a serialized object? Or am I trying to do something that just doesn't work that way? I am trying to encrypt a serialized object. I can read and write the object to a file without a problem(IF I don't encrypt it). The encryption routine I am using works great when I am just reading in text and writing out encrypted data/Reading in encrpted data and writing out decrypted text.
7
11942
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return "DirectoryEntry" objects within the path that I specify (either using the DirectoryEnrtry.Children or using the DirectorySearcher class) and all started well and dandy! Now the problem; some of the properties of the DirectoryEntry objects being...
4
6087
by: Goh | last post by:
Hi, I would like to know how can we implement a web page that intelligent enough to unique identify that pc have been visit before without any cookies and login user require. I have try implement this by MAC address. When user browser the web site I sometime can get user pc MAC and sometime no. Why this type of implementation are so not consistency? Does any
5
1894
by: JoeC | last post by:
I am still working in my maze game and I am making improvments. The main sticking problem that I have is passing my graphic library. This probram uses windows apis but my question has nothing to do with those. So please dont jump on me for that. I am making this graphics library and my bitmap data is stored in an array. This library works some time and when I have to use the copy constructor it dosn't seem to work. I did my best to...
0
8549
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8970
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8815
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8812
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7643
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6475
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2989
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1967
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.