|
0) Put the GUID in apostrophes if you insist on writing poor SQL.
1) Use a parameterized query to write good SQL.
|
|
|
|
|
I am trying to retrieve the contents of multiple textboxes using a for loop as follows:
for (int x = 1; x < 6; x++)
{
string vDate = "txtDate" + x + ".Text"; // vDate = txtDate1.Text on 1st iteration
string vFund = "txtFund" + x + ".Text";
string outDate = vDate; // this should = txtDate1.Text on 1st iteration
string outFund = vFund;
}
When I run the code outDate = txtDate1.Text and outFund = txtFund1.Text. How do I set variable to equal the text contents of the textbox? Any help would be appreciated.
Thanks
|
|
|
|
|
Instead of just outputting C# code in text file better leverage C# ability to write code that in turn output code for you. You can then execute that outputted code. I am taking about code dom feature of C#
|
|
|
|
|
private void DoSomethingWithATextBox(TextBox textBox){
}
...
DoSomethingWithATextBox(txtDate1);
DoSomethingWithATextBox(txtDate2);
DoSomethingWithATextBox(txtDate3);
DoSomethingWithATextBox(txtDate4);
DoSomethingWithATextBox(txtDate5);
DoSomethingWithATextBox(txtDate6);
Much easier than doing the loop method which will be dependent on proper code generation. Also, the loop method is available in ASP.NET but I still don't like it. My method is much cleaner and easier to maintain.
|
|
|
|
|
Hi,
two easy ways:
1.
use a List<TextBox> textboxes to which you add the textboxes of interest, then a simple foreach(TextBox tb in textboxes) will do the job.
2.
if you want to enumerate ALL the textboxes of a Form, and they all are at the top level of that form, you could do:
foreach (Control c in this.Controls) {
TextBox tb=c as TextBox;
if (tb!=null) { ... do something to tb ... }
}
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Anyone got a good idea as to how I can monitor the number of times my application is actively running on various PCs across a network. I want to licence my product based on concurrent usage, therefore allow it to be installed on any number of PCs on a newtwork, but perhaps only allow ten instances to be running/accessed at the same time. My app does not use a database, therefore preventing me monitoring the number of logged in connections. I suppose I could place a file in a shared location, but would rely on the application have the rights to update the file, and not making it obvious to users that this file monitors the number of active instances. Neither can I guarentee the network admin will allow a socket server type application to be installed allowing each program to connect as a client.
The reason for this query is that I'm not too keen on having to enforce users or company to register a product key based on the PC's MAC address (or something simplar) for each PC it's installed on, but if this is the best approach, then this is the approach I'll take.
Any ideas on a possible way forward??
Andy
|
|
|
|
|
Run a licensing server. Require each allowable instance to have a key (either Software Key or a FOB) and then check it intermittently in code. Disconnect the oldest client just like your ISP will do. (It was fun back in the day to set my own IP address and boot people off Earthlink)
|
|
|
|
|
Hello there,
I've got a typical Form called Form1 with some UserControls including a DataGridView (called dgView) inside a TabControl.
The dgViews are databound to datatables which for themselves can alter their values.
The dgViews always and to every time should show the content of these datatables. So I wrote the methods ActualizeView1() and ActualizeView2() which puts the rows inside the dgViews into different colors. I call these methods each time these lines have to be redrawn.
I don't know which funktion does this or why but I know that after c'tor Form1() did its job view1_Load(...) is called before Form1_Load(...)
view2_Load(...) is only called when I click on the tabPage2 so the contents inside are to be drawn.
So I called ActualizeView1() out of view1_Load(...) and ActualizeView2() out of view2_Load(...)
The problem still is: When I click on the tabPage2, the contents inside view2 do not have the color that they should. Every row in the dgViews must have a color (NOT black), but they are ALL black - like I only would have bound the datatables but not given them colors.
When I ActualizeView2() again the colors are correct.
So the question is: When do I have to call my method ActualizeView() in order to get the proper colors? Or is there s.th. that puts the rows back in black from view2_Load(...) or from elsewhere?
Thanks in advance
Harry
no plan,
no signature
|
|
|
|
|
You could handle the TabControl.SelectedIndexChanged event something like this:
private void tc_SelectedIndexChanged(object sender, System.EventArgs e)
{
TabControl tc = (TabControl)sender;
if (tc.SelectedTab == view1Tab)
{
ActualizeView1();
}
else if (tc.SelectedTab == view2Tab)
{
ActualizeView2();
}
etc.
}
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Thanks, but unfortunately it was not helpful.
It doesn't matter if I call ActualizeView() out of TabControl_IndexChanged() or out of MyList_Load().
I see the debugger running into that code. I also see it run over the lines, that look like
this.myList.dgView.Rows[index].DefaultCellStyle.ForeColor = Color.Red;
But after the method terminates the contents are there, but every line is drawn black.
btw: This problem disappears when I draw myList again after the tabControl changed for the first time. But I want to fix it without having to draw again.
Greets
Harry
no plan,
no signature
|
|
|
|
|
|
Hi,
I am having 50 MB(Approx. after counting from editplus editor) of data's in a stringbuilder and when i am converting in to Stream it was throwing error as "System.OutOfMemoryException was thrown".
The code below:
StringBuilder strCmdBuild; // Holds the 50 MB values;
// Converting Stringbuilder to Stream.
Stream stCommand = new MemoryStream (ASCIIEncoding.Default.GetBytes (strCmdBuild.ToString()));
// Here I am using the StreamReader to read..
StreamReader objReader = new StreamReader (stCommand);
And loop through till all values getting read..
string str = objReader.ReadLine ();
Mainly the error("System.OutOfMemoryException") throws before the completion of reading...
How to rectify this error?
With reg,
Subbu
|
|
|
|
|
Can you post the full code?
|
|
|
|
|
Thanks a lot...I gave sample values of strTemp and it has so many SET_VALUE 01 and other values and Adding the values in to CmdBuilder StringBuilder and return it to called function.
strTemp (it holds 50 MB of datas) = \r\n<START_COMMAND>\r\nSET_VALUE 10\t >W\r\nSET_VALUE 11\t >W\r\nSET_VALUE 10\t >W\r\nSET_VALUE 10\t >W\r\n.............
..................SET_VALUE 11\t >W\r\nSET_VALUE 10\t >W\r\nSET_VALUE 10\t >W\r\nSET_VALUE 10\t >W\r\nRESP_BLOCK\r\nDo_DR_END \r\n\r\n\r\n<END_COMMAND>
------------------------------------------------------------------
// Code Starts here just copy and paste it in notepad and view it...
static public StringBuilder GetCommand(StringBuilder strTemp) // string strTemp
{
StringBuilder CmdBuilder = new StringBuilder ();
StringBuilder CmdLine = new StringBuilder ();
try
{
int TDILength = 0;
if (strTemp.ToString().Trim() != "")
{
Stream stCommand = new MemoryStream (ASCIIEncoding.Default.GetBytes (strTemp.ToString()));
StreamReader objReader = new StreamReader (stCommand);
//strTemp.ToString() = ""; //strTemp
//RESP_BLOCK
while(true)
{
string str = objReader.ReadLine ();
if (str == null)
break;
if (str.IndexOf (">W") > 0 )
{
if (str != "")
{
str = str.Replace(" >W","");
//CmdBuilder.Append(str.Trim () + "\r\n");
CmdLine.Append(str.Trim () + "\r\n");
}
TDILength++;
}
else if (str.Trim () == "RESP_BLOCK")
{
if (CheckForMaximumSize (TDILength)) // Check Whether we have reached max. size or not
{
TDILength = 0;
CmdBuilder.Append("<END_COMMAND>\r\n"); // If we reached say END_VALUE -> to issue BULK TRANSFER
CmdBuilder.Append("RESET\r\n");
CmdBuilder.Append("<START_COMMAND>\r\n"); // Start-up new Block
}
CmdBuilder.Append(CmdLine.ToString ());
CmdLine = new StringBuilder ();
}
else if (str.Trim () == "RESP_BLOCK_START")
{
while(true)
{
str = objReader.ReadLine ();
if (str.IndexOf (">W") > 0 )
{
if (str != "")
{
str = str.Replace(" >W","");
CmdLine.Append(str.Trim () + "\r\n");
}
TDILength++;
}
else if ( str =="RESP_BLOCK_END")
{
if (CheckForMaximumSize (TDILength)) // Check Whether we have reached max. size or not
{
TDILength = 0;
CmdBuilder.Append("<END_COMMAND>\r\n"); // If we reached say END -> to issue TRANSFER
CmdBuilder.Append("FIFO_RESET\r\n");
CmdBuilder.Append("<START_COMMAND>\r\n"); // Start-up new Block
}
CmdBuilder.Append(CmdLine.ToString ());
CmdLine = new StringBuilder ();
break;
}
}
}
else if (str.IndexOf ("WAIT") == 0)
CmdBuilder.Append(str.Trim () + "\r\n");
if (str.Trim () == "<START_COMMAND>")
CmdBuilder.Append(str + "\r\n");
if (str.Trim () == "<END_COMMAND>")
CmdBuilder.Append(str + "\r\n");
if (str.Trim () == "START ")
{
while(true)
{
str = objReader.ReadLine ();
if (str.Trim () == "END " || str == null)
break;
else if (str.Trim () != "")
CmdBuilder.Append(str + "\r\n");
}
}
}
objReader.Close ();
stCommand.Close ();
}
else
throw new Exception ("There is no Commands to Transfer....");
}
catch(Exception objException)
{
//throw objException;
MessageBox.Show(objException.Message);
}
if (CmdBuilder != null)
return CmdBuilder.Replace(">W","");
else
return null;
}
Thanks,
Subbu
|
|
|
|
|
Delete all of your code and don't use a memory stream or any streams and break your logic out into different methods. You may be surprised at how quickly trying a different approach from scratch works. That is what the experienced dev's do.
|
|
|
|
|
Did you know you can use strings in a switch ?
|
|
|
|
|
Poor form, I always use String.Compare instead of a switch.
I also believe strings are over and misused in modern programming.
|
|
|
|
|
Ennis Ray Lynch, Jr. wrote:
Poor form
Opinion
I don't like huge if/else if/else if/else if/else if/else if blocks (and then there is the O(n) time complexity of it..)
|
|
|
|
|
I believe the complexity of both is O(1) and you may be confusing a run time estimate with complexity.
Yes it is my opinion, but then again I like not having to know about the culture when doing string comparisons, http://msdn.microsoft.com/en-us/library/hyxc48dt.aspx[^]
But you are right, it is just my opinion and both will run fine, and in .NET, the switch will be faster.
|
|
|
|
|
Actually they are both O(k) (*), strange eh? They thought it would be a good idea to make a dictionary that maps all the strings in the cases to integers, and then switch on that. And that dictionary, is a local variable! Argh! So it's remade every time, so that'll be k Dictionary.Add's every time.. great..
The if/else if chain is trivially O(k), there are k cases (definition of k) and if no match is found at all then all cases were tried so k string comparisons (which aren't O(1) by themselves, unless both strings are interned).
Both ways make no sense.
1) There is a total order over strings, so a tree-based search could be used.
2) That "temporary" dictionary should not be temporary, so the cost of adding the entries can be amortized.
Anyway, it's true that it's O(1) if you look at the resulting exe/dll, but you can change the number of cases in the source so it's really a "variable" after all
|
|
|
|
|
Removing the loop removes the n, which does not seem logical except that Big Oh is not a measure of complexity and not running time.
for(int i=0;i<n;i++){
SomeAction(i);
}
is O(n)
but
SomeAction(0);
SomeAction(1);
...
SomeAction(n-1);
is O(1)
|
|
|
|
|
I really have to disagree. You have something of non-constant length n, so you can't simplify the n away.
It's really just a special case of unrolling
And this n doesn't suddenly become constant just because you said "ok so let's not change it anymore" - that's like saying sorting an array is constant time for any given array
|
|
|
|
|
In analyzing the complexity of algorithms you need to examine the code as written, not the compiler output. While I try to be forgiving in the matter of opinion this is a matter of fact. I am not going to get pedantic on the matter and I am not going to try and prove why this is the case. You don't have to believe me but I would suggest doing a lot more research in the matter before deciding to disagree further.
|
|
|
|
|
Come one, be flexible, the number of cases is the real variable here, if you start treating it as a constant (and YES I am very well aware that it actually is a constant) it becomes impossible to say anything interesting about it anymore.
So I say O(k), and k is a constant as usual (it's a k, not an n or m or whatever), but if you do that last simplification to O(1) you just lose the necessary detail without gaining anything.
|
|
|
|
|
And not if you write it like this. You'd have to fix the n. The "..." is not of fixed length now.
edit: yea ok sorry that was just useless. There is a difference between what is right and what is useful. Actually that applies to this whole argument so if you don't mind I'll just quit. Last modified: 31mins after originally posted --
|
|
|
|