|
leppie wrote:
test case
That was the idea. It definitely looks like a problem and I don't have the time to play around with other options. Just wondering if you tried diagnosing the potential but with other cases.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi!
I'm trying to keep track of forms I spawn in my code. I am using the usual instantiation to create the form (Form oForm = new Form()), however I have multiple instances of that form running, and I need to be able to check the name of each one (Maybe using the Tag property).
I've thought about a foreach loop, but I don't know exactly how to implement it.
Thanks!
|
|
|
|
|
You need to keep track of these Form s in a collection so that you can enumerate them. Using a foreach loop is easy - just be sure you don't change the underlying collection.
So, if you had a static collection property on a class (perhaps your Form , if you're dealing with multiple instances of a single Form class, for example), you can add your instances to it and enumerate it at any time:
public class MyForm : Form
{
private static ArrayList instances = new ArrayList();
public MyForm()
{
instances.Add(this);
}
protected override void Dispose(bool disposing)
{
instances.Remove(this);
base.Dispose(disposing);
}
public static IList Instances
{
get { return instances; }
}
} At any time, just use the following code to enumerate your forms and, for example, output the caption:
foreach (MyForm form in MyForm.Instances)
Console.WriteLine(form.Text); If you want to do this for multiple types, then define a class with a public or internal static collection property, perhaps adding a few handy methods. Don't forget to override Dispose(bool) and remove your form instance, and to add it in your constructor for that matter. If you overload your constructor, make sure that only one instances gets added.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for the reply! I think your way sounds good, I'll try it right away.
Thanks
|
|
|
|
|
I am building an N'tier application and i want to save some constants to one of the tiers wich is a dll file.
can i use a specific configuration file for it? I want it to be in the same directory with the dll and not with exe file of the application, when i am trying it i can't reach it, i mean i cant read from it.
if i have to build an excutable loader for this dll how can i do it, how to build this excutable loader???
Thanks in advanced
Dudi
Be Good...
Dudi
|
|
|
|
|
Building an executable loader won't help. The .config file is configured when the AppDomain is created and all assemblies loaded into that AppDomain use the AppDomain.SetupInformation.ConfigurationFile.
If you need to use a different file, then simply read-in a file without using the ConfigurationSettings class.
If you want the location of the DLL, use Assembly.Location (or other properties based on your deployment scenario), where the Assembly is your DLL. The easiest way to get this is to reference a Type and get the Type.Assembly property. So, if in your DLL you did something like the following, that would get you the filename; append ".config" (for example - could be anything) and you can manually parse that yourself (depending on the content of the file, you could use the XML classes, XML Serialization, etc.):
string path = this.GetType().Assembly.Location;
path += ".config";
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I need to display listview items in a combobox. From what I have read I need to use a datagrid instead of a listview to achieve this. But I don't understand how to save data in a datagrid. Anyway, if anyone can help me or knows of a good example of displaying listview items in cobobox it would be of great help.
|
|
|
|
|
I'm constructing a search string based on input from user in a web application. This search string basically translates to a search criteria and is assigned to a RowFilter on a dataview. My code throws an exception when my search strings contains quotes and other SQL characters which might invalidate the search criteria.
How do I make sure that this doesnt happen, ie. how to avoid sql escape characters from my search string. Is there a method in .net which will help me. I tried google, searching for SQL escape characters but I couldnt find anything relevant to what i'm looking for.
Also note that the filtering is done on a dataview which is already on cache.
Thanks,
Kannan
|
|
|
|
|
I think the only problem is with ' and you can just replace it with '' (not a quote, but 2 apostrophies).
|
|
|
|
|
I have this method in one of my utility classes:
public static string ToSQL(object value)
{
if (value == null)
return "NULL";
if (value is string)
return "'" + value.ToString().Replace("'", "''") + "'";
return value.ToString();
}
It's handy for SQL search criteria.
Regards,
Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
|
|
|
|
|
The easiest way to handle this is used parameterized SQL expressions in ADO.NET. For example, if you use a SqlCommand , use the SqlParameter s in the SqlCommand.Parameters (the property documentation includes an example). Constructing the SQL string without the parameters is straight forward enough since you have total control over it. Using parameters for the values (for an INSERT, UPDATE, DELETE, etc.) will eliminate the common problems (trying to inject another statement) while taking care of escaping quotes for you.
This is by far a better and safer alternative. The days of concatenating strings to form a SQL expression are (or at least should be) over.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for the tip, I was aware of this, but was wondering how one could use them for search criteria as that of a RowFilter which expects a SQL string.
Thanks,
Kannan
|
|
|
|
|
You're on your own, there. There is a good side, though: users can't cause an injection of an invalid SQL statement in the RowFilter property, thus - potentially - whiping out your entire database. An Exception would be thrown if anything is invalid. So, while there's nothing really to help you with that (just good ol' string parsing), there's not much harm in not checking for every possible exploit.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have developed following escape function that work fine with gridview rowfilter:
public static string Escape(string aString)
{
string searchStr = Regex.Replace(aString, "[.']", "''");
searchStr = Regex.Replace(searchStr, "[.]]", "[]]");
searchStr = Regex.Replace(searchStr, "[.[]", "[[]");
searchStr = Regex.Replace(searchStr, "[.*]", "[*]");
searchStr = Regex.Replace(searchStr, "[.%]", "[%]");
return searchStr;
}
Before passing string to rowfilter just escape your search string with above function. Should work
|
|
|
|
|
Hi all,
is dynamic loading of appDomains also work in compact framework .net ?
I have run example in dynamic loading in .net framework,
but since compact framework dont have AppDomainSetup Class,
so what can I do ?
The code in dynamic loading in .net framework is in
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp05162002.asp
but when the port the code to compact framework, the code doesn't work coz AppDomainSetup Class is missing in .net CF.
Can anyone point to sample code or articles on it ?
Thanks
|
|
|
|
|
I'm not sure if this fits in the right category under C# Forum. It's a Web-FORM, but Code-Behind is C#.
Gurus, I've been having a bit of a problem.
I want to manipulate the strings that's return
from the database.
Here's the Scenario:-
Front-End ASPX page:-
<asp:dropdownlist id="DropDownList1" Runat="server" AutoPostBack="True">
Code-Behind in c#:- (Assume i populate it on PageLoad)
I'm using DataReader.Execute to read the info retrieve from DB. And let's say the query return 1 column. "FirstName".
DropDownList1.DataSource = dbCmd.ExecuteReader();
DropDownList1.DataTextField = "FirstName";
DropDownList1.DataTextField = "FirstName";
DropDownList1.DataBind();
Now, this works right off the box. It populates the dropdownlist. But, this is not exactly what I want. There are some results from "FirstName" column that I want to manipulate. How do I do that ? In other words, how do i manually tap into the reading loop ?
Any sample code would definately help.
Please advice. Thanks
Stanley
|
|
|
|
|
Three ways come to mind:
1. Change the query to return the data in the format you need it.
2. Retrieve the data in a DataSet (instead of a DataReader), manipulate the DataSet, and bind to that.
3. Loop through the DataReader and add the data manually to the drop down (DropDownList1.Items.Add ).
Regards,
Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
|
|
|
|
|
Hi I've made a DLL because I want to use a library I'm developing many projects (Solutions) using it and since my library is still changing, I wouldn't like to change all my library files in every project when I want to modify my library.
So anyway, I made that dll, which works fine, and I added references to it in my projects, but now the default behaviour of VS.NET is to copy the dll in the local directory. But when they make a copy, the changes I make in my library doesn't apply in the directories in which it's been copied... (obviously).
So I'm wondering how to make this work out, I've tried to change the Copy Local property but I got the error message that says that the compiler can't find the reference...
Anybody can help me?
|
|
|
|
|
goldoche wrote:
But when they make a copy, the changes I make in my library doesn't apply in the directories in which it's been copied... (obviously).
I'm not sure that's true. Doesn't VS.NET automatically recopy any DLLs that have changed?
Regards,
Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
|
|
|
|
|
Make a project reference instead of an assembly reference. If your projects are in the same solution, right-click on a project and select Add Reference. Then click the Projects tab and select the projects you want to reference. This also establishes a build dependency so that any changes in the dependencies will cause them to be built when you build a project that references them. This will also copy the latest, build-specific (i.e., release, debug, etc.) assembly to the local directory for that project that depends on them.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks a lot... but what is the property "Local Copy" for?
|
|
|
|
|
It determines whether assemblies should be copied locally (to the target directory) or not. Assemblies found in the GAC (typically solidified assemblies - not those you're developing) don't need to be copied; it's a waste of time and space. Set "Local Copy" to false and when you run your app it will use those assemblies from the GAC or any other location you might have configured (via the .config file to configure a private path or assembly codebase, or using a publisher policy to define assembly redirections with an optional codebase).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I'm doing a program for a pocket-pc that has a "save as" dialog-box. When I execute it in the explorer, It's ok, but when I try to execute it in pocket-pc it doesn't work, in order to show the dialog-box only appears the text of the file. There is someone who can help me?
Thanks!!
This is the code:
private void Command3_Click(object sender, System.EventArgs e)
{
Response.ContentType="text/plain";
Response.AddHeader( "content-disposition","attachment; filename=Testa1.txt");
FileStream sourceFile = new FileStrea(@"C:\Inetpub\wwwroot\FitxIdatziWeb\Testa1.txt", FileMode.Open);
long FileSize;
FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();
Response.BinaryWrite(getContent);
}
|
|
|
|
|
I am fairly new to programing and am progaming in C#. I am very motivated to create code that is lean. By that I mean not repeating code and creating seperate classes when I need to. I thought that I would pick the brains of the people here at 'The Code Project' and get there opinions on when to create your own classes, and get some general programming formats. Thanks ahead of time. I would love to be enlightend.
Jeff St. Germain, MCP
A fool is a person who doesn't learn from others experience.
--Me
|
|
|
|
|
I recommend you simply start developing your main application. As you discover generic classes/methods that you use (or will use) in multiple places, extract them out to your utility DLL. If these classes belong to a particular area (web, windows, gui, networking), you may consider adding them to a more specific utility DLL.
That's the quickest and easiest way to build up your library of reusable classes.
Regards,
Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
|
|
|
|
|