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

Presisting an object in database

Hi All,

I am trying to presist an object in database. So I was searching and
found the one (at the bottom of the message) in the groups and I
followed it and it did create and store the data. But the data is
incomplete. In the following example instead of using struct I used
class. When I say the data is incomplete, I meant the value of the
members are gone. When I store the object in the table, I am storing it
as BLOB.

Here is the class I am trying to presist

public class GSValues
{
private int year;
private User user;

public GSValues()
{
}

public GSValues(int year, User usr)
{
year = yearSelection;
user = usr;
}

public int Year
{
get { return year; }
}
public User CurrentUser
{
get { return user; }
}
}

Following is my serialisation part
XmlSerializer x = new XmlSerializer(typeof(GSValues));
string xmlString;
using (StringWriter sw = new StringWriter())
{
x.Serialize(sw, selectedValues);
xmlString = sw.ToString();
}
System.Text.ASCIIEncoding enc = new ASCIIEncoding();
SelectionObject = enc.GetBytes(xmlString);

Following is deserialisation part

GSValues value = null;
XmlSerializer x = new XmlSerializer(typeof(GSValues));
ASCIIEncoding enc = new ASCIIEncoding();
string xmlString = enc.GetString(SelectionObject);
using (StringReader sr = new StringReader(xmlString))
{
value = (GSValues)x.Deserialize(sr);
}
return value;
Could some one explain to me what am I doing wrong?

Thanks in advance.
----- Message from groups ------
Normally when I approach issues, I take all the crude out of the
picture.
For example, the db processing here has nothing to do with
serialization, so
remove it till you get your serializer working. I am guessing your
input
string is not right. Test it directly with your object like so:

// Serialize.
KeyValue kv = new KeyValue("123", "Hello");
XmlSerializer x = new XmlSerializer(typeof(KeyValue));
string xmlString;
using( StringWriter sw = new StringWriter() )
{
x.Serialize(sw, kv);
xmlString = sw.ToString();

}
// Deserialize.
XmlSerializer ser = new XmlSerializer(typeof(KeyValue));
using (StringReader sr = new StringReader(xmlString))
{
KeyValue kv2 = (KeyValue)ser.Deserialize(sr);
Console.WriteLine("Key:" + kv2.Key);
Console.WriteLine("Value:" + kv2.Value);
}
public struct KeyValue
{
public string Key;
public string Value;

public KeyValue(string key, string value)
{
this.Key = key;
this.Value = value;
}
}
--
William Stacey, MVP
http://mvp.support.microsoft.com

Jul 25 '06 #1
5 1363
I think that the best practice is you should use Serializable attribute
on GSValues type and User type. Apply the SerializableAttribute
attribute to a type to indicate that instances of this type can be
serialized, so you can control which type can be serialized or can't be
serialized.

You can also use XSD designer to design your class shema and use XSD
tool to generate class source code. It will add xml serializable
attribute automatically.

Ok, my suggestions is based on the data on the database is complete. If
data is not complete, I think you should inspect the database table
design view and table column setting.

hope useful for you.

Sincerely,
simida

DBC User wrote:
Hi All,

I am trying to presist an object in database. So I was searching and
found the one (at the bottom of the message) in the groups and I
followed it and it did create and store the data. But the data is
incomplete. In the following example instead of using struct I used
class. When I say the data is incomplete, I meant the value of the
members are gone. When I store the object in the table, I am storing it
as BLOB.

Here is the class I am trying to presist

public class GSValues
{
private int year;
private User user;

public GSValues()
{
}

public GSValues(int year, User usr)
{
year = yearSelection;
user = usr;
}

public int Year
{
get { return year; }
}
public User CurrentUser
{
get { return user; }
}
}

Following is my serialisation part
XmlSerializer x = new XmlSerializer(typeof(GSValues));
string xmlString;
using (StringWriter sw = new StringWriter())
{
x.Serialize(sw, selectedValues);
xmlString = sw.ToString();
}
System.Text.ASCIIEncoding enc = new ASCIIEncoding();
SelectionObject = enc.GetBytes(xmlString);

Following is deserialisation part

GSValues value = null;
XmlSerializer x = new XmlSerializer(typeof(GSValues));
ASCIIEncoding enc = new ASCIIEncoding();
string xmlString = enc.GetString(SelectionObject);
using (StringReader sr = new StringReader(xmlString))
{
value = (GSValues)x.Deserialize(sr);
}
return value;
Could some one explain to me what am I doing wrong?

Thanks in advance.
----- Message from groups ------
Normally when I approach issues, I take all the crude out of the
picture.
For example, the db processing here has nothing to do with
serialization, so
remove it till you get your serializer working. I am guessing your
input
string is not right. Test it directly with your object like so:

// Serialize.
KeyValue kv = new KeyValue("123", "Hello");
XmlSerializer x = new XmlSerializer(typeof(KeyValue));
string xmlString;
using( StringWriter sw = new StringWriter() )
{
x.Serialize(sw, kv);
xmlString = sw.ToString();

}
// Deserialize.
XmlSerializer ser = new XmlSerializer(typeof(KeyValue));
using (StringReader sr = new StringReader(xmlString))
{
KeyValue kv2 = (KeyValue)ser.Deserialize(sr);
Console.WriteLine("Key:" + kv2.Key);
Console.WriteLine("Value:" + kv2.Value);
}
public struct KeyValue
{
public string Key;
public string Value;

public KeyValue(string key, string value)
{
this.Key = key;
this.Value = value;
}
}
--
William Stacey, MVP
http://mvp.support.microsoft.com
Jul 26 '06 #2
DBC User wrote:

Hi,
Here is the class I am trying to presist

public class GSValues
{
private int year;
private User user;
Here is the problem. The XmlSerializer only serializes public members.
Your fields are private. Either make them public or add a public setter
to the properties. It should work with XmlSerialization after you do that.

The problem with that is that you have to make all the serializable
members public, giving everyone access to them. You might want to create
small data classes, which are additionally encapsulated in the real
classes that the user gets to see.

There are also object databases. Have you taken a look at some of them?

hth,
Max
Jul 26 '06 #3
Hi Max,

Thanks, after looking my code I realised I do not need to presist the
user field. So I changed all the fields to public and I added
[NonSerialized] attribute to User type declaration now I am getting an
error
"There was an error reflecting field 'user'"

Thanks.
Markus Stoeger wrote:
DBC User wrote:

Hi,
Here is the class I am trying to presist

public class GSValues
{
private int year;
private User user;

Here is the problem. The XmlSerializer only serializes public members.
Your fields are private. Either make them public or add a public setter
to the properties. It should work with XmlSerialization after you do that.

The problem with that is that you have to make all the serializable
members public, giving everyone access to them. You might want to create
small data classes, which are additionally encapsulated in the real
classes that the user gets to see.

There are also object databases. Have you taken a look at some of them?

hth,
Max
Jul 26 '06 #4
[NonSerialized] applies to fields, i.e. binary serialization; for
property-based xml-serialization you need [XmlIgnore] - I would make the
fields private though, and expose through public getters / setters (needs
both on a serialized property)

Marc
Jul 26 '06 #5
Thanks a lot and it helped. All the documents I read somehow either
missed it or not given that importance.
Again thanks and is now working.
Marc Gravell wrote:
[NonSerialized] applies to fields, i.e. binary serialization; for
property-based xml-serialization you need [XmlIgnore] - I would make the
fields private though, and expose through public getters / setters (needs
both on a serialized property)

Marc
Jul 26 '06 #6

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

Similar topics

15
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
8
by: Lance | last post by:
I've been teaching myself C# for a few months now and I have a concept problem. I know how to instantiate an object from a class I’ve created, but now I want to retrieve and store data in a...
7
by: isamusetu | last post by:
anybody knows how to share the dll between the process? I know there is a way to set the #pragma data_seg in the visual studio 6.0 C++, that can make the dll can be shared between the multiple...
8
by: ST | last post by:
Hello everyone, Can anyone help me with this error above when I debug my web app project in vstudio.net?? I can't figure it out! It was working fine for months, and now all of a sudden it's not!!...
2
by: nacho222 | last post by:
I'm currently in the middle of writing a persistence framework, and I have to make a design decission. The framework will take care of all the saving and restoring objects, and also the...
7
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id -...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
1
Curtis Rutland
by: Curtis Rutland | last post by:
How To Use A Database In Your Program Part II This article is intended to extend Frinny’s excellent article: How to Use a Database in Your Program. Frinny’s article defines the basic concepts...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.