.NET Framework - VB.net syntax for C# code
Asked By Chris
21-Mar-07 05:42 PM
Not sure if this is OT
I periodically, once every week or so, get an error when trying to read an
XML doc stored in cache. I get an 'object not set to a reference' I have a
gut feeling it happens when the app is under strain and the moment between
testing the cache and returning the cached xml doc it expires. I have read
several other people have the same problem. I have seen some C# code which
theoretically makes sense to me, but I don't understand the syntax and how
you would write it in VB.net. And I thought I had got me head round
Dim cacheitem as object = Cache(key) as XMLDocument
but with no luck. Here is the C# code in full.
Object cacheItem = Cache[key] as DataTable;
if(cacheItem == null)
{
cacheItem = GetData();
Cache.Insert(key, cacheItem, null, DateTime.Now.AddHours(1),
TimeSpan.Zero);
}
return (DataTable)cacheItem;
Regards, Chris.
DataTable
(1)
TimeSpan
(1)
DateTime
(1)
AddHours
(1)
GetData
(1)
Insert
(1)
DavidAnto replied...
(via Instant VB):
Dim cacheItem As Object = TryCast(Cache(key), DataTable)
If cacheItem Is Nothing Then
cacheItem = GetData()
Cache.Insert(key, cacheItem, Nothing, DateTime.Now.AddHours(1),
TimeSpan.Zero)
End If
Return CType(cacheItem, DataTable)
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: C#/VB to Python converter

DATETIME to only return DATE .NET Framework What magic to I need to do to get the DATETIME.Today to only return the DATE?!? C# Discussions ToShortDateString (1) ToLongDateString (1) TimeSpan (1) DateTime (1) Class (1) Date (1) String Felix Palmen (1) Felix Felix Palmen (1) Do you mean the "System.DateTime" struct? There is no such type as For System.DateTime, it is not possible for it "to only return the date". Note the name of the type: "DateTime". it is a date _and_ a time. Always. If you want to only _display_ the date after you have retrieved a particular DateTime value (including DateTime.Today), then just do that. There is nothing forcing you to include
me use less memory! .NET Framework I have got a problem. I load a huge datatable, with 111 columns. This datatable runs a System.OutOfMemory exception if its bigger than about 280, 000 rows. that is fine. I have come to accept that. when I fill a datatable up to near its max limit of 280k rows, use of program memory goes from choice there. but then I go through a loop like this: / / dtResults is the HUGE datatable. / / I clone the datatable a few times. this makes new datatables / / with the same columns. DataTable dtRejectedResults = new DataTable(); dtRejectedResults = dtResults.Clone(); DataTable dtExcludedResults = new DataTable(); dtExcludedResults = dtResults.Clone(); DataTable dtExcludedResults2 = new DataTable(); dtExcludedResults2 = dtResults.Clone(); DataTable dtExcludedResults3
realized it is probably not safe. Can someone comment on this: public DataRow GetWhatever() { using (DataTable dataTable = GetDataTable()) { return dataTable[0]; } } The problem is that "DataRow" has a "Table" property that points back to the same "dataTable" in the "using" statement above. That object is disposed of in the "using" statement however this correct? Note BTW that the indexer above is actually present in my case (the "DataTable" is actually a "DataTable" derivative created using the VS dataset designer) C# Discussions Visual Studio (1) Vista (1) DataTable.Dispose (1) Console.WriteLine (1) GetDataTable (1) DataTable (1) DataRow (1) ADO.NET (1) Yes, you definitely do not want to dispose objects
XmlTests.Program.SortDataSetFromDatabase (1) System.Diagnostics.Debug.WriteLine (1) XmlTests.Program.SortDataSet (1) System.Data.DataTable.Select (1) DataViewRowState.CurrentRows (1) Visual Studio 2005 (1) OleDbDataAdapter (1) Hallo Martin, Am Sun genannte Ausnahmefehler kann mehrere Ursachen haben. Die ersten sind relativ schnell zu ??berpr??fen: 1. DataTable.Columns null ist oder enth??lt keine Eintr??ge enth??lt. 2. DataTable.Columns[sSortExp] findet keine entspr. Spalte. Da die Zuweisung string sSortExp = oDs.Tables[0].Columns genau dem Fehlertext geworfen, den Du angegeben hast. Meine Frage: Kann es sein, dass deine DataTable eine DataRow = = null enth??lt? (Wie immer in solch obskuren F??llen ist es gut System.Data.dll aufgetreten. Die Spalte [Status] wurde nicht gefunden. bei System.Data.NameNode.Bind(DataTable table, List`1 list) bei System.Data.BinaryNode.Bind(DataTable table, List`1 list) bei System.Data.DataExpression.Bind(DataTable table) bei System.Data.DataExpression. . ctor(DataTable table, String expression, Type type) bei System.Data.Select. . ctor(DataTable table, String filterExpression, String
How is System.Data.DataTable.Load() determining whether a column is a primary key? .NET Framework IBM's ADO.NET lot of cases relies on the MS System class's implementation. In the case of DataTable.Load(), IBM's provider relies completely on the implementation provided in the Microsoft's System.Data.DataTable. What we'd like to find out are the schema's column attributes that are is a part of the primary key, and, therefore, needs to be added to the DataTable's PrimaryKey property. The description of the Load(IDataReader) method on MSDN states that in the case when DataTable has no schema one is inferred from the IDataReader's result set. Is the mechanism for the question is that we encountered a problem where the PrimaryKey property of the DataTable is not being set for the tables with defined nullable PK, even though the "IsKey" column attribute is set to is defined with a non-nullable PK, then the DataTable's PrimaryKey property is set correctly. However, if I understand the docs correctly, column nullability in my previous reply in another thread you raised in newsgroup. We can know the DataTable.Load() behavior through Reflected codes or debugging .NET sources codes. To know how to debug