|
Look as Dave suggested, the first preference should be to let it be handled by the database since they are more optimized to do this.
However, if you insist on loading all data into a datatable, you can try using a dataview. However i am not so sure about the performance. Test it atleast.
DataView view = table.DefaultView;
view.RowFilter = "Value>2";
|
|
|
|
|
Thanks mate, unfortunately, I can't do that as I need to load the whole table to the datatable to cut down trips to the database.. And using the dataGridView control is not an option too as I'm using the listView control!.. Thanks anyways mate!
|
|
|
|
|
I used the following code and Dataview method is faster than the select method. I do not know why but it is.
Try it. May be it helps you.
DataTable table = new DataTable();
table.Columns.Add(columnName);
for (int i = 0; i < 1000000; i++)
{
table.Rows.Add(i);
}
MessageBox.Show("Loaded", "");
string condition = columnName + ">500 AND " + columnName + "<10000";
DateTime timeStart = DateTime.Now;
DataView view = table.DefaultView;
view.RowFilter = condition;
DataTable newTable = view.ToTable();
DateTime timeStop = DateTime.Now;
TimeSpan span = timeStop - timeStart;
MessageBox.Show(span.TotalMilliseconds.ToString(), "");
timeStart = DateTime.Now;
DataRow[] rows = table.Select(condition);
timeStop = DateTime.Now;
span = timeStop - timeStart;
MessageBox.Show(span.TotalMilliseconds.ToString(), "");
|
|
|
|
|
Did you also try just letting the database do the work by having it just return the rows you need instead of 1,400,000 of them and throwing a bunch out?
|
|
|
|
|
Hey there Dave,
Dave Kreskowiak wrote: just return the rows you need
My first reply to such post would've been exactly the same
Ok, here are some more details.. I have this phone list that I want to filter out whenever the user types the first letters of the phone holder name and the list should shrink as the name builds up to match those with the very same name typed so far "something like the VS.Net intelli-sense"..
No you have the full picture, tell me how would you do it??
ps. I'm glad you saw this post Dave, I truly dmire your logic!
|
|
|
|
|
I wouldn't start with the loading the entire database.
I'd start by building the list from the first characters typed. The first character would return just the entries that started with that character, and I'd probably return only the top 20 to 30 entries. After all, you're not going to show ALL of those entries to the user, only enough to give them an idea they are on the right track. Keep doing this until the user either enters the entire entry themselves or have picked one from the list.
|
|
|
|
|
I am able to send mail from my gmail account to any other mail id.
No error.
But when i try to send mail from my yahoo account, it shows error.
I changed all required smtp server, port no etc .
If i configure same yahoo account in MS Outlook express, it send mail, but i cant do that from my application.
Before asking here, i searched a lot, but all the examples exist uses gmail, not a single one for yahoo.
Some says you need yahoo plus account, but if this is the case then why MS outlook able to access my normal yahoo account.
Any help/comment/guidance !! 
|
|
|
|
|
Well, you could read the message they send.
Since I wrote an application which can send email via smtp, I just created an account at yahoo and tried myself.
"Transaction failed : Cannot send message due to possible abuse; please visit
http://postmaster.yahoo.com/abuse_smtp.html for more information"
I.e. Yahoo blocks the mails!
|
|
|
|
|
Hi ,
Is it possible to add a blank generic item to the List before binding it to the datasource?
I hope this makes sense.
Thanks,
|
|
|
|
|
It depends on what type your List is. Do you mean that you want to add a blank item to something like a combobox? If so, and you are using Win Forms or ASP.NET, you can bind to your data source, then insert the blank to the top of the list afterwards using something like myCombo.Items.Insert(" ", " "); "WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
yes thats right I want to add a blank item to the combobox. I have tried the
combobox.items.insert("","") but am getting the error "Cannot add items to the combobox when a datasource is assigned". Using WINFORMS
So I thought of creating a generic list of type T and pass a new instance of the item T to the list before the List is bound to the datasource.
private static List<T> CreateList<T>(List<T> data)
{
List<T> list = new List<T>();
list.AddRange(data);
return list;
}
I hope this makes sense.modified on Friday, February 26, 2010 6:26 AM
|
|
|
|
|
If you don't know what is the blank value in advance, you can't do it the way you are trying.
You can, for example, add a "" (or string.Empty) in a list of strings, but this will not work for other types.
A possible solution is to create a generic "wrapper".
For example:
public struct MyWrapper<T>
{
public MyWrapper(T value)
{
_value = value;
_hasValue = value != null;
}
private T _value;
public T Value
{
get
{
return _value;
}
}
private bool _hasValue;
public bool HasValue
{
get
{
return _hasValue;
}
}
public override string ToString()
{
if (!_hasValue)
return "";
return _value.ToString();
}
}
As this wrapper is a struct, it can be initialized by the default constructor, in which case the _hasValue is false, so the ToString will return blank. If you initialize it with null (considering T is a reference type) it will do the same.
So, you can create a List<MyWrapper<T>> and add the first value as new MyWrapper<T> and then add a wrapper value for each value of the original list.
But, if the original list is changed, this list will not be.
*I didn't test the code, so maybe there is an error, but I think you can get the idea.
You can also go even further and create an IList wrapper over another IList. It is a big work, but:
You can always return Count as Count + 1.
When requested for item 0, you always return the empty wrapper value.
When requested for any other item, you return a wrapper over the real list item at (index-1) [after all, requesting item 1 means item 0 in the real list].
The advantage is that changing any item in the real data-source will be reflected in your wrapper data-source also, but it is a much more complex task.
|
|
|
|
|
Here's a quick sample that should work:
private void BindToComboBox()
{
List<string> items = new List<string>();
items.Add("Hello");
items.Add("Hello 1");
items.Add("Hello 2");
items.Add("Hello 3");
items.Add("Hello 4");
cboList.Items.AddRange(items.ToArray());
cboList.Items.Insert(0, " ");
} "WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
"Cannot add items to the combobox when a datasource is assigned"
You need to add the blank row to a datatable before you bind it to the combobox.
This is off the top of my head so will need debugged obviously but you will get the idea:
SqlConnection MyConnection = new SqlConnection(PublicVars.ConnectionString);
MyConnection.Open();
SqlDataAdapter sqlDA1 = new SqlDataAdapter("SELECT ItemID FROM some_items", MyConnection);
DataSet sqlDS = new DataSet();
sqlDA1.Fill(sqlDS, "some_items");
DataTable tblItems= sqlDS.Tables["some_items"];
DataRow NewRow = tblItems.NewRow();
NewRow[0] = "-- Click To Select --";
tblItems.Rows.InsertAt(NewRow, 0);
cboItems.DataSource = tblItems;
cboItems.DisplayMember = "ItemID";
cboItems.ValueMember = "ItemID";
cboItems.SelectedIndex = 0;
MyConnection.Dispose();
You should get the idea.
|
|
|
|
|
Hi everyone.
I'm having serious trouble with method EmitCalli of ILGenerator . I've created a simple class to illustrate what I'd like to do:
class TestClass {
public delegate Object DirectReadAccessor(TestClass firstArgument);
DirectReadAccessor[] directReadAccessors;
public Object directRead(int index) {
return directReadAccessors[index](this);
}
}
As you can see, I have an array of delegates, and by calling directRead(i) I call the the i -th delegate with this as a parameter. Fairly simple.
I'd like to generate the method directRead with Emit . I compile this and see the following IL code:
.method public hidebysig instance object directRead(int32 fieldIndex) cil managed
{
.maxstack 2
.locals init (
[0] object CS$1$0000)
--> L_0000: nop
L_0001: ldarg.0
L_0002: ldfld class TestClass.TestClass/DirectReadAccessor[] TestClass.TestClass::directReadAccessors
L_0007: ldarg.1
L_0008: ldelem.ref
L_0009: ldarg.0
L_000a: callvirt instance object TestClass.TestClass/DirectReadAccessor::Invoke(class TestClass.TestClass)
--> L_000f: stloc.0
--> L_0010: br.s L_0012
--> L_0012: ldloc.0
L_0013: ret
}
(where you can just ignore the lines pointed with --> )
I see that it uses callvirt on method Invoke , instead of using calli as was my first instinct. I have the notion that calli would be more efficient, though I may be wrong (please let me know what you think about that). So I try to use calli instead.
I read that Calli follows this process:
1. Method arguments arg1 through argN are pushed onto the stack.
2. The method entry pointer is pushed onto the stack.
3. Method arguments arg1 through argN and the method entry pointer are popped from the stack; the call to the method is performed. When complete, a return value is generated by the callee method and sent to the caller.
4. The return value is pushed onto the stack.
So here was my first attempt:
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldfld, directReadAccField);
ilGenerator.Emit(OpCodes.Ldarg_1);
ilGenerator.Emit(OpCodes.Ldelem, typeof(DirectReadAccessor));
ilGenerator.EmitCalli(OpCodes.Calli,
CallingConventions.Standard | CallingConventions.HasThis,
typeof(Object),
new Type[] { typeof(TestClass) },
null);
ilGenerator.Emit(OpCodes.Ret);
This generates the following IL, which apparently isn't valid and can't even be translated back to C#:
.method public instance object directRead(int32) cil managed
{
.maxstack 3
L_0000: ldarg.0
L_0001: ldarg.0
L_0002: ldfld class [TestClass]TestClass/DirectReadAccessor[] EXT_IdPoolImpl::directReadAccessors
L_0007: ldarg.1
L_0008: ldelem.any [TestClass]TestClass/DirectReadAccessor
L_000d: calli method instance object *(class [TestClass]TestClass)
L_0012: ret
}
Seeing that the generated code used ldelem.r ef instead of ldelem.any , as I had done with Emit, I changed to ldelem.r ef as well:
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldfld, directReadAccField);
ilGenerator.Emit(OpCodes.Ldarg_1);
ilGenerator.Emit(OpCodes.Ldelem_Ref);
ilGenerator.EmitCalli(OpCodes.Calli,
CallingConventions.Standard | CallingConventions.HasThis,
typeof(Object),
new Type[] { typeof(TestClass) },
null);
ilGenerator.Emit(OpCodes.Ret);
I get this:
.method public instance object directRead(int32) cil managed
{
.maxstack 3
L_0000: ldarg.0
L_0001: ldarg.0
L_0002: ldfld class [TestClass]TestClass/DirectReadAccessor[] EXT_IdPoolImpl::directReadAccessors
L_0007: ldarg.1
L_0008: ldelem.ref
L_0009: calli method instance object *(class [TestClass]TestClass)
L_000e: ret
}
But still no luck.
Then, although it seems to contradict the instructions above (the steps followed by calli ) I tried moving my first Ldarg_0 to just before calli , to mimic what is done with callvirt :
.method public instance object directRead(int32) cil managed
{
.maxstack 2
L_0000: ldarg.0
L_0001: ldfld class [TestClass]TestClass/DirectReadAccessor[] EXT_IdPoolImpl::directReadAccessors
L_0006: ldarg.1
L_0007: ldelem.ref
L_0008: ldarg.0
L_0009: calli method instance object *(class [TestClass]TestClass)
L_000e: ret
}
But nothing.
I can use callvirt and the code works, but I'd like to know how to use calli , if not for performance just for the sake of knowledge
Can anyone explain?
Thanks a lot, and sorry for the lengthy post.modified on Friday, March 5, 2010 3:23 AM
|
|
|
|
|
Reading the ECMA specs, it looks like you might need a ldvirtftn instruction before the calli instruction. The calli instruction expects a native pointer to the method rather than a metadata token.
|
|
|
|
|
Thanks for your answer, Kythen.
However, this is bad news for me...
I need to call a method that is already on the stack. I cannot load it with ldvirtftn because I don't have it at the time when I'm generating the code.
The situation is like this: I'm trying to generate the following class:
public MyClass {
public delegate Object DirectReadAccessor<T>(T firstArgument);
private static DirectReadAccessor<MyClass>[] directReadAccessors;
public Object DirectRead(int i) {
directReadAccessors[i](this);
}
public static void InitializeClass(MethodInfo[] directReadAccessorsMInfo) {
int length = directReadAccessorsMInfo.Length;
Type[] typeArguments = new Type[] { typeof(MyClass) };
directReadAccessors = new DirectReadAccessor<MyClass>[length];
for (int i = 0; i < length; i++) {
directReadAccessors[i] = (DirectReadAccessor<MyClass>)
Delegate.CreateDelegate(
DirectReadAccessor<MyClass>,
null,
directReadAccessorsMInfo[i].MakeGenericMethod(typeArguments)
);
}
}
}
* About open instance methods (and other things).
This has been tricky because MyClass doesn't exist when I'm trying to declare the field directReadAccessors , which is of type DirectReadAccessor<MyClass>[] , or when I emit the method InitalizeClass , which again uses MyClass , that doesn't exist yet (that's what I'm creating). However, I've managed to do all this, but now I'm having trouble with method DirectRead , since I don't know how to call the delegate once I have it on the stack. Apparently what I need is the following emit:
ilGenerator.Emit(OpCodes.Callvirt, invokeMInfo);
where invokeMInfo is the method Invoke on DirectReadAccessor<MyClass> , and which I should obtain like so:
MethodInfo invokeMInfo = typeof(DirectReadAccessor<MyClass>).GetMethod(
"Invoke",
BindingFlags.Instance | BindingFlags.Publ<code></code>ic | BindingFlags.NonPublic,
null,
new Type[] { typeof(MyClass) },
null
);
Again, the problem is that neither MyClass nor DirectReadAccessor<MyClass> exist yet. I have the TypeBuilder for MyClass and the unfinished DirectReadAccessor type, which I've created like this:
directReadAccessorType = typeof(DirectReadAccessor<>).MakeGenericType(typeBuilder);
But if I try to call GetMethod("Invoke", ....) on directReadAccessorType as shown above I get a NotSupportedException , because I cannot obtain the method Invoke for an unfinished type. I've tested this assumption by making the same call after finalizing the type with:
typeBuilder.CreateType();
And indeed I do not get the exception in that case. However, I need to be able to get the Invoke method's MethodInfo before finalizing the type, while I'm emitting the code for InitializeClass .
It's a strange situation: I'll have the delegate when I need it, but I cannot produce the code to invoke it. Can anyone offer any help?modified on Friday, March 5, 2010 3:23 AM
|
|
|
|
|
In case anyone is insterested, they answered this for me at stackoverflow. The following call works:
TypeBuilder.GetMethod(directReadAccessorType, typeof(DirectReadAccessor<>).GetMethod("Invoke"));
|
|
|
|
|
Good morning all,
I am currently working on enslaving the webbrowser control to be an editor the way I like it.
One problem I steped into was the lack of beeing able to supply a target for links.
My first approach was to write my own Link-Selection window (no problem here) and it works like a charm on all text-selections.
But unfortunately not on images...
Is there a way to get the selected image?
If my text-range is empty I at least know that I have to deal with an image, but unfortunately not on which one.
I was able to iterate the link collection of the document before saving it and add a target there via setAttribute, but still I would like to know how to get the selected image.
I noticed that when using the execCommand (CreateLink) the selected image (which got a border with the resizing dots on the edges) gets a selection overlay that I am also not able to reproduce. Maybe changing the selction into that would solve the problem...
Any ideas on this one?
best regards and have a nice day
hoernchenmeister
|
|
|
|
|
Iam very new to this community, i want to read a .doc and display it to console. I tried with the following code but getting some garbage values in addition to actual code.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StreamReader file = new StreamReader("D:\\keywords\\id1.doc");
String st = "";
while (st != null)
{
st = file.ReadLine();
Console.WriteLine(st);
}
file.Close();
}
}
}
Can some body helpme out with the Process for reading a .doc file...
Thanks in Advance....
|
|
|
|
|
Message Removed
modified 23-Nov-14 7:08am.
|
|
|
|
|
|
I suggest looking at that example again. That code is not specific to a Windows Form application. It can be used in both, aybe with a tweak or two to fit specific requirements.
|
|
|
|
|
|
Whell, abouve post all requiers Office installed.
If you want independed application, start reading this: Office Binary Protocol[^]
|
|
|
|
|