|
private void AddProducts()
{
int i = 1;
foreach (TabPage tp in tabControl1.TabPages)
{
ObjectQuery<tblProducts> filteredProduct=new ObjectQuery<tblProducts>("Select value p from tblProducts as p where p.ProductType="+i,afos);
FlowLayoutPanel flp = new FlowLayoutPanel();
flp.Dock = DockStyle.Fill;
foreach(tblProducts tprod in filteredProduct)
{
Button b = new Button();
b.Size = new Size(100, 100);
b.Image = tprod.ProductImage;
b.Text=tprod.ProductName;
b.Tag = tprod;
b.Click += new EventHandler(UpdateProductList);
flp.Controls.Add(b);
}
tp.Controls.Add(flp);
i++;
}
}
Please help. I want to automatically call an image from the database and make it an image in the button..I have managed to have the Button automatically add depending on the number of Products on the database, there's no solid button. My only problem is to have the button's image set automatically from the images in the database.
Thank you so much.
|
|
|
|
|
And what is the result of the code that you show?
It's not clear what problem you are having.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
I am using the following code to convert from one type of object to another. It works fine, but I am looking for a way to improve performance:
public static object ConvertObject(object source, Type targetType)
{
object result;
Type sourceType = source.GetType();
DataContractSerializer sourceSerializer = new DataContractSerializer(sourceType);
using (MemoryStream ms = new MemoryStream())
{
DateTime start = DateTime.Now;
sourceSerializer.WriteObject(ms, source);
TimeSpan writeObjectTime = DateTime.Now - start;
ms.Seek(0, SeekOrigin.Begin);
DataContractSerializer targetSerializer = new DataContractSerializer(targetType);
XmlReader r = XmlReader.Create(ms);
DynamicObjectSerializer.MyReader myreader = new DynamicObjectSerializer.MyReader(r);
start = DateTime.Now;
result = targetSerializer.ReadObject(myreader);
TimeSpan readObjectTime = DateTime.Now - start;
LoggerHolder.LogDebug("\"{0}\" Write Time = {1}, Read Time = {2}", source.GetType().Name, writeObjectTime, readObjectTime);
}
return result;
}
Using the MemoryStream actually slows the process down since the object is first being written to it and then read from it. If it was possible to serialize the source object to the target one directly it would have been taken half the time. Any idea how this can be done?
Thanks.
|
|
|
|
|
I think it may be valuable for you if you give more detail on what type of object conversion you are performing here.
Measuring elapsed time using DateTime is well-known to be "limited" in accuracy. Starting with .NET 2.0, you can use the 'StopWatch in the System.Diagnostics library timer to get more accurate results: [^].
Executing the code several times before starting a timing operation is also believed to improve timing accuracy by eliminating any possible one-time costs of getting the code "jitted."
There are several articles here on CodeProject dealing with accurate timing: just search.
“The best hope is that one of these days the Ground will get disgusted enough just to walk away ~ leaving people with nothing more to stand ON than what they have so bloody well stood FOR up to now.” Kenneth Patchen, Poet
|
|
|
|
|
I know how to measure time - thanks - this is not the issue here.
I'm looking for someone with DataContractSerializer and serialization experience.
Thanks.
|
|
|
|
|
There was no need to get short with Bill. He was only trying to give you a bit of good advice. Using a DateTime to measure code timing, clearly shows you do not know how to measure time.
Everyone dies - but not everyone lives
|
|
|
|
|
Sorry - didn't mean to insult in no way - However, measuring is not the issue here - i am quite sure with the results since the times of the operations are long (~10 seconds) because the objects contains a lot of data - it is not milliseconds of difference.
|
|
|
|
|
You should consider using Fasterflect[^] or Automapper[^]. Your current implementation is hugely malperformant.
[Edit]
And you're really mapping between objects, not converting types, right?
[/Edit]
/ravi
|
|
|
|
|
Object converting - right.
The problem is that i want to convert objects without knowing its members - it should be done using their names (one property data in object one is copied to object two's property which has the same name).
using serialization i can serialize one object from one module to an object of another module easily.
I was looking for a way to serialize from one object to another without using a memory stream in the middle - that will solve my problem.
|
|
|
|
|
impeham wrote: it should be done using their names That's exactly what I thought you wanted to do.
You're mapping (not converting) objects. Use Automapper instead of serialization. It's orders of magnitude faster.
/ravi
|
|
|
|
|
i will look into it - thanks a lot 
|
|
|
|
|
After checking AutoMapper i realized that i need to specifically map all internal types of the objects from source to target. this requires as my objects can contain many types and it will require a LOT of work (not to mention maintenance as they change) before it can be used and i need something to do the conversions automatically with minimal work => leads me to my first posted code.
So i return to the original question - is it possible to skip the MemoryStream read/write?
|
|
|
|
|
impeham wrote: is it possible to skip the MemoryStream read/write? Only if you write mappings for each internal object.
My philosophy is to build Clone() methods for each object (class), and call them for nested objects as required. Implementing Clone() is easily done by calling .NET's MemberwiseClone() for most properties. You will find the effort required to do this small in comparison to the run-time performance gain.
/ravi
|
|
|
|
|
That will take a lot of work for implementation & maintenance afterwards which i am trying to avoid. I will have to think about this more.
Thanks.
|
|
|
|
|
If you have a generalized object graph and a need to map any type of object to any other type, I would like to suggest that you revisit your design. Object mapping is mostly used when converting DTOs to business objects, and frameworks like Automapper and Fasterflect prove to be very convenient (and fast) in these cases.
If you choose to go with your existing design, you may want to consider XmlSerializer which caches serializers for different types, causing a one-time performance hit on first convert. But this is also not as performant as simple mapping.
Cheers,
/ravi
|
|
|
|
|
Unfortunately, i cannot add the XmlInclude attribute to my types, so using XmlSerializer won't work.
|
|
|
|
|
OK.
This[^] article suggests BinaryDataContractSerializer may be the way to go. Not sure if you'd already seen this.
/ravi
|
|
|
|
|
Thanks - i'll look into it
|
|
|
|
|
hi
how to call web api passing multiple parameters in c#
|
|
|
|
|
From where: desktop application, web page ... ?
Please give some proper detail about your problem.
|
|
|
|
|
Hi
I want to call web api from desktop application .
For that I use readasync method . Using readasync method, pass multiple parameters as a concatenated string. In webapi I split that string in to multiple parameters. How can I pass multiple parameters from c# application?
|
|
|
|
|
You already answered your own question.
|
|
|
|
|
I have one function witch check signature.
public static bool CheckSignature(string key64, string sig64)
{
var key = Convert.FromBase64String(key64);
var sig = Convert.FromBase64String(sig64);
var rsa = new RSACryptoServiceProvider(new CspParameters
{
Flags = CspProviderFlags.UseMachineKeyStore
});
rsa.FromXmlString("<RSAKeyValue><Modulus>4IlzOY3Y9fXoh3Y5f06wBbtTg94Pt6vcfcd1KQ0FLm0S36aGJtTSb6pYKfyX7PqCUQ8wgL6xUJ5GRPEsu9 gyz8ZobwfZsGCsvu40CWoT9fcFBZPfXro1Vtlh/xl/yYHm+Gzqh0Bw76xtLHSfLfpVOrmZdwKmSFKMTvNXOFd0V18=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>");
var def = new RSAPKCS1SignatureDeformatter();
def.SetKey(rsa);
def.SetHashAlgorithm("MD5");
return def.VerifySignature(key, sig);
}
Now i need to create signature for one of my keys.
public static byte[] CreateSignature(string key64)
{
var data = Convert.FromBase64String(key64);
var rSaCryptoServiceProvider = new RSACryptoServiceProvider(new CspParameters
{
Flags = CspProviderFlags.UseMachineKeyStore
});
rSaCryptoServiceProvider.FromXmlString("<RSAKeyValue><Modulus>4IlzOY3Y9fXoh3Y5f06wBbtTg94Pt6vcfcd1KQ0FLm0S36aGJtTSb6pYKfyX7PqCUQ8wgL6xUJ5GRPEsu9 gyz8ZobwfZsGCsvu40CWoT9fcFBZPfXro1Vtlh/xl/yYHm+Gzqh0Bw76xtLHSfLfpVOrmZdwKmSFKMTvNXOFd0V18=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>");
var RSAform = new RSAPKCS1SignatureFormatter();
RSAform.SetKey(rSaCryptoServiceProvider);
RSAform.SetHashAlgorithm("MD5");
byte[] hashData = CalculateMD5Hash(data);
return RSAform.CreateSignature(hashData);
}
But im getting an exception "Keyset does not exist" on "RSAform.CreateSignature(hashData)" line.
Any suggestion?
Still amater
|
|
|
|
|
RSA keys are in pairs, but you used public key in both methods. replacing provided key in CheckSignature with private key should solve your problem.
and in CheckSignature method you should feed VerifySignature with MD5 hash of key varibale, the code should be more like this
public static byte[] CreateSignature(string key64)
{
var data = Convert.FromBase64String(key64);
var rSaCryptoServiceProvider = new RSACryptoServiceProvider(new CspParameters
{
Flags = CspProviderFlags.UseMachineKeyStore
});
rSaCryptoServiceProvider.FromXmlString(privateKey);
var RSAform = new RSAPKCS1SignatureFormatter();
RSAform.SetKey(rSaCryptoServiceProvider);
RSAform.SetHashAlgorithm("MD5");
var md5 = MD5.Create();
byte[] hashData = md5.ComputeHash(data);
return RSAform.CreateSignature(hashData);
}
hope this helps
|
|
|
|