|
First, you do not need to open a connection when using a DataAdapter derivative. The DataAdapter will open and close the connection as necessary.
Second, to avoid connecting back to the server multiple times - and for large data sets this could require a tremendous amount of time on the user's part and lots of bandwidth on your server - have SQL Server do the work using joins like the following example:
select o.* from Orders o left outer join Customers c on o.CustomerID = c.CustomerID Third, you can implement relationships in your DataSet between DataTable s using a DataRelation . In Visual Studio, if you add a new DataSet class to your project you can import these tables from Server Explorer (or author them yourself, but this takes more time and is prone to error) then draw relationships between fields from different tables. This creates the DataRelation for you. Be sure to look at the code to see how it's done, or read the documentation[^] for the class for an example.
Your SqlCommand for your SqlDataAdapter then should have two SQL statements in one command to fill each table (separated by a semicolon) or swap the SqlDataAdapter.SelectCommand with second command and fill the same table with a different table mapping (the previous data would be saved).
Then you'd have a DataSet with everything you need already with only a one (or two, if you do the latter suggestion of a second command and calling DataAdapter.Fill again) connection to the server. This would be a much more efficient use of network bandwidth and the database server that is designed to handle this sort of query.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi there,
I am using C#. I have to create a random unique identifier that will be used as a primary key in my table. I created a string array dynamically that contains all upper case letters, and numbers 0-9 like this:
private ArrayList m_arrCharPool;
private string m_sExcludeChars = "IO01";
In the constructor I initialise my array:
m_arrCharPool = new ArrayList();
// Clear the array
m_arrCharPool.Clear();
// If required, include uppercase letters A thru Z
if(m_bIncludeLetters)
{
for(int i = 65; i < 91; ++i)
{
m_arrCharPool.Add(Convert.ToChar(i).ToString());
}
}
// If required, include numbers 0 thru 9
if(m_bIncludeNumbers)
{
for(int i = 48; i < 58; ++i)
{
m_arrCharPool.Add(Convert.ToChar(i).ToString());
}
}
So basically my array looks something like this:
"A", "B", "C".... "7", "8", "9"
No you will see I create a string variable called m_sExcludeChars. This variable represents all the characters that are not allowed in the array. So now I need to loop through the m_arrCharPool array, and search for each character in m_sExcludeChars, and delete that array element totally from the array.
My loop is something like this, but I know it is wrong. Can someone please help me with my loop so that it performs correctly. If I can do this whole process differently, please let me know:
// Remove the following characters from the array: I, O, 0, 1
for(int i = 0; i < m_arrCharPool.Count; ++i)
{
foreach(char j in m_sExcludeChars)
{
if(m_arrCharPool.IndexOf(j))
{
m_arrCharPool.RemoveAt(i);
}
}
}
Regards,
Brendan
|
|
|
|
|
The problem with your loop is that you are looping through an array and deleting elements from it within the loop. Copying the contents into another array, deleting elements from it and then reassigning this copied array to the original array should do the trick.
ArrayList copyOfCharPool = new ArrayList(m_arrCharPool);
for(int i = 0; i < m_arrCharPool.Count; ++i)
{
foreach(char j in m_sExcludeChars)
{
if(copyOfCharPool.IndexOf(j))
{
copyOfCharPool.RemoveAt(i);
}
}
}
m_arrCharPool = copyOfCharPool;
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Thanks for the reply.
Correct me if I am wrong, but it looks almost exactly like my code. What you are doing is to assign my original to a copy array, looping through the copy array, removing the elements, and then assigning the copy array back to the original array. I don't think my way works, but isn't it longer doing it your way? Why can't you do it my way?
|
|
|
|
|
ma se wrote:
looping through the copy array,
No I didn't, I looped through the original array and that's the key to this problem. Like I said in my earlier post, looping through a collection and deleting elements from it at the same time will not work correctly.
int []a = new int[]{1,2,3,4,5,6,7,8,9, 10};
ArrayList arrayList = new ArrayList(a);
for (int i = 0; i<arrayList.Count; ++i)
arrayList.RemoveAt(i);
How many times do you think the loop will run? It won't run 10 times, most probably will run 5 or 6. In fact, if you use the foreach statement instead of for to loop, you'll get an exception stating that you can't modify the collection when enumerating over it.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Thanks
I will try it out at home and get back to you tomorrow.
You sure the loop in the first reply will work 100%? So I will be left with (36-4) elements?
B
|
|
|
|
|
Why not just use a string that contains all available characters?
const string chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
---
b { font-weight: normal; }
|
|
|
|
|
Because there are 2 properties that can be set to either false, or true. So the user can choose if there is just letters, numbers or both. Then it generates the string/array like you mentioned, that is, if he chose numbers and letter.
Must I then still do it this way as you mentioned??
|
|
|
|
|
Then put the letters in one string and numbers in another, and concatenate these according to what the user has chosen.
string chars = string.Empty;<br />
if (m_bIncludeLetters) chars += letters;<br />
if (m_bIncludeNumbers) chars += numbers;
---
b { font-weight: normal; }
|
|
|
|
|
Ok it sounds good enough, but I will still need to loop through it to search for characters not allowed and to remove them. Once I have removed these characters, then from what is left will I create a unique id of about 9 characaters in length.
private string m_sExcludeChars = "IO01";
If the string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
How do I loop through it and remove the characaters that I have defined in m_sExcludeChars? So the final string should look like this after the 4 characters have been removed:
"ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
O yes, if I declare a char array like:
private char[] chars;
How do I append characters to it? I want a similar result to:
private char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray();
I hope this makes sense.
Regards,
Brendan
|
|
|
|
|
Can't you just remove the unwanted characters from the original string constants?
Otherwise they can be removed with some Replace calls, or more efficiently with a regular expression:
chars = RegEx.Replace(chars, "[IO01]", "");
Do you really need a char array? You can get char values from a string in the same way as from a char array:
char myChar = chars[index];
ma se wrote:
How do I append characters to it? I want a similar result to:
private char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray();
What's wrong with that code?
---
b { font-weight: normal; }
|
|
|
|
|
After an installation for a single user all went well, but it I try the same installation for all users the shortcut on users desktop points not to my application (.exe) but starts the .msi again.
If I start my app (.exe) in the users application folder directly all works fine.
Do I have to install additional folders for a All Users desktop and all users program folder ?
Thanks
Ariadne
-- modified at 10:16 Thursday 6th October, 2005
|
|
|
|
|
I rewrote the whole setup-project: Now it works... It seems an internal error.
Then there is probably a minor bug: If I install a project as all users in the Software-install folder the icon is a yellow folder icon. If I install a project as single user the in the setup project selected icon appears.
Ariadne
|
|
|
|
|
Im working with Sockets in C#, and i have everything working with events and some threads, i am accepting multiple connections i just would like to know how can i detect if a client or a server has closed it's socket. Is there an event or is there a thrown exception is there something i can do to detect this when it happens?
Any help is appreciated.
|
|
|
|
|
hi..
i hav created setup of my desktop application using dot net setup project....i want when installation completes it automatically pop up a readme file ..how can ido this..
thanks
|
|
|
|
|
shashank veerkar wrote:
i want when installation completes it automatically pop up a readme file
in the VS-setup project go to the user interface and add in the End-Section a new dialog form: Readme (or something like that, I use a german version of VS there the name of this dialog is "Infodatei")
Hope this helps
Ariadne
|
|
|
|
|
hi..
im developing a fax service to send fax automatically from a system..for this i need to find how many Fax Machines are attached to the system..and how to get list of them..so please help me to solve this problem...
thanks ..
|
|
|
|
|
Why won't the Form see this method?
///////////////////////////////////////////
//Here's a snippet of my Complex2.cs code (seperate Sol'n file)
private Pen [] activePen;
private int _penIndex = -1;
public Complex2()
{
Pen[] activePen = new Pen[] { new Pen( Color.Blue, 2 ), new Pen( Color.Green, 2 ), new Pen(Color.Red, 2) };
activePen[0].StartCap = System.Drawing.Drawing2D.LineCap.Triangle;
// TODO: Add constructor logic here
real = 0;
imag = 0;
}
public Complex2(double _re, double _im)
{
real = _re;
imag = _im;
}
public Pen GetNextPen()
{
_penIndex = (_penIndex + 1) % 3;
activePen[_penIndex].StartCap = System.Drawing.Drawing2D.LineCap.Triangle;
return activePen[_penIndex];
}
//////////////////////////////////////////////
//Here's some relevant code form Form1.cs
protected Complex2 cmplxValue1 = new Complex2(0,0); //these get values changed from TestBoxes...
protected Complex2 cmplxValue2 = new Complex2(0,0);
void DrawComplexNumber1(Graphics g)
{
Pen p = GetNextPen();
c_xy.SetOrigin(xIndent + graphRect.Width/2, yIndent + graphRect.Y/2);
cmplxValue1.Real = Convert.ToDouble(textComplex_1Real.Text);
cmplxValue1.Imaginary = Convert.ToDouble(textComplex_1Imaginary.Text);
g.DrawLine(GetNextPen(), 250, 250, (float)(250 + (xIndent * cmplxValue1.Real)), (float)(250 - (yIndent * cmplxValue1.Imaginary)));
}
ERROR
CForm1.cs(335): The name 'GetNextPen' does not exist in the class or namespace 'ComplexTest2.Form1'
Even if I try this:
g.DrawLine(cmplxValue1.GetNextPen()....) the instantiated object doesn't "see" the method... please help thanks a lot....
|
|
|
|
|
You wrote Anonymous wrote:
Pen p = GetNextPen(); this will generate
The name 'GetNextPen' does not exist in the class or namespace 'ComplexTest2.Form1' error because the compiler will think GetNextPen() member of Form and this not true
because it is a member of complex2 and you didn't call it from an object of this class
Anonymous wrote:
Even if I try this:
g.DrawLine(cmplxValue1.GetNextPen()....) the instantiated object doesn't "see" the method... please help thanks a lot....
are you replaced all places you call this function or gust this line? you need to replace all the places like
Pen p = GetNextPen(); too
MCAD
-- modified at 1:01 Thursday 6th October, 2005
|
|
|
|
|
hi there!
can i know what designer class should i inherit to use it for winforms?
i used the parentcontroldesigner class, but it seems it's not applicable..
or a winform doesn't support designer? if ever, what should i do to make a designer verb for winforms?
tnx in advance!
|
|
|
|
|
You may want to take a look at the code for #Develop. If I recall correctly, they've got a WinForms designer in there. Might be worth taking a look at.
Picture a huge catholic cathedral. In it there's many people, including a gregorian monk choir. You know, those who sing beautifully. Then they start singing, in latin, as they always do: "Ad hominem..."
-Jörgen Sigvardsson
|
|
|
|
|
Why if XmlReader has an EOF property, the BinaryReader doesn't have it?
I don't like this, you can read why in my blog:
The EOF Issue[^]
How do you solve this issue?
Eduardo Diaz
site | english blog | spanish blog
|
|
|
|
|
Use the ReadBytes method.
---
b { font-weight: normal; }
|
|
|
|
|
Yes, we can use ReadBytes, then why bother to use BinaryReader!?
I like to BinaryReadar because you read primitive types.
using ReadBytes you have to write code like this:
byte[] data = f.ReadBytes(sizeof(double));
double value = BitConverter.ToDouble(data, 0);
The problem is that accroding to documentation:
==== Microsoft Documentation ====
Return Value
A byte array containing data read from the underlying stream. This might be
less than the number of bytes requested if the end of the stream is reached.
======
What happen when EOF is reached? data is null?
Also this could generate an exception to be thrown when I call BitConverter.ToDouble().
BinaryReader is bad designed.
Eduardo Diaz
site | english blog | spanish blog
|
|
|
|
|
ediazc wrote:
What happen when EOF is reached? data is null?
No. You simply get an array that contains less bytes that you requested.
The way to detect end of stream is to examine the length of the array. If the length is shorter than the number of bytes requested, you have reached the end of the stream. The array will contain the last remaining bytes of the stream. If you already read all data up to the end, the length of the array will be zero.
You have to check the length of the array before you try to convert the bytes to a double. If there aren't bytes enough for a double, you have reached the end of the stream.
ediazc wrote:
BinaryReader is bad designed.
I don't think so. Perhaps it's not designed the way you are used to, or maybe not designed the way you try to use it. I believe it's rather well designed for it's purpose, though.
---
b { font-weight: normal; }
|
|
|
|