|
Ok I see. Well, if you want to use regexps, the easiest way I can think of is something like:
string HttpText = "HTTP/1.1 200 OK\r\nDate: Fri, 07 Aug 2009 18:10:29 GMT\r\nServer: Apache
/2.0.63 (Unix) mod_ssl/2.0.63 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimi
ted/1.4 FrontPage/5.0.2.2635 PHP/5.2.8\r\nX-Powered-By: PHP/5.2.8\r\nContent-Length: 140\r\
nConnection: close\r\nContent-Type: text/html\r\n\r\nA Word a Moment\r\nWord : somber\r\n\r
\nPron : sombu(r)\r\n\r\nMeaning :\r\n1. Lacking brightness or colour; dull\r\n\r\nExample
: (empty)\r\n\r\n";
Regex MyRegex = new Regex("\r\n\r\n");
String[] MyStrings = MyRegex.Split(HttpText, 2);
if (MyStrings.Length != 2)
{
throw new ApplicationException("Error parsing HTTP response!");
}
Console.WriteLine("Header:");
Console.WriteLine("-------");
Console.WriteLine(MyStrings[0]);
Console.WriteLine("Body:");
Console.WriteLine("-------");
Console.WriteLine(MyStrings[1]);
Console.WriteLine("-------");
But it doesn't make much sense to use the Regex object here, since the pattern separating header from body is a fixed string ("\r\n\r\n"), so the same can be done using String.Split() , which is simpler and more performing.
If you want to use Regex.Replace() , like in your example, then you will have to limit the number of matches to replace to one, otherwise ALL matches will be replaced with the empty string! See the docs here[^].
Regarding how to read data from a socket, if you check the docs for objects in System.Net.Sockets , like TcpClient[^], you'll see they expose methods to obtain a NetworkStream[^], which you can use to read data.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
Excellent. thank you for your needful reply. the tcpclient will help for now and also i will tinger with the others. i had a quick view of your links and i have to spend more time on that.
Today's Beautiful Moments are
Tomorrow's Beautiful Memories
|
|
|
|
|
My pleasure, good luck.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
Hello,
I am creating a GUID and then using that ID inside a SQL statement. I get an error saying that incorrect syntax near '-' , and I believe its referring to the dashes that are created with the GUID. So i was going to split up the GUID and just make it a string of characters. Can anyone help me do this? BElow is a little bit of code to show how I'm using it. Thanks for your help!
SqlConnection sqlConn = new SqlConnection("Data Source=localhost;Initial Catalog=Cost_Model;Integrated Security=True");
sqlConn.Open();
System.Guid UID = System.Guid.NewGuid();
Int16 SCENARIOID = 1;
SqlCommand sqlComm1 = new SqlCommand(
"INSERT INTO web_far15 select " + UID + ", * from fn_far15(" + SCENARIOID + ")",
sqlConn);
SqlDataReader r = sqlComm1.ExecuteReader(CommandBehavior.CloseConnection);
Session["TableName"] = "web_far15";
Session["SessionID"] = UID;
r.Close();
Thanks again for your help!
|
|
|
|
|
Ummm... what? If you provide the GUID as a string SQL Server will parse it.
SELECT * FROM table WHERE ID='blah-blah-blah...'
I don't know whether or not other databases will.
But you'd have an even easier time using a parameterized statement.
|
|
|
|
|
All I did to get the GUID was
System.Guid UID = System.Guid.NewGuid();<code> which created the code.<br />
<br />
When I try to pass the GUID directly it does not allow it, I get the '-' error explained above. Its also not letting me typecast it to a string.
|
|
|
|
|
Parse it with what? Why do you need to parse it?
System.Guid has a constructor that will do it. SQL Server will do it. But you shouldn't need to do it at all.
|
|
|
|
|
I believe I need to parse it so I can take out the dashes. When I run the code, I get the error "Incorrect syntax near '-'." and it breaks at SqlDataReader r = sqlComm1.ExecuteReader(CommandBehavior.CloseConnection);
So when I read that, that sounds like I need to take out the dashes. Am I incorrect?
|
|
|
|
|
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.
|
|
|
|