From: Gummadi on
Hi,

Following is the method that i am using to Convert a Datatable into ADODB
Recordset. It all works fine. I am able to successfully convert Datatable
into Recorsets.

But there are few cases where this code throws exception. In a scenario
where i define the Datatype of the Field to Decimal, but i may get something
like "--" in the data, it is throwing exception. Is there a way that i can
ignore the Datatype at the line
resultFields[0].Value = dr[columnName];
where i am trying to assign the data to the resultField.

So basically i want the datatype to be decimal, but if i get some other
values other than Decimal, i just want that to be ignored.

Can someone please help me with this?



private ADODB.Recordset ConvertToRecordset(System.Data.DataTable inTable,
string columnName, System.Type columnType)
{
ADODB.Recordset result = new ADODB.Recordset();
result.CursorLocation = ADODB.CursorLocationEnum.adUseClient;

ADODB.Fields resultFields = result.Fields;
System.Data.DataColumnCollection inColumns = inTable.Columns;

foreach (DataColumn inColumn in inColumns)
{
if (inColumn.ColumnName == columnName)
{
resultFields.Append(inColumn.ColumnName
, TranslateType(columnType)
, inColumn.MaxLength
, ADODB.FieldAttributeEnum.adFldMayBeNull
//, inColumn.AllowDBNull ?
ADODB.FieldAttributeEnum.adFldIsNullable :
//
ADODB.FieldAttributeEnum.adFldUnspecified
, null);
}
}

result.Open(System.Reflection.Missing.Value
, System.Reflection.Missing.Value
, ADODB.CursorTypeEnum.adOpenStatic
, ADODB.LockTypeEnum.adLockOptimistic, 0);

foreach (DataRow dr in inTable.Rows)
{
result.AddNew(System.Reflection.Missing.Value,
System.Reflection.Missing.Value);

try
{
resultFields[0].Value = dr[columnName];
}
catch
{
//resultFields[0].Value = "--";
}
}
result.MoveFirst();
return result;
}

Thanks,
Gummadi
From: Patrice on
Hello,

> So basically i want the datatype to be decimal, but if i get some other
> values other than Decimal, i just want that to be ignored.

You could perhaps use
http://msdn.microsoft.com/en-us/library/system.decimal.tryparse.aspx to
check if the value can be converted to a Decimal...

--
Patrice


From: Jeff Johnson on
"Gummadi" <Gummadi(a)discussions.microsoft.com> wrote in message
news:5B1F2C72-50DE-467D-AB83-D68364F0BA4B(a)microsoft.com...

> Following is the method that i am using to Convert a Datatable

[Canned response]

This is a VB "classic" newsgroup. Questions about VB.NET (including VB
2005/2008 and VB Express, which have dropped .NET from their names) are
off-topic here, as are posts filled with C#!

Please ask .NET questions in newsgroups with "dotnet" in their names. The
*.vb.* groups are for VB6 and earlier. If you don't see the *.dotnet.*
groups on your news server, connect directly to the Microsoft server:
msnews.microsoft.com.

For questions specific to the VB.NET language, use this group:

microsoft.public.dotnet.languages.vb

Please note that things like controls and data access, which have their own
subgroups in the Classic VB hierarchy, are not language-specific in .NET, so
you should look for groups like these:

microsoft.public.dotnet.framework.windowsforms.controls
microsoft.public.dotnet.framework.adonet

(Note that "vb" is not present in the group name.)