|
Heath Stewart wrote:
Unfortunately, you'll still need a valid CultureInfo
Can't have everything!
Thanks for your help.
"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
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
|
|
|
|
|
Hello!
This is my first post. I have a little problem. I have build a small Windows Form application. It has 1 form and a couple of buttons and textboxes and a datagrid. I want to build a simple search. So, if I type a letter "D" in a textbox, I want in a DataGrid a list of all names that starts with a "D". If I type further a letter "a" and "v" for example, I would get a "Dave" names for example.
This is what I programmed so far:
static void FilterData()<br />
{<br />
<br />
try<br />
{<br />
<br />
string strFilter = "SELECT Avtosalon.Ime," + <br />
"Naslov.Ulica, Naslov.PostnaSt, Naslov.Kraj, Avtosalon.Telefon," + <br />
"Avtosalon.EPosta, Avtosalon.Vodja, Avtosalon.OdpiralniCas " +<br />
"FROM Avtosalon INNER JOIN Naslov ON Avtosalon.IDNaslov = Naslov.IDNaslov ";<br />
<br />
if (!MyForm.txtIme.Focused)<br />
{<br />
strFilter = strFilter + "WHERE Avtosalon.Ime LIKE '" + MyForm.txtIme.Text + "%'";<br />
if (MyForm.txtUlica.Focused)<br />
strFilter = strFilter + " AND Naslov.Ulica LIKE '" + MyForm.txtUlica.Text + "%'";<br />
else if (MyForm.txtPostnaSt.Focused)<br />
strFilter = strFilter + " AND Naslov.PostnaSt LIKE '" + MyForm.txtPostnaSt.Text + "%'";<br />
else if (MyForm.txtKraj.Focused)<br />
strFilter = strFilter + " AND Naslov.Kraj LIKE '" + MyForm.txtKraj.Text + "%'";<br />
else if (MyForm.txtTelefon.Focused)<br />
strFilter = strFilter + " AND Avtosalon.Telefon LIKE '" + MyForm.txtTelefon.Text + "%'";<br />
else if (MyForm.txtEPosta.Focused)<br />
strFilter = strFilter + " AND Avtosalon.EPosta LIKE '" + MyForm.txtEPosta.Text + "%'";<br />
else if (MyForm.txtVodja.Focused)<br />
strFilter = strFilter + " AND Avtosalon.Vodja LIKE '" + MyForm.txtVodja.Text + "%'";<br />
else if (MyForm.txtOdpiralniCas.Focused)<br />
strFilter = strFilter + " AND Avtosalon.OdpiralniCas LIKE '" + MyForm.txtOdpiralniCas.Text + "%'";<br />
} <br />
<br />
MyDataSet = new DataSet();<br />
MyDataAdapter = new SqlDataAdapter(strFilter,conn); <br />
MyDataAdapter.Fill(MyDataSet,"Avtosalon"); <br />
MyDataAdapter.Dispose();<br />
MyForm.BeginInvoke(CallDataBindToDataGrid);<br />
strFilter = null;<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show(ex.Message.ToString());<br />
}<br />
<br />
}<br />
<br />
<br />
<br />
static void DataBindToDataGrid()<br />
{<br />
<br />
MyForm.gridAvtosalon.DataMember = "Avtosalon"; <br />
MyForm.gridAvtosalon.DataSource = MyDataSet;<br />
MyDataSet = null;<br />
MyDataAdapter = null;<br />
}
private void txtIme_TextChanged(object sender, System.EventArgs e)<br />
{<br />
try<br />
{<br />
UpdateThread = new Thread(UpdateThreadStart);<br />
UpdateThread.Name = "Update Thread";<br />
UpdateThread.IsBackground = true;<br />
UpdateThread.Start();<br />
<br />
<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show(ex.Message.ToString());<br />
}<br />
<br />
}
So, the problem is, when I type in a textbox, I get an error message "There is an open DataReader associated with this
Connection wich must be closed first" and I don't use DataReader at all. Could you help me, couse I study this now for
days and I can't see where is a problem. Thank in advance.
|
|
|
|
|
There are several problems here. With every character you type, TextChanged is fired. Since your updating your DataSet in the background using the same SqlConnection , then all those threads are trying to open separate connections to the same resource (the SqlConnection instance) at once. Even using separate connection resources isn't a good way, because this code is grossly inefficient (with each character typed, the database is queried). Instead, either use a button that filters the results when clicked, or use the LostFocus event or something for a TextBox .
Also, you're relying on input from the user to fill the WHERE clause. This is extremely bad. Think of what would happen if I typed the following into a TextBox :
A';DELETE FROM Avtosalon WHERE Ime LIKE ' That would delete all records in your Actosalon table - and even worse is possible if your DB security isn't tight (I could drop the master table, for example). The important thing when writing software is NEVER TRUST USER INPUT - no matter who the user is (they could inadvertently do something bad). And don't think they won't know your database structure: I could use the IL Disassembler (ildasm.exe) that comes with the SDK (free) or some other disassembler/decompiler and easily find out how to attack your code and database.
Instead, do what you're supposed to use with ADO.NET: parameterized queries. Read about the SqlParameter class and the SqlCommand.Parameters property in the .NET Framework SDK for lots of information and example source code. Using this not only makes for easier to read code, but eliminates the need for you to make sure strings are escaped (which is one thing that allows me to type what I did above) and it automatically performs checks for common attacks, this decreasing the attack surface.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hello every one,
can i integrate my Menu with Windows Shell ContextMenu (the one which pops up whenever we right click mouse) using C# classes.
Problem is, I want to do it without using Interop (Com) simply because i dont have pre-requiste COM knowledge.
suggestion of ne kind are welcomed
thanx.
|
|
|
|
|
goooooooogle wrote:
Problem is, I want to do it without using Interop (Com) simply because i dont have pre-requiste COM knowledge. suggestion of ne kind are welcomed
If you don't want to learn, why are you bothering asking. There are a whole slew of articles here on both COM Interop and Shell programming within C#. I would suggest you read those and when you get stuck either check out MSDN[^], post a question to the articles author on the forum within the article or post a question here.
- Nick Parker My Blog | My Articles
|
|
|
|
|
goooooooogle wrote:
Problem is, I want to do it without using Interop (Com) simply because i dont have pre-requiste COM knowledge
Impossible. Windows is still COM based so either you do it in COM or you don't do it all.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
How would I go about changing the number of spaces a tab uses when the user presses Tab on his/her keyboard? Thanks in advance.
Happy Programming and may God Bless!
"Your coding practices might be buggy, but your code is always right."
Internet::WWW::CodeProject::bneacetp
|
|
|
|
|
From my experience, I dont think u can, I digged alot thru the Win API but couldnt find anything...
top secret xacc-ide 0.0.1
|
|
|
|
|
See the documentation[^] for the RichTextBox.SelectionTabs property. This is the same as sending the EM_SETPARAFORMAT with a PARAFORMAT2 structure in native code with the cTabCount and rgzTabs fields assigned.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for the help.
Happy Programming and may God Bless!
"Your coding practices might be buggy, but your code is always right."
Internet::WWW::CodeProject::bneacetp
|
|
|
|
|
Hi All,
We want to use sql server reporting services (ssrs), but want to create our own viewer and print buttons. Does anyone know of an article that shows how to do this. We really need to create a dll that can be called that will print our reports. Any info would be appreciated.
later,
Clyde
|
|
|
|
|
Here is task at hand:
I need to create a help system that allows users to hit F1 on any field, and they will get a popup explaining the use of the field. Assume that I know how to capture the keyboard event. I have a seperate class that has all functionailty of the HtmlHelp API in it. I need to call the HH_DISPLAY_TEXT_POPUP command. which takes the following structure for input: HH_POPUP as well as the location of the .chm file. I am getting confused as to how I define a structure to pass into this function. I do not have much experience with COM. The reason we are using this HtmlHelp API instead of the System.Windows.Forms.Help definition is becasue we want to read directly from our already created .chm file. If there is a way to do this with .net controls, please let me know as well. Thanks in advance!
|
|
|
|
|
Dear, Sir and Madam
Please see the code below.
bool bCtrl = false;
private void treeView1_KeyDown(object sender, KeyEventArgs e)
{
bCtrl = e.Control;
this.Text= bCtrl.ToString();
}
private void treeView1_KeyUp(object sender, KeyEventArgs e)
{
bCtrl = e.Control;
this.Text= bCtrl.ToString();
}
private void treeView1_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
if(bCtrl)
DoDragDrop(e.Item, DragDropEffects.Copy);
else
DoDragDrop(e.Item, DragDropEffects.Move);
}
While I dragging item(node) why the KeyDown event is not fire?
How to solve this problem?
Sorry for bad English.
Thank You.
|
|
|
|
|
|
It's not supposed to. If you want to detect if the CTRL key was pressed while dragging, handle the Control.QueryContinueDrag event. See the documentation for that event as well as the QueryContinueDragEventArgs in the .NET Framework SDK for more information and examples.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have a class in my middle tier in which I hold a DataTable object, which has already been filled out with data. All I want to do is filter the DataRows based on one of the Fields in the DataRow, and return a DataTable object with the filtered Rows.
This is my code:
public DataTable FilteredData( int clientType )<br />
{<br />
string filter = "CLIENT_TYPE = " + clientType;<br />
DataRow[] foundRows = _myData.Select( filter );<br />
DataTable dt = new DataTable();<br />
foreach( DataRow row in foundRows )<br />
{<br />
dt.Rows.Add( row );<br />
}<br />
return dt;<br />
}
The problem is that when it reaches the line dt.Rows.Add( row ); I get an exception saying that "This row already belongs to another table". It doesn't matter that I will be returning a seperate DataTable, as the Rows won't be updated in the interface. Can someone please tell me how I can return my data in a DataTable object, I would rather not have to return an array of DataRows.
|
|
|
|
|
Why don't you use DataView instead of DataTable?
You won't have to duplicate the data.
With your approach, you have to clone each row as DataRow can only belong to one datatable.
|
|
|
|
|
I would recommend you don't disconnect the DataRow from its DataTable . There is a lot of other information in there, such as change information - not to mention that the column definitions are part of the DataTable (which you didn't replicated; FWIW, the easiest way to do that is DataTable.Clone , which replicates only the structure).
An array of DataRow s is still bindable, as well. An array implements IList so that you can bind it to a DataTable , ComboBox , or any other control. In this case, it's much better to bind against a DataView instantiated with the DataTable as the ctor parameter, then use the RowFilter property. This will track all changes.
If you don't need binding or change tracking and just want a simple list of DataRow s, then you'll either have to cope with the array, remove them from the old table and add them to the new table (and use a reverse for loop - changing the enumerable will throw an exception), or use Array.CopyTo (inheritted from your DataRow[] ) which will perform a deep copy (a shallow copy won't solve your problem, since it only copies a reference to the original (same) object).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for all your help. I have decided to continue with using DataTable objects as the interface between the DataLayer and the Business layer and to now use DataView objects as the interface between the BusinessLayer and the GUI.
|
|
|
|
|
I must profess my config file ignorance. I'm required to use an open source logging project. The project deposits logs into text files based on a path attribute in my application config file. I would really like to dynamically set that path, say to, the application path. Can you use functions in a config file, or do I have to just do it manually?
Thanks,
Ryan
|
|
|
|
|
If you want the application path, simply use Application.StartupPath . Don't force a user to put the path of the executable into the .config file in that very path - it's redundent, requires extra steps (always faulty), and is easy to get from code (even with native executables).
If you want simple string settings, see ConfigurationSettings.AppSettings in the .NET Framework SDK, which also includes examples.
If you want to make your own configuration section, implement IConfigurationSectionHandler , which is documented in the .NET Framework SDK as well and includes samples. There are also several articles here on CodeProject that discuss section handlers that you should search for.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hey Heath,
As I stated in my orginial post, I am forced to use an open source project that requires me to use the .config file. Is there a way to use COnfigurationSettings.AppSettings in the .config file?
Thanks,
Ryan
|
|
|
|
|
What does that have to do with open source? ConfigurationSettings.AppSettings and Application.StartupPath should both be available for any project that is run against an implement of the CLI (.NET, Mono, and Portable.NET). Read about the <appSettings> section in the .NET Framework SDK.
Open source just means the source is available for anyone to use and modify according to the license agreement. What language, framework, platform, etc. is not a factor of whether or not something is called "open source". Technically, every project posted on this site that provides source code is open source.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
you only store data in the config file. There isn't any environment variable type of mapping unless the library provides it.
You probably have to set the logging directory programatically whenever the application directory changes.
I can imagine the sinking feeling one would have after ordering my book,
only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon
|
|
|
|
|
Thanks Andy, thats what I needed to know. I wasn't sure if there might be some sort of environment variables.
Ryan
|
|
|
|