|
foreach (DataRow row in DS.Tables[0])
{
x1.Text = row["test"].ToString();
}
|
|
|
|
|
shouldn't that be
DS.Tables[0].Rows
??
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Yes... I did that the other day too
|
|
|
|
|
Personally I would not do the "upgrade" in the first place as your code will actually run slower. Unless you have a compelling reason to do so rather do the following:
int rowCount = DSData.Tables[0].Rows.Count;
for (int x = 0; x < rowCount ; x++)
{
x1.Text = DSData.Tables[0].Rows[x]["test"].ToString();
}
|
|
|
|
|
My test says otherwise:
DataTable dt = new DataTable("table1");
DataColumn dc = new DataColumn("col1");
dt.Columns.Add(dc);
for (int i = 1; i < 1000000; i++)
{
DataRow row = dt.NewRow();
row["col1"] = "string" + i.ToString();
dt.Rows.Add(row);
}
long start = DateTime.Now.Ticks;
for (int i = 0; i < dt.Rows.Count; i++)
{
string s = (string)dt.Rows[i]["col1"];
}
long middle = DateTime.Now.Ticks;
foreach (DataRow r in dt.Rows)
{
string s = (string)r["col1"];
}
long end = DateTime.Now.Ticks;
TimeSpan forLength = new TimeSpan(middle - start);
TimeSpan foreachLength = new TimeSpan(end - middle);
Console.WriteLine("for = " + forLength.ToString());
Console.WriteLine("foreach = " + foreachLength.ToString());<pre>
Output:
<code>for = 00:00:00.9079668
foreach = 00:00:00.2661282</code>
But in the scheme of things, less than a second to process 1000000 records, either way fine by me.
|
|
|
|
|
I stand corrected then
I was going according to the MSDN Documentation on enumeration overhead. Thanks for the info.
|
|
|
|
|
I would suggest you enclose all of that in another for loop that performs 2 or more
passes, just to get rid of some effects such as data cache hits.
Any first pass measurement typically is unreliable.
Luc Pattyn
|
|
|
|
|
Good idea:
pass 1:
for = 00:00:00.0312510
foreach = 00:00:00.0156255
pass 2:
for = 00:00:00.0625020
foreach = 00:00:00.0312510
pass 3:
for = 00:00:00.0937530
foreach = 00:00:00.0312510
pass 4:
for = 00:00:00.1562550
foreach = 00:00:00.0468765
|
|
|
|
|
Well, even these measurements are not trustworthy,
your system timer seems to tick in increments of 15.6 msec
so that is also the margin of error.
I now suggest you redo it in this way:
int maxIter=100;
for (int pass=0; pass<4; pass++) {
start timer (or read DateTime.Now.Milliseconds)
for (int iter=0; iter<maxIter; iter++) {
code to be timed
}
stop timer
start timer
for (int iter=0; iter<maxIter; iter++) {
alternative code to be timed
}
stop timer
}
Luc Pattyn
|
|
|
|
|
Just for fun I did the test 1000 times and got the following results:
for: 00:12:08.2600974
foreach: 00:03:40.6635750
Note that this is total time for 1000 runs; the result seems clear though.
Internet - the worlds biggest dictionary
|
|
|
|
|
go with foreach !
Luc Pattyn
|
|
|
|
|
Hello,
andre_swnpl wrote: as your code will actually run slower
Why do you think that?
andre_swnpl wrote: Rows[x]"
If you really safe time with the for instead of the foreach, than you will loose it here again, I think!
All the best,
Martin
|
|
|
|
|
hello
my question is that i'm getting a page HTML by my code.what i want....to make the thumbnail image of this HTML display like we do by "PRINTSCREEN" button.
can i do this?
thanx
Dotnet
|
|
|
|
|
Hi,
Im writing a very simple network chat app. This is only a chat app for two people over network.
Here is my code:
<br />
public partial class MyChat : Form<br />
{<br />
Socket sock;<br />
Thread receiver;<br />
static IPAddress localIPAddr;<br />
static int intPort;<br />
IPEndPoint multiep;<br />
<br />
public MyChat()<br />
{<br />
InitializeComponent();<br />
<br />
localIPAddr = IPAddress.Parse(txtHost.Text);<br />
intPort = Convert.ToInt32(txtPort.Text.ToString());<br />
<br />
multiep = new IPEndPoint(localIPAddr, 4040);<br />
sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); <br />
<br />
IPEndPoint iep = new IPEndPoint(IPAddress.Any, intPort);<br />
sock.Bind(iep);<br />
<br />
sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(localIPAddr));<br />
<br />
receiver = new Thread(new ThreadStart(packetReceive));<br />
receiver.IsBackground = true;<br />
receiver.Start(); <br />
}<br />
<br />
void packetReceive()<br />
{<br />
EndPoint ep = (EndPoint)multiep;<br />
byte[] data = new byte[1024];<br />
string stringData;<br />
int recv;<br />
while (true)<br />
{<br />
recv = sock.ReceiveFrom(data, ref ep);<br />
stringData = Encoding.ASCII.GetString(data, 0, recv);<br />
rtxtMsgWindow.AppendText("from " + ep.ToString() + ": " + stringData);<br />
}<br />
}<br />
<br />
private void btnExit_Click(object sender, EventArgs e)<br />
{<br />
receiver.Abort();<br />
sock.Close();<br />
Close(); <br />
} <br />
<br />
private void txtMessage_KeyPress(object sender, KeyPressEventArgs e)<br />
{<br />
if (e.Handled = e.KeyChar == 13)<br />
{<br />
byte[] message = Encoding.ASCII.GetBytes(txtMessage.Text);<br />
txtMessage.Clear();<br />
sock.SendTo(message, SocketFlags.None, multiep);<br />
}<br />
}<br />
}<br />
<br />
Problem is i get the following socket exception The requested address is not valid in its context at the line of code i made bold above.
The specified ip address would be my own ip address to test and see if the message gets posted. If this is the problem is there any other way i could get around this maybe a different way of socket programming maybe using tcpclient or something?
Im a noob to sockets or network programming pls help thx
Regards
|
|
|
|
|
Hi.
I'm writing a client for a webservice which is built on Java. Some operations provided by the service send attachments to the client calling them. The standard used by the server for this task is MIME(SOAP with Attachments). As some of you might know, the .NET Framework and WSE don't have(and never will have) any support for SwA/MIME.
Does anyone has knoleddge of the existance of a good free/afordable component for .NET which can handle MIME responses from webservices? I only found one such beast, written by Alotsoft, but it cost $5000...
Thanks Alot.
|
|
|
|
|
|
Which event should be handled before closing form, so closing can be canceled. Now I handle OnClosing and OnClosed but returning from these on Cancel, closes the form anyway.
|
|
|
|
|
use FormClosing or FormClosed event instead. and use e.Cancel=true;
|
|
|
|
|
in the "closing" event of that form put following line
e.cancel = true;
form closing will be cancelled.
Rahul Kulkarni
|
|
|
|
|
|
Application in c# that opens a word file.
The Application included a reference to the Microsoft Word 11.0 Object Library and Microsoft Office 11.0 Object Library.
On computer with word 2003 the application runs ok but on computer with word 2000 got error message:
"Microsoft.Office.Interop.Word
Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Can anyone suggest a solution?
Is there backwards compatibility with the interop assemblies?
Thanks
|
|
|
|
|
AFAIK there is no backwards compatibility.
For Office 2003 its the Microsoft Office 11.0 Object Library
For Office 2002 its the Microsoft Office 10.0 Object Library
and for Office 2000 its Microsoft Office 9.0 Object Library (I think)
|
|
|
|
|
I need to open the word document in all word versions
How can I do that?
|
|
|
|
|
No backwatrd compatibility means you can only open one specific version of Word with a given dll. However, these programs themselves contain backward compatibility, so I recommend to use dll version 11.0 (Word 2003), that way you can open earlier version doc files.
|
|
|
|
|
The application has to support all word versions.
|
|
|
|