|
This is documented right here[^]. This is NOT SUPPORTED in C# 1.0 and 1.1. This is a new capability introduced in C# 2.0.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
|
i am using the following function to close some open forms with tag "NOTMAIN" in my application.
private void CloseOldForms()
{
FormCollection frmColl = Application.OpenForms;
foreach (Form tempFrm in frmColl)
{
string tempFrmTxt = tempFrm.Tag.ToString();
if (tempFrmTxt.Equals("NOTMAIN"))
{
tempFrm.Close();
}
}
}
But after looping one time in foreach loop,the application stops with an exception i.e enumeration was modified operation may not execute.
So can anybody tell how can i do the task
|
|
|
|
|
The call to tempFrm.Close() modifies the Application.OpenForms property. Try this:
private void CloseOldForms()
{
ArrayList frmColl = new ArrayList();
frmColl.AddRange (Application.OpenForms);
foreach (Form tempFrm in frmColl as Form) {
string tempFrmTxt = tempFrm.Tag.ToString();
if (tempFrmTxt.Equals ("NOTMAIN")) {
tempFrm.Close();
}
}
}
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
Try this instead:
private void CloseOldForms()
{
FormCollection frmColl = Application.OpenForms;
foreach ( int i = 0; i < frmColl.Count; ++i )
{
Form tempFrm = frmColl[i];
string tempFrmTxt = tempFrm.Tag.ToString();
if (tempFrmTxt.Equals("NOTMAIN"))
{
tempFrm.Close();
}
}
}
I'm not absolutely sure of this, but I think that when you call Close on your form, it is attempting to remove the form object from the form collection automatically which is what causes this error since you are at that point in effect trying to alter the contents of the collection over which you are iterating.
Hope that helps.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
perlmunger wrote: foreach ( int i = 0; i < frmColl.Count; ++i )
This WILL cause an IndexOutOfRange exception. The solution is very easy, but not so obivous:
foreach ( int i = frmColl.Count; i >= 0; i-- )
Run the the loop in the other direction! Start at the end and work your way back to the front...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-- modified at 14:47 Saturday 11th February, 2006
|
|
|
|
|
Good point.
Thanks.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
perlmunger wrote: foreach ( int i = 0; i < frmColl.Count; ++i )
This may cause index out of rnage exception. I think while (frmColl.Count>0) woulld be a safer alternative. In which case, you will also have to replace the following line with
Form tempFrm = frmColl[0];
- Malhar
|
|
|
|
|
That won't work because there is the potential that not all forms are going to be closed and removed from the collection, therefore this would result in an infinite loop - which is less than desirable.
ColinMackay.net
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
|
|
|
|
|
You may be right. I hadn't seen this property "OpenForms" of Application class. I am assuming it is newly introduced in 2.0,
- Malhar
|
|
|
|
|
It was nothing to do with that. It was to do with this:
if (tempFrmTxt.Equals("NOTMAIN"))
{
tempFrm.Close();
} That introduces the aspect that no all forms will be closed. Which means that the while loop becomes infinite if as much as one form is to stay open.
ColinMackay.net
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
|
|
|
|
|
You're 100% correct. I had overlooked that little piece of detail.
- Malhar
|
|
|
|
|
thank you all for taking some time to reply.i finally managed to overcome the problem with ur suggestion.the following code now works fine.
private void CloseOldForms()
{
FormCollection frmColl = Application.OpenForms;
for (Int32 index = 0; index < frmColl.Count; index++)
{
string tempFrmTxt = frmColl[index].Tag.ToString();
if (frmColl[index] != this)
{
if (frmColl[index].Text.Equals("NODEINFO"))
{
frmColl[index].Close();
index--;
}
}
if (tempFrmTxt.Equals("NOTMAIN"))
{
frmColl[index].Close();
index--;
}
}
}
|
|
|
|
|
the problem is in my webservice, but this is in C#, and the problem is more C# related.
the code below is the code i use, it always return false through the Catch, the Catch returns an error found in here[^] (the blue boxes are the absolute paths, you don't need them).
the problem i can't figure out, is why?
if someone can help me i would be very thankful.
yours, NaNg.
[WebMethod]<br />
public bool AddProject(string ProjName, string ProjID)<br />
{<br />
try<br />
{<br />
objCon.Open();<br />
DataSet ds = new DataSet();<br />
objDA = new OleDbDataAdapter(string.Format("Select Count([SID]) from tblProjects Where SPorjID = '{0}' And SProjName = '{1}'", ProjID, ProjName), objCon);<br />
123 -> objDA.Fill(ds, "tblProjects");<br />
if ((int)ds.Tables[0].Rows[0][0] == 0)<br />
{<br />
objCmd = new OleDbCommand(string.Format("Insert into tblProjects(SProjID, SProjName) Values('{0}', '{1}')", ProjID, ProjName), objCon);<br />
objCmd.ExecuteNonQuery();<br />
return true;<br />
}<br />
else<br />
return false;<br />
}<br />
catch (Exception exp)<br />
{<br />
return false;<br />
}<br />
finally<br />
{<br />
objCon.Close();<br />
}<br />
}
|
|
|
|
|
You either need to set the insert command's command text or you need to use parameters. Change your code to do this instead:
string selectCommand = "SELECT COUNT...";
objDA = new OleDbDataAdapter( selectCommand, connection );
objDA.InsertCommand.CommandText = string.Format("Insert into tblProjects(SProjID, SProjName) Values('{0}', '{1}')", ProjID, ProjName);
objDA.InsertCommand.ExecuteNonQuery();
Alternatively, you can use parameters like this:
string selectCommand = "SELECT COUNT...";
objDA = new OleDbDataAdapter( selectCommand, connection );
objDA.InsertCommand = new OleDbCommand( "Insert into tblProjects(SProjID, SProjName) Values( ?, ?)", connection );
objDA.InsertCommand.Parameters.Add( "@SProjID", OleDbType.VarChar, 30, "SProjID" ).Value = ProjID;
objDA.InsertCommand.Parameters.Add( "@SProjName", OleDbType.VarChar, 30, "SProjName" ).Value = ProjName;
objDA.InsertCommand.ExecuteNonQuery();
Hope that helps.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
hmmm... =\ both of them give me the same exception as before, nothing works and i don't know why =\
and i want to focus you, the problem is not in the insert command, but in the select command.
string.Format("Select Count([SID]) from tblProjects Where SPorjID = '{0}' And SProjName = '{1}'", ProjID, ProjName);
|
|
|
|
|
i'm sorry for mistaking you....
i found the problem and fixed it, i almost couldn't find it, i was wrong in the field name, between the r and the o... thanks alot
|
|
|
|
|
Glad you figured it out.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
Hi All,
I need to create 2 list in my application.
I want to add Grid to each list ( to make easy the user to read the list ).
For example .... first list will hold all process that run in the machine that the other list will contain info about the process that the user will be focus on (on mouse click even the second list will be update).
My problem is
1.What object i need to use to get this grid list ?
2.I do i get the path of some process that run ? ( the path of the file of the process )
Thanks for any help.
-- modified at 9:50 Saturday 11th February, 2006
|
|
|
|
|
|
First, if you post a question in the forum, you better be patient waiting for an answer. We don't get paid to do this since The Code Project is entirely a volunteer effort. On top of that, you asked a question on the weekend!
To answer your first question, a ListView control will do everything you appear to be specifying, but in one control. It'll look just like the list in the Task Manager.
As for getting the path to the process, you need to get the MainModule of the Process, then you can use the FileName property of the module. Be prepared to handle some exceptions though. YOu can't get the Filename for the System or Idle processes.
For Each proc As Process In Process.GetProcesses()
Try
Debug.WriteLine(String.Format("{0} - {1}", proc.ProcessName, proc.MainModule.FileName))
Catch ex As Exception
Debug.WriteLine(String.Format("{0} - N/A", proc.ProcessName))
End Try
Next
Damn! Wrong forum again!! Here's the C# version:
foreach (Process proc in Process.GetProcesses())
{
try
{
Debug.WriteLine(string.Format("{0} - {1}", proc.ProcessName, proc.MainModule.FileName));
}
catch (Exception ex)
{
Debug.WriteLine(string.Format("{0} - N/A", proc.ProcessName));
}
}
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-- modified at 16:52 Saturday 11th February, 2006
|
|
|
|
|
Hello gurus,
I'd like to know how I can browse all SQL servers over a enterprise network in C# in order to build the connection string. The user could select the SQL server on wich (s)he want to connect to like the "Browse for servers" dialog box from the "SQL Server Management Studio" does...
Can someone show me a code snippet for doing this?
Thanks in advance for the help.
Best regards.
Fred.
There is no spoon.
|
|
|
|
|
You don't mention if you are using SQL Server 2000 or 2005. I'll assume 2000 because I can provide you with an answer (though I would be surprised if the answer was much different in 2005).
You need to use SQLDMO. This is a COM component that allows you to programmatically control SQL Server. Here is a basic snippet for loading a list of servers:
ArrayList serverList = new ArrayList();
SQLDMO.Application dmo = new SQLDMO.Application();
SQLDMO.NameList nameList;
nameList=dmo.ListAvailableSQLServers();
foreach ( object svrName in nameList)
{
string server = svrName.ToString();
if (server == "(local)")
{
server = "localhost" ;
}
serverList.Add ( server ) ;
}
The section where I check to see if the current server name is (local) and then change it to localhost is there because if you provide (local) in your connection string, it won't work properly. It expects localhost.
You can now use the ArrayList serverList in a listview or a combobox. In order to use SQLDMO, you will need to add a reference to it. Just right-click and "Add Reference" on the references folder in your project. Then click the COM tab in the ensuing dialog. Search through that list for "Microsoft SQLDMO Object Library". Click Select and then Ok. Visual studio will automatically make a .NET wrapper for the object and you should be able to use it as I have specified in my code above (though the SQLDMO namespace may come up as something different in yours. Just look at the references list once it's been added to know if you need to alter that in your code).
Let me know if you have any questions.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
When I wana to run this project http://www.codeproject.com/csharp/FTP/FTPCom.ZIP but it has a some errors
any one can run ?
|
|
|
|
|
Maybe you should try posting to the articles message board?
I did notice that it was developed using VS 2002 & .Net Framework 1.0, most people are now using v1.1 of the framework. Maybe this is the problem?
Regards
Wayne Phipps
____________
Time is the greatest teacher... unfortunately, it kills all of its students
View my Blog
|
|
|
|