Click here to Skip to main content
15,892,298 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: abt state management Pin
N a v a n e e t h15-Mar-07 18:17
N a v a n e e t h15-Mar-07 18:17 
AnswerRe: abt state management Pin
badgrs16-Mar-07 0:36
badgrs16-Mar-07 0:36 
Questionerror run solution vs 20005 sp1 Pin
AnhTin15-Mar-07 17:15
AnhTin15-Mar-07 17:15 
AnswerRe: error run solution vs 20005 sp1 Pin
Harini N K15-Mar-07 18:26
Harini N K15-Mar-07 18:26 
QuestionThe ConnectionString property has not been initialized." Pin
sridevi1415-Mar-07 17:11
sridevi1415-Mar-07 17:11 
AnswerRe: The ConnectionString property has not been initialized." Pin
N a v a n e e t h15-Mar-07 18:19
N a v a n e e t h15-Mar-07 18:19 
QuestionRequire Generic Custom Collection code in C#.Net Pin
ravi88515-Mar-07 17:07
ravi88515-Mar-07 17:07 
AnswerRe: Require Generic Custom Collection code in C#.Net Pin
Dave Doknjas16-Mar-07 12:51
Dave Doknjas16-Mar-07 12:51 
(via Instant C# - let me know how the converted code works for you - your original posted formatting is preserved)

using System;
using System.Data.SqlClient;
using System.Collections;

/////
///// A strongly-typed collection of objects.
/////

internal class SqlParamCollection : ICollection, IList, IEnumerable, ICloneable
{

#region Interfaces
/////
///// Supports type-safe iteration over a .
/////
public interface ISqlParamCollectionEnumerator
{

/////
///// Gets the current element in the collection.
/////
SqlParameter Current {get;}

/////
///// Advances the enumerator to the next element in the collection.
/////
/////
///// The collection was modified after the enumerator was created.
/////
/////
///// true if the enumerator was successfully advanced to the next element
///// false if the enumerator has passed the end of the collection.
/////
bool MoveNext();

/////
///// Sets the enumerator to its initial position, before the first element in the collection.
/////
void Reset();
}
#endregion

private const int DEFAULT_CAPACITY = 15;

#region Implementation (data)
private SqlParameter[] m_array;
private int m_count = 0;

private int m_version = 0;
#endregion


#region Static Wrappers
/////
///// Creates a synchronized (thread-safe) wrapper for a
///// SqlParamCollection instance.
/////
/////
///// An SqlParamCollection wrapper that is synchronized (thread-safe).
/////
public static SqlParamCollection Synchronized(SqlParamCollection list)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
return new SyncSqlParamCollection(list);
}

/////
///// Creates a read-only wrapper for a
///// SqlParamCollection instance.
/////
/////
///// An SqlParamCollection wrapper that is read-only.
/////
public static SqlParamCollection GetReadOnly(SqlParamCollection list)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
return new ReadOnlySqlParamCollection(list);
}
#endregion

#region Construction
/////
///// Initializes a new instance of the SqlParamCollection class
///// that is empty and has the default initial capacity.
/////
public SqlParamCollection()
{
m_array = new SqlParameter[DEFAULT_CAPACITY + 1];
}

/////
///// Initializes a new instance of the SqlParamCollection class
///// that has the specified initial capacity.
/////
/////
///// The number of elements that the new SqlParamCollection is initially capable of storing.
/////
public SqlParamCollection(int capacity)
{
m_array = new SqlParameter[capacity + 1];
}

/////
///// Initializes a new instance of the SqlParamCollection class
///// that contains elements copied from the specified SqlParamCollection.
/////
///// The SqlParamCollection whose elements are copied to the new collection.
public SqlParamCollection(SqlParamCollection c)
{
m_array = new SqlParameter[c.Count + 1];
AddRange(c);
}

/////
///// Initializes a new instance of the SqlParamCollection class
///// that contains elements copied from the specified array.
/////
///// The array whose elements are copied to the new list.
public SqlParamCollection(SqlParameter[] a)
{
m_array = new SqlParameter[a.Length + 1];
AddRange(a);
}
#endregion

#region Operations (type-safe ICollection)
/////
///// Gets the number of elements actually contained in the SqlParamCollection.
/////
public virtual int Count
{
get
{
return m_count;
}
}

/////
///// Copies the entire SqlParamCollection to a one-dimensional
///// array.
/////
///// The one-dimensional array to copy to.
public virtual void CopyTo(SqlParameter[] array)
{
this.CopyTo(array, 0);
}

/////
///// Copies the entire SqlParamCollection to a one-dimensional
///// array, starting at the specified index of the target array.
/////
///// The one-dimensional array to copy to.
///// The zero-based index in at which copying begins.
public virtual void CopyTo(SqlParameter[] array, int start)
{
if (m_count > (array.GetUpperBound(0) + 1 - start))
{
throw new System.ArgumentException("Destination array was not long enough.");
}
System.Array.Copy(m_array, 0, array, start, m_count);
}

/////
///// Gets a value indicating whether access to the collection is synchronized (thread-safe).
/////
///// true if access to the ICollection is synchronized (thread-safe) otherwise, false.
public virtual bool IsSynchronized
{
get
{
return m_array.IsSynchronized;
}
}

/////
///// Gets an object that can be used to synchronize access to the collection.
/////
public virtual object SyncRoot
{
get
{
return m_array.SyncRoot;
}
}
#endregion


#region Operations (type-safe IList)
/////
///// Gets or sets the at the specified index.
/////
///// The zero-based index of the element to get or set.
/////
///// is less than zero
///// -or-
///// is equal to or greater than .
/////
public virtual SqlParameter this[int index]
{
get
{
ValidateIndex(index); //// throws
return m_array[index];
}
set
{
ValidateIndex(index); //// throws
m_version += 1;
m_array[index] = value;
}
}

/////
///// Gets or sets the with the specified key.
/////
///// The name of the element to get or set.
/////
/////
public virtual SqlParameter this[string key]
{
get
{
int index = 0;
index = FindItem(key); //// throws
return m_array[index];

}
set
{
int index = 0;
index = FindItem(key); //// throws
m_version += 1;
m_array[index] = value;
}
}

/////
///// Adds a to the end of the SqlParamCollection.
/////
///// The to be added to the end of the SqlParamCollection.
///// The index at which the value has been added.
public virtual int Add(SqlParameter item)
{
if (m_count == m_array.Length)
{
EnsureCapacity(m_count + 1);
}

m_array[m_count] = item;
m_version += 1;
m_count += 1;
return (m_count - 1);
}

/////
///// Removes all elements from the SqlParamCollection.
/////
public virtual void Clear()
{
m_version += 1;
m_array = new SqlParameter[DEFAULT_CAPACITY + 1];
m_count = 0;
}

/////
///// Creates a shallow copy of the .
/////
public virtual object Clone()
{
SqlParamCollection newColl = new SqlParamCollection(m_count);
Array.Copy(m_array, 0, newColl.m_array, 0, m_count);
newColl.m_count = m_count;
newColl.m_version = m_version;

return newColl;
}

/////
///// Determines whether a given is in the SqlParamCollection.
/////
///// The to check for.
///// true if is found in the SqlParamCollection otherwise, false.
public virtual bool Contains(SqlParameter item)
{
int i = 0;
for (i = 0; i <= (m_count - 1); i++)
{
if (m_array[i].Equals(item))
{
return true;
}
}
return false;
}

/////
///// Returns the zero-based index of the first occurrence of a
///// in the SqlParamCollection.
/////
///// The to locate in the SqlParamCollection.
/////
///// The zero-based index of the first occurrence of
///// in the entire SqlParamCollection, if found otherwise, -1.
/////
public virtual int IndexOf(SqlParameter item)
{
int i = 0;
for (i = 0; i <= (m_count - 1); i++)
{
if (m_array[i].Equals(item))
{
return i;
}
}
return -1;
}

/////
///// Inserts an element into the SqlParamCollection at the specified index.
/////
///// The zero-based index at which should be inserted.
///// The to insert.
/////
///// is less than zero
///// -or-
///// is equal to or greater than .
/////
public virtual void Insert(int index, SqlParameter item)
{
ValidateIndex(index, true); //// throws

if (m_count == m_array.Length)
{
EnsureCapacity(m_count + 1);
}

if (index < m_count)
{
Array.Copy(m_array, index, m_array, index + 1, m_count - index);
}

m_array[index] = item;
m_count += 1;
m_version += 1;
}

/////
///// Removes the first occurrence of a specific from the SqlParamCollection.
/////
///// The to remove from the SqlParamCollection.
/////
///// The specified was not found in the SqlParamCollection.
/////
public virtual void Remove(SqlParameter item)
{
int i = IndexOf(item);
if (i < 0)
{
throw new System.ArgumentException("Cannot remove the specified item because it was not found in the specified Collection.");
}

m_version += 1;
RemoveAt(i);
}

/////
///// Removes the element at the specified index of the SqlParamCollection.
/////
///// The zero-based index of the element to remove.
/////
///// is less than zero
///// -or-
///// is equal to or greater than .
/////
public virtual void RemoveAt(int index)
{

ValidateIndex(index); //// throws

m_count -= 1;

if (index < m_count)
{
Array.Copy(m_array, index + 1, m_array, index, m_count - index);
}

//// We can't set the deleted entry equal to null, because it might be a value type.
//// Instead, we'll create an empty single-element array of the right type and copy it
//// over the entry we want to erase.
SqlParameter[] temp = new SqlParameter[1];
temp[0] = new SqlParameter();
Array.Copy(temp, 0, m_array, m_count, 1);
m_version += 1;
}

/////
///// Gets a value indicating whether the collection has a fixed size.
/////
///// true if the collection has a fixed size otherwise, false. The default is false
public virtual bool IsFixedSize
{
get
{
return false;
}
}

/////
///// gets a value indicating whether the IList is read-only.
/////
///// true if the collection is read-only otherwise, false. The default is false
public virtual bool IsReadOnly
{
get
{
return false;
}
}
#endregion


#region Operations (type-safe IEnumerable)

/////
///// Returns an enumerator that can iterate through the SqlParamCollection.
/////
///// An for the entire SqlParamCollection.
public virtual ISqlParamCollectionEnumerator GetEnumerator()
{
return new Enumerator(this);
}
#endregion


#region Public helpers (just to mimic some nice features of ArrayList)

/////
///// Gets or sets the number of elements the SqlParamCollection can contain.
/////
public virtual int Capacity
{
get
{
return m_array.Length;
}

set
{
if (value < m_count)
{
value = m_count;
}

if (! (value == m_array.Length))
{
if (value > 0)
{
// C# implementation
//Dim temp() As SqlParameter = New SqlParameter(Value-1) {}
//Array.Copy(m_array, temp, m_count)
//m_array = temp
// VB implementation
Array.Resize(ref m_array, value + 1);
}
else
{
m_array = new SqlParameter[DEFAULT_CAPACITY + 1];
}
}
}
}

/////
///// Adds the elements of another SqlParamCollection to the current SqlParamCollection.
/////
///// The SqlParamCollection whose elements should be added to the end of the current SqlParamCollection.
///// The new of the SqlParamCollection.
public virtual int AddRange(SqlParamCollection x)
{

if (m_count + x.Count >= m_array.Length)
{
EnsureCapacity(m_count + x.Count);
}

Array.Copy(x.m_array, 0, m_array, m_count, x.Count);
m_count += x.Count;
m_version += 1;

return m_count;
}

/////
///// Adds the elements of a array to the current SqlParamCollection.
/////
///// The array whose elements should be added to the end of the SqlParamCollection.
///// The new of the SqlParamCollection.
public virtual int AddRange(SqlParameter[] x)
{

if (m_count + x.Length >= m_array.Length)
{
EnsureCapacity(m_count + x.Length);
}

Array.Copy(x, 0, m_array, m_count, x.Length);
m_count += x.Length;
m_version += 1;

return m_count;
}

/////
///// Sets the capacity to the actual number of elements.
/////
public virtual void TrimToSize()
{
this.Capacity = m_count;
}

#endregion

#region Implementation (helpers)

/////
///// is less than zero
///// -or-
///// is equal to or greater than .
/////
private void ValidateIndex(int i)
{
ValidateIndex(i, false);
}

/////
///// is less than zero
///// -or-
///// is equal to or greater than .
/////
private void ValidateIndex(int i, bool allowEqualEnd)
{
int max = 0;

if (allowEqualEnd)
{
max = m_count;
}
else
{
max = m_count - 1;
}

if (i < 0 | i > max)
{
throw new System.ArgumentOutOfRangeException("Index was out of range. Must be non-negative and less than the size of the collection.", (object)1, "Specified argument was out of the range of valid values.");
}
}

/////
///// was not found.
/////
private int FindItem(string key)
{
int index = 0;

for (index = 0; index < m_array.Length; index++)
{
if (m_array[index].ParameterName == key)
{
return index;
}
}

throw new System.ArgumentException("The specified key was not found.");
}

private void EnsureCapacity(int min)
{

int newCapacity = 0;
if (m_array.Length == 0)
{
newCapacity = DEFAULT_CAPACITY;
}
else
{
newCapacity = m_array.Length * 2;
}

if (newCapacity < min)
{
newCapacity = min;
}

this.Capacity = newCapacity;
}

#endregion

#region Implementation (ICollection)

public void CopyTo(Array array, int start)
{
array.Copy(m_array, 0, array, start, m_count);
}

#endregion

#region Implementation (IList)
// Default removed
//ORIGINAL LINE: Property IList_Item(ByVal i As Integer) As Object Implements IList.Item
//INSTANT C# NOTE: C# does not support parameterized properties - the following property has been divided into two methods:
public object GetIList_Item(int i)
{
return (object)(this[i]);

}
public void SetIList_Item(int i, object Value)
{
this[i] = (SqlParameter)Value;
}

public int Add(object x)
{
return this.Add((SqlParameter)x);
}

public bool Contains(object x)
{
return this.Contains((SqlParameter)x);
}

public int IndexOf(object x)
{
return this.IndexOf((SqlParameter)x);
}

public void Insert(int pos, object x)
{
this.Insert(pos, (SqlParameter)x);
}

public void Remove(object x)
{
this.Remove((SqlParameter)x);
}

//Sub RemoveAt(ByVal pos As Integer) Implements IList.RemoveAt
// Me.RemoveAt(pos)
//End Sub

#endregion

#region Implementation (IEnumerable)

IEnumerator IEnumerable.GetEnumerator()
{
return IEnumerable_GetEnumerator();
}
public IEnumerator IEnumerable_GetEnumerator()
{
return (IEnumerator)(this.GetEnumerator());
}

#endregion

#region Nested enumerator class
/////
///// Supports simple iteration over a .
/////
private class Enumerator : IEnumerator, ISqlParamCollectionEnumerator
{

#region Implementation (data)

private SqlParamCollection m_collection;
private int m_index;
private int m_version;

#endregion

#region Construction

/////
///// Initializes a new instance of the Enumerator class.
/////
/////
internal Enumerator(SqlParamCollection tc)
{
m_collection = tc;
m_index = -1;
m_version = tc.m_version;
}

#endregion

#region Operations (type-safe IEnumerator)

/////
///// Gets the current element in the collection.
/////
public SqlParameter Current
{
get
{
return m_collection[m_index];
}
}

/////
///// Advances the enumerator to the next element in the collection.
/////
/////
///// The collection was modified after the enumerator was created.
/////
/////
///// true if the enumerator was successfully advanced to the next element
///// false if the enumerator has passed the end of the collection.
/////
public bool MoveNext()
{
if (! (m_version == m_collection.m_version))
{
throw new System.InvalidOperationException("Collection was modified enumeration operation may not execute.");
}

m_index += 1;

if (m_index < m_collection.Count)
{
return true;
}
else
{
return false;
}
}

/////
///// Sets the enumerator to its initial position, before the first element in the collection.
/////
public void Reset()
{
m_index = -1;
}
#endregion

#region Implementation (IEnumerator)

object IEnumerator.Current
{
get
{
return IEnumerator_Current;
}
}
public object IEnumerator_Current
{
get
{
return (object)this.Current;
}
}

#endregion
}
#endregion

#region Nested Syncronized Wrapper class
private class SyncSqlParamCollection : SqlParamCollection
{

#region Implementation (data)
private SqlParamCollection m_collection;
private object m_root;
#endregion

#region Construction
internal SyncSqlParamCollection(SqlParamCollection list)
{
m_root = list.SyncRoot;
m_collection = list;
}
#endregion

#region Type-safe ICollection

public override void CopyTo(SqlParameter[] array)
{
lock(this.m_root)
{
m_collection.CopyTo(array);
}
}

public override void CopyTo(SqlParameter[] array, int start)
{
lock(this.m_root)
{
m_collection.CopyTo(array, start);
}
}

public override int Count
{
get
{
lock(this.m_root)
{
return m_collection.Count;
}
}
}

public override bool IsSynchronized
{
get
{
return true;
}
}

public override object SyncRoot
{
get
{
return this.m_root;
}
}
#endregion

#region Type-safe IList
public override SqlParameter this[int i]
{
get
{
lock(this.m_root)
{
return m_collection[i];
}
}
set
{
lock(this.m_root)
{
m_collection[i] = value;
}
}
}

public override int Add(SqlParameter x)
{
lock(this.m_root)
{
return m_collection.Add(x);
}
}

public override void Clear()
{
lock(this.m_root)
{
m_collection.Clear();
}
}

public override bool Contains(SqlParameter x)
{
lock(this.m_root)
{
return m_collection.Contains(x);
}
}

public override int IndexOf(SqlParameter x)
{
lock(this.m_root)
{
return m_collection.IndexOf(x);
}
}

public override void Insert(int pos, SqlParameter x)
{
lock(this.m_root)
{
m_collection.Insert(pos, x);
}
}

public override void Remove(SqlParameter x)
{
lock(this.m_root)
{
m_collection.Remove(x);
}
}

public override void RemoveAt(int pos)
{
lock(this.m_root)
{
m_collection.RemoveAt(pos);
}
}

public override bool IsFixedSize
{
get
{
return m_collection.IsFixedSize;
}
}

public override bool IsReadOnly
{
get
{
return m_collection.IsReadOnly;
}
}
#endregion

#region Type-safe IEnumerable
public override ISqlParamCollectionEnumerator GetEnumerator()
{
lock(this.m_root)
{
return m_collection.GetEnumerator();
}
}
#endregion

#region Public Helpers
//// (just to mimic some nice features of ArrayList)
public override int Capacity
{
get
{
lock(this.m_root)
{
return m_collection.Capacity;
}
}

set
{
lock(this.m_root)
{
m_collection.Capacity = value;
}
}
}

public override int AddRange(SqlParamCollection x)
{
lock(this.m_root)
{
return m_collection.AddRange(x);
}
}

public override int AddRange(SqlParameter[] x)
{
lock(this.m_root)
{
return m_collection.AddRange(x);
}
}
#endregion
}
#endregion

#region Nested Read Only Wrapper class
private class ReadOnlySqlParamCollection : SqlParamCollection
{

#region Implementation (data)
private SqlParamCollection m_collection;
#endregion

#region Construction
internal ReadOnlySqlParamCollection(SqlParamCollection list)
{
m_collection = list;
}
#endregion

#region Type-safe ICollection

public override void CopyTo(SqlParameter[] array)
{
m_collection.CopyTo(array);
}

public override void CopyTo(SqlParameter[] array, int start)
{
m_collection.CopyTo(array, start);
}

public override int Count
{
get
{
return m_collection.Count;
}
}

public override bool IsSynchronized
{
get
{
return m_collection.IsSynchronized;
}
}

public override object SyncRoot
{
get
{
return this.m_collection.SyncRoot;
}
}
#endregion

#region Type-safe IList
public override SqlParameter this[int i]
{
get
{
return m_collection[i];
}

set
{
throw new NotSupportedException("This is a Read Only Collection and can not be modified");
}
}

public override int Add(SqlParameter x)
{
throw new NotSupportedException("This is a Read Only Collection and can not be modified");
}

public override void Clear()
{
throw new NotSupportedException("This is a Read Only Collection and can not be modified");
}

public override bool Contains(SqlParameter x)
{
return m_collection.Contains(x);
}

public override int IndexOf(SqlParameter x)
{
return m_collection.IndexOf(x);
}

public override void Insert(int pos, SqlParameter x)
{
throw new NotSupportedException("This is a Read Only Collection and can not be modified");
}

public override void Remove(SqlParameter x)
{
throw new NotSupportedException("This is a Read Only Collection and can not be modified");
}

public override void RemoveAt(int pos)
{
throw new NotSupportedException("This is a Read Only Collection and can not be modified");
}

public override bool IsFixedSize
{
get
{
return true;
}
}

public override bool IsReadOnly
{
get
{
return true;
}
}
#endregion

#region Type-safe IEnumerable
public override ISqlParamCollectionEnumerator GetEnumerator()
{
return m_collection.GetEnumerator();
}
#endregion

#region Public Helpers
//// (just to mimic some nice features of ArrayList)
public override int Capacity
{
get
{
return m_collection.Capacity;
}

set
{
throw new NotSupportedException("This is a Read Only Collection and can not be modified");
}
}

public override int AddRange(SqlParamCollection x)
{
throw new NotSupportedException("This is a Read Only Collection and can not be modified");
}

public override int AddRange(SqlParameter[] x)
{
throw new NotSupportedException("This is a Read Only Collection and can not be modified");
}
#endregion
}
#endregion

}


David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter, VB to C++ converter
Instant Python: C# to Python converter, VB to Python converter

QuestionProfile Pin
shapper15-Mar-07 13:53
shapper15-Mar-07 13:53 
QuestionSessionID keep changing its value Pin
vietpointer15-Mar-07 13:42
vietpointer15-Mar-07 13:42 
AnswerRe: SessionID keep changing its value Pin
Venkatesh Mookkan15-Mar-07 16:17
Venkatesh Mookkan15-Mar-07 16:17 
QuestionBlogs. Pin
deepaks315-Mar-07 11:22
deepaks315-Mar-07 11:22 
QuestionActive directory and aspx/asp Pin
henlow198415-Mar-07 9:56
henlow198415-Mar-07 9:56 
AnswerRe: Active directory and aspx/asp Pin
Venkatesh Mookkan15-Mar-07 16:22
Venkatesh Mookkan15-Mar-07 16:22 
QuestionWeb Method In a User Control Pin
Sam Heller15-Mar-07 6:48
Sam Heller15-Mar-07 6:48 
AnswerRe: Web Method In a User Control Pin
Marcus J. Smith15-Mar-07 9:51
professionalMarcus J. Smith15-Mar-07 9:51 
GeneralRe: Web Method In a User Control Pin
Sam Heller15-Mar-07 12:56
Sam Heller15-Mar-07 12:56 
GeneralRe: Web Method In a User Control Pin
harshini21510-Dec-09 7:56
harshini21510-Dec-09 7:56 
Questionhow can install dotnetnuke without IIS ? Pin
B.A15-Mar-07 6:17
B.A15-Mar-07 6:17 
QuestionLanguage Preference Pin
Rahithi15-Mar-07 5:58
Rahithi15-Mar-07 5:58 
AnswerRe: Language Preference Pin
enjoycrack15-Mar-07 6:09
enjoycrack15-Mar-07 6:09 
QuestionClient side validation using database values Pin
snir_ya15-Mar-07 5:58
snir_ya15-Mar-07 5:58 
AnswerRe: Client side validation using database values Pin
enjoycrack15-Mar-07 6:02
enjoycrack15-Mar-07 6:02 
QuestionRe: Client side validation using database values Pin
snir_ya15-Mar-07 6:06
snir_ya15-Mar-07 6:06 
AnswerRe: Client side validation using database values Pin
RichardGrimmer15-Mar-07 6:11
RichardGrimmer15-Mar-07 6:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.