Monday, March 12, 2012

How to Convert from OleDbParametersCollection to OleDbParameter[] array?

I am trying to convert from OleDbParametersCollection object to OleDbParameter[] array. How to perform this operation(ConvertToOleDbParameter())?

What is the problem with the following code? :

Code Snippet

private OleDbParameter[] ConvertToOleDbParameter(OleDbParameterCollection paramCollection)

{

OleDbParameter[] paramArray = new OleDbParameter[paramCollection.Count];

OleDbParameter param = new OleDbParameter();

paramArray.Initialize();

IEnumerator enumParams = paramCollection.GetEnumerator();

for (int i = 0; i < paramCollection.Count; i++)

{

try

{

enumParams.MoveNext();

param = (OleDbParameter) enumParams.Current;

paramArray[i] = param;

}

catch (Exception e)

{

MessageBox.Show(e.Message);

}

}

return paramArray;

}

So, when I do following:

Code Snippet

//....

try

{

OleDbParameter[] arrParams = ConvertToOleDbParameter(paramCollection);

oleCommand.Parameters.AddRange(arrParams);

oleCommand.ExecuteNonQuery();

return true;

}

catch (Exception exc)

{

System.Windows.Forms.MessageBox.Show(exc.Message);

return false;

}

The above throws the following exception:

The OleDbParameter is already contained by another OleDbParameterCollection.

I would appreciate your help.

Thanks.

oleCommand.Parameters is also a OledDbParameterCollection. Your conversion method merely returns an array whose elements are references of the existing parameters in paramCollection. The AddRange() call tries to add those parameters in paramCollection to another parametercollection (oleCommand.Parameters).

The error message tells you that for any parameter, it can only belong to one parametercollection.

Labels: , , , , , , , , ,

how to convert DataColumn to string array

Dear friends,

How to convert DataColumn to String Array

I have a set of values in DataColumn that values should be transferred to String Array. Is there any sort way without using looping statement.

Thanks.

I think there is no other alternative than looping the table .. a similar stuff is discussed and can be used

http://dotnet.org.za/keithrull/archive/2006/01/13/38913.aspx


Hi,

If you just need to get the values in each DataColumn in a DataRow, there is an ItemArray property for DataRow class. You can go through each row and call DataRow.ItemArray property to get it.

Here is a link for more information

http://msdn2.microsoft.com/en-us/library/system.data.datarow.itemarray.aspx

HTH. If this does not answer your question, please feel free to mark the post as Not Answered and reply. Thank you!

Labels: , , , , , , , , , ,