Check out these two functions for serializing and deserializing data using a BinaryFormatter. It serializes a hashtable but can be replaced by an arraylist and should work just fine. static void Serialize() { // Create a hashtable of values that will eventually be serialized. Hashtable addresses = new Hashtable(); addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052"); addresses.Add("Fred", "987 Pine Road, Phila., PA 19116"); addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301"); // To serialize the hashtable and its key/value pairs, // you must first open a stream for writing. // In this case, use a file stream. FileStream fs = new FileStream("DataFile.dat", FileMode.Create); // Construct a BinaryFormatter and use it to serialize the data to the stream. BinaryFormatter formatter = new BinaryFormatter(); try { formatter.Serialize(fs, addresses); } catch (SerializationException e) { Console.WriteLine("Failed to serialize. Reason: " + e.Message); throw; } finally { fs.Close(); } } static void Deserialize() { // Declare the hashtable reference. Hashtable addresses = null; // Open the file containing the data that you want to deserialize. FileStream fs = new FileStream("DataFile.dat", FileMode.Open); try { BinaryFormatter formatter = new BinaryFormatter(); // Deserialize the hashtable from the file and // assign the reference to the local variable. addresses = (Hashtable) formatter.Deserialize(fs); } catch (SerializationException e) { Console.WriteLine("Failed to deserialize. Reason: " + e.Message); throw; } finally { fs.Close(); } // To prove that the table deserialized correctly, // display the key/value pairs. foreach (DictionaryEntry de in addresses) { Console.WriteLine("{0} lives at {1}.", de.Key, de.Value); } }
Elvis (a.k.a Azerax) Life is Music listen to it before it fades
Azerax
Posts
-
Save inside a file ArrayList code -
Displaying data from 2 Tables in one GridI guess the best way to do it is to combine the data from the two tables into one using a join in the query used to retrieve it. And then you can bind the dataTable to the Grid. Elvis (a.k.a Azerax) Life is Music listen to it before it fades
-
Cannot get value because it is DBNullOhh I am sorry I got it wrong again . Please note the corrected code as follows: dtpBirthday.Value = dsPlayer.Tables["Players"].Rows[0]["Birthday"]== DBNull.Value ? DateTime.Now : dsPlayer.Tables["Players"].Rows[0]["Birthday"].ToString() ; Sorry for the trouble... Elvis (a.k.a Azerax) Life is Music listen to it before it fades
-
Cannot get value because it is DBNullOhh I am sorry :(. Please note the corrected code as follows:
dtpBirthday.Value = dsPlayer.Players[0]["BirthDay"]== DBNull.Value ? DateTime.Now : dsPlayer.Players[0]["Birthday"].ToString() ;
Where birthday is a field in your dataTable. Elvis (a.k.a Azerax) :cool: Life is Music listen to it before it fades -
Redim Arrays in C#I have written a function for a similar purpose that creates a dynamic array. Here Check this out:
_/// /// Function to add an array element to a dynamic Array /// /// Dynamic Array /// Element to add to the dynamic array /// array with the element appended to it_ public static Array RedimNPreserve(int[] Arr,int Element) { System.Collections.ArrayList al = new System.Collections.ArrayList(); int i; if(Arr!=null) { for(i=0;i<=Arr.GetUpperBound(0);i++) { al.Add(Arr[i]); } } al.Add(Element); return al.ToArray(typeof(int)); }
Elvis (a.k.a. Azerax) Life is Music listen to it before it fades -
Cancel closing a formCancelling the closing of a form is quite simple and can be implemented as follows: protected void Form1_Cancel (Object sender, CancelEventArgs e) { if (!myDataIsSaved) { e.Cancel = true; MessageBox.Show("You must save first."); } else { e.Cancel = false; MessageBox.Show("Goodbye."); } } Elvis (a.k.a. Azerax) Life is Music listen to it before it fades
-
Cannot get value because it is DBNullHow about trying this? dtpBirthday.Value = dsPlayer.Players[0].BirthDay.ToString()== DBNull.Value ? DateTime.Now : dsPlayer.Players[0].BirthDay ; This is an example of a conditional if statement and will best suit your purpose. The Conditional Statement variable = op1 == op2 ? trueValue : FalseValue ; where: op1 == op2 is your condition Life is Music listen to it before it fades away