|
Hi,
please next time use the "code block" button to publish code, that way you get PRE tags which
provide a background and a monospaced font, and preserve formatting; all this makes it a lot
easier to read and help you.
Nevertheless there are two major errors I could spot:
1.
you are using File.ReadAllText() which returns a single string, hence your listbox contains only one item.
2.
you are using ListBox.Find(string str) which searches for an item STARTING with str, probably
not finding anything (because you only have one item, and it probably does not start with @)
You will need some more code to get your app to do what you want...
BTW: I advise against MessageBox for tracing what is going on, because you can see only one at a
time, once closed they are gone forever, and they soon become very annoying. I recommend you use
Console.WriteLine() instead. That creates a trace log in the output pane of Visual Studio, and does
not stop your thread.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 11:26 PM
|
|
|
|
|
hi there again.
I guess the first error you spoted is right,I'm using File.ReadAllText() which returns a single string,hence my listbox contains only one item.what should I do to make it not just one item?I mean it reads the text file and returns one item,instead of the several items that there are!!
|
|
|
|
|
Hi,
I am not going to hold your hand and spoon feed you.
There is a lot of documentation available that gives you all the details of the available classes and methods; whatever way you got hold of ReadAllText() is likely to also contain the right methods for your needs.
And if you are new to programming, to .NET or to C# the one and only right way to get started is by buying and studying a book on the subject. That way you get the overall picture, all the basic terminology and information, as well as a lot of examples; all this in a much shorter time than it would take you in any other way.
BTW I do not recommend any particular book, go to the bookstore, look at some, and pick one or two that look promising.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 11:27 PM
|
|
|
|
|
hi.
tnx for your help.I had searched and read 2 E-books and did'nt find what I was looking for.I studied one,was'nt there either.searched MSDN found something helpful,but not exactly though.I guess I should change the method of my researching.
but thank you,you did help me.
|
|
|
|
|
I recommend you buy and study a real book; downloading and browsing an ebook is no substitute.
Electronic information is fine as reference material, but introductions and tutorials better be
actual books you can read at your leisure; they do cost a few dollars/euros but are worth what you pay for them.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 11:27 PM
|
|
|
|
|
hi.
well in that case what book do you suggest?would Christian Nagel's be fine?or Andrew Stellman & Jennifer Greene's?I've heard so much about Christian Nagel complete refrence!!is that worthy?
|
|
|
|
|
Hi,
I don't know the books you are mentioning, which does not say anything about those books.
I own one where Nagel is one of many authors.
As I said before: "I do not recommend any particular book, go to the bookstore, look at some, and pick one or two that look promising"
To learn a language or technology, you need an introduction or a tutorial, not a "complete reference".
However you may want to possess a reference book too; it should not be your only book on the subject.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Monday, February 9, 2009 3:45 PM
modified on Friday, June 10, 2011 11:27 PM
|
|
|
|
|
I have a windows form program that needs to check records to make sure they are valid, and if so will append the description to the record. In an effort to reduce the impact this has on our database server, I'm loading everything I need into memory using a DataAdapter and filling a DataTable. Now that I have my table filled, I want to go through each item number and search the table to see if it exists...that's where I seem to be stuck. I'm open to other ideas if this isn't the best method. I've considered arraylists, dictionarys, even jagged arrays but couldn't figure out how to iterate through the DataReader to fill the arrays which is why I fell back to the DataTable. Here's what I have so far:
string[] AllContents = File.ReadAllLines(scanner_number);
DataTable dt1 = inv1.get_part_information();
foreach (string s in AllContents)
{
string[] splitup = s.Split(new char[]{}, StringSplitOptions.RemoveEmptyEntries);
<big>DataRow[] row = dt1.Select("'I$PN' = 'CA-106-77'");</big>
string test1 = row[0].ToString();
string test2 = row[1].ToString();
splitup[2] = splitup[2].Remove(8);
splitup[2] = splitup[2].Substring(4, 2) + "/" + splitup[2].Substring(6, 2) + "/" + splitup[2].Substring(0, 4);
textBox1.Text += "...." + s + Environment.NewLine;
}
now, I've hard coded the select string because I'm just trying to figure out how to get the Expression correct. The system appears to find the column correctly, and that is a verified value, however it always fails on the string test line...and watching the datarow in visual studio shows that there is no data in the row.
I've searched around online and did see one forum suggest trying something like this considering I am only ever expecting one row to be returned:
DataRow row = dt1.Select("'I$PN' = 'CA-106-77'")[0]
but this gives me the indexoutofrange exception.
"You're damned if you do, and you're damned if you dont" - Bart Simpson
|
|
|
|
|
Hi,
I'm no DB specialist but did some, so I'll try and help:
1.
itmaster21 wrote: DataRow row = dt1.Select("'I$PN' = 'CA-106-77'")[0]
is bound to fail if no data is found: you can't access row 0 when there are no rows at all.
itmaster21 wrote: DataRow[] row = dt1.Select("'I$PN' = 'CA-106-77'");
seems OK with two comments:
1. it is unfortunate to have special characters in field names, without the dollar sign, the field name should not need single quotes, it could look like DataRow[] row = dt1.Select("name = 'CA-106-77'");
2. you are confusing yourself by calling the result row, it really should be rows indicating you may obtain 0, 1 or any number of rows.
itmaster21 wrote: string test1 = row[0].ToString();
string test2 = row[1].ToString();
is unsafe again: the number of rows returned can be less than 2, in which case one or both index operations would fail with an IndexOutOfRangeException. You need to program more defensively.
itmaster21 wrote: splitup[2] = splitup[2].Remove(8);
Dito. What guarantee do you have splitup holds 3 or more elements?
At the very least, you should put everything in a try-catch construct, and show the entire exception
if and when you get it (that is Exception.ToString, not just Exception.Message).
Hope this helps.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Hello Luc,
Thank you for the response. Well, the splitup string array is actually where I am parsing out a text file I download from a hand held scanner basically containing the item, quantity, and date seperated by spaces. Up until now I've honestly never really worried too much about Exception handling in this stage. I'm a network professional by trade, but when I would work on programming projects like this I generally throw everything together sloppy to "learn" or figure out if I can even do what it is I'm trying to do before going back through and adding my try, catch blocks and writing the user friendly messages when an exception occours. It's probably not good, however, to get into that habbit though, huh? Thanks!
Furthermore, I know it's tricky when you have special characters in the column but this was all designed way before I came on board and I know they're not going to let me change that now
I'll play around with the code a bit and try to repost an update tomorrow.
"You're damned if you do, and you're damned if you dont" - Bart Simpson
|
|
|
|
|
Hi Luc,
I was able to get this to work. Looking at the debugger again, the system see's the column name with double quotes. I was able to get it to find the record by using this search expression:
DataRow[] rows = dt1.Select("[\"I$PN\"] = 'CA-106-77'");
Notice I changed the name to rows! Now I'm going to spend some time writing some try and catches just in the event there are no rows or multiple rows found.
Thanks for all of your help and input!
"You're damned if you do, and you're damned if you dont" - Bart Simpson
|
|
|
|
|
|
Hi Ennis,
That's the really weird part. I've tried enclosing the column in [], and when I do that the debugger throws the error: "Cannot find column named "CA-" which doesn't make any sense to me. Playing around, the only way I can get it to find the column is wrapping single quotes around it.
"You're damned if you do, and you're damned if you dont" - Bart Simpson
|
|
|
|
|
The link suggests that '-' is a special character and must be escaped.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
Oh, I thought it said you only needed to do that for the column name. So it should look like this? [I$PN] = 'CA\-106\-77' ?
update. Ah, I got it working. I used the debugger and looked at the table again. The table see's the column header with double quotes in it. I was able to get it to work by using this.
DataRow[] rows = dt1.Select("[\"I$PN\"] = 'CA-106-77'");
Thank you for your help!
"You're damned if you do, and you're damned if you dont" - Bart Simpson
modified on Monday, February 9, 2009 8:59 AM
|
|
|
|
|
First: What Is a User-Control Embedded Class?
A User-Control Embedded Class is a user-control with no U.I. but has a separate class (such as public class MyEmbeddedClass() { }) located just below the user-control's partial class, and the class is just like any other class, having properties, private and static methods (mostly static), etc., such that when the user-control is placed on a web page, only that webpage wherein the user-control is placed on can now call MyEmbeddedClass() methods.
And, in a web app that compiles on the fly, only that web page is impacted when MyEmbeddedClass (located in the user-class'es code-behind) is changed.
Usage:
Create a user control called classMyStuffcontrol.ascx, then add a SEPARATE class to it as follows
public class MyStuff(){
}
We'll pretend it is a decent sized class, having 12 or so static methods (functions), around 500 lines of code.
Focus #1: Now, this user control is needed on about 7 web pages and 3 other user controls.
1) Does this mean, in Focus #1, that there would be 10 instances of the class Mystuff (within classMyStuffcontrol.ascx), even though there are only static methods?
Focus #2: Now, one of those pages which already uses one of the user controls that has MyStuff() in use also needs access to the class MyStuff, so I add the MyStuffControl to the web page as well..
2) Does this mean that there are multiple instances of the class MyStuff for that webpage and user-control, both using classMyStuffcontrol.ascx, would there be 2 instances of the class MyStuff instantiated even though there are only static methods?
Now, before ya start asking "why", here's why I do this:
- some 3 years ago I was asked to create a single user control that did tons of stuff; no extra code files would be accepted. So I coded the whole thing in a single UI page and corresponding code-behind page (those 2 files were acceptable). The code behind had 6 separate classes which focused on a given set of tasks.
Soon after this, a different client asked for the same thing once they became aware that I had done something like this already. From there, it kept snowballing; client after client wanted similar output (1 or 2 files containing all the code for a big solution.
After 6 months, I set aside this 'practice', but a few months later started using it wherein only a single class was associated to a single user-control. Now, there are still the universal / web-app-wide classes I used, but alot of the other classes were specific to a given area, and/or not used as much as a webapp-wide class would be used.
What this permitted was my ability to change the user-control embedded class, affecting only those web pages (wherein an on-the-fly compile took care of it) versus a web-app restart (such as what occurs when the web.config file is changed).
Have I described this enough such that you would be able to provide feedback as to its usefulness and/or practicality? Specifically, am I asking for trouble down the line or are user-control embedded classes, when used in moderation, a reasonable solution?
Thx.
ASPX ~ Apple Simply Performs eXcellently
|
|
|
|
|
Gary - if I were you, I'd ask this question in the General IT forum or the .NET forum. The problem with posting here is that this is a high traffic forum, so your post will soon get swamped. As it is, the post is worthy of more consideration so I would definitely consider moving it.
|
|
|
|
|
Well, after posting it 2 other times to other .Net forums on CodeProject, and multiple times on other sites, yours is the *only* reply I've received. So, if "this post is [really] worthy of more consideration", you and I seem to be the only ones who conclude such.
http://www.codeproject.com/script/Forums/View.aspx?fid=12076&msg=2888591[^]
ASPX ~ Apple Simply Performs eXcellently
|
|
|
|
|
I've used xsd.exe to great effect in the past to generate classes from xsds, but what I'd like to do now is do the same thing at runtime. The idea is to provide a plugin-type architecture where xsds can be dumped into a certain location and on its next run my application will pick them up, generate code from them and add that code to itself. I can't go into specifics too much, but I can give a more general example:
Imagine an app that by default only knows how to handle certain xml files. What I want to do is essentially provide that application with the ability to understand other document types by giving it a definition (xsd) of what those types look like. From the definition the app will be able to generate classes from which it can instantiate objects of the document's type. Assuming the generated class conforms to an interface (which it will) generic code can then be written to use any document type without knowing anything about that type in advance.
I'm not too concerned about the more theoretical bits here, I'll try that myself and see if it works as I think it should, but if anyone has any examples of how a class can be created from an xsd, so I can perpetrate this madness I'd be grateful.
|
|
|
|
|
Just use a DOM parser. It will save time.
If you really, really, really want to try this approach, spawn a process, run xsd.exe with valid options on the file, run csc.exe on the .cs file created, use and Assembly.LoadFile to load the assembly.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
Hi
I want to read the caption(title) of another window in C#. Is it possible? If it is how can I do it?
Thanks.
|
|
|
|
|
Use P/Invoke. Get the hWnd of a window through one method or another, then call SetWindowText, passing the new title and hWnd as parameters
|
|
|
|
|
|
Hi folks,
i´m trying to get a SelectedListViewItemCollection threadsafe, because i´m using it in a method which is called from another thread (FileSystemWatcher_Created).
I have tried to get the collection using a delegate function, but it won´t work fine.
Here is the code:
private void FuelleUnitsAuto()
{
Invoke(new clearListViewDelegate(BeginUpdateListView), new object[] { lV_Einheiten });
Invoke(new beginUpdateListViewDelegate(ClearListView), new object[] { lV_Einheiten });
ListView.SelectedListViewItemCollection coll = (ListView.SelectedListViewItemCollection)Invoke (new selectedListViewItemsDelegate(SelectedListViewItems), new object[] { lV_Num });
foreach (cEinsatzListViewItem i in coll)
{
}
Invoke(new endUpdateListViewDelegate(EndUpdateListView), new object[] { lV_Einheiten });
}
private delegate ListView.SelectedListViewItemCollection selectedListViewItemsDelegate(ListView view);
private ListView.SelectedListViewItemCollection SelectedListViewItems(ListView view)
{
return view.SelectedItems;
}
Anyone knows this error and can help?
Thanks a lot in advance,
Tobe
modified on Friday, February 6, 2009 10:22 AM
|
|
|
|
|
Does someone know this problem?
|
|
|
|
|