Reputation: 6476
I have the following VB code:
Dim dt As DataTable = DAL.WMS_Collaboration_Fetch(0).Tables(0)
If dt.Rows.Count > 0 Then
'Bind Dataset to Gridview
Dim _WMS_CollaborationInfo As New WMS_CollaborationInfo
With _WMS_CollaborationInfo
.CollaborationName = dt.Rows(0).Item["CollaborationName").ToString
.CollaborationID = dt.Rows(0).Item("CollaborationID").ToString
End With
converted it to C# code like this:
DataTable dt = DAL.WMS_Collaboration_Fetch(0).Tables(0);
if (dt.Rows.Count > 0) {
//Bind Dataset to Gridview
WMS_CollaborationInfo _WMS_CollaborationInfo = new WMS_CollaborationInfo();
{
_WMS_CollaborationInfo.CollaborationName = dt.Rows[0].Item["CollaborationName"].ToString;
_WMS_CollaborationInfo.CollaborationID = dt.Rows[0].Item["CollaborationID"].ToString;
}
I am, however, unable to run the C# code. In VB, the table column, using DataTable, is accessed by just passing in the column name (well, I dont know much about VB) e.g "CollaborationID" in
dt.Rows(0).Item("CollaborationID").ToString
Please what is the C# equivalent of reading the data from the table using DataTable? I simply mean how can i re-write my C# code so that it works.
Upvotes: 2
Views: 676
Reputation: 498942
Indexers in C# use square brackets - []
. You also don't need to access .Item
:
_WMS_CollaborationInfo.CollaborationID =
dt.Rows[0]["CollaborationID"].ToString();
Upvotes: 5
Reputation: 13419
In C# there is a distinction between methods and indexers. The first use parenthesis, the second squared braces. This took me a while to grasp while moving from VB where you use parenthesis for both.
Try:
dt.Rows[0].Item["CollaborationID"].ToString()
Edit: VB.NET and C# Comparison
I used to refer to this cheat sheet all the time, check out the Arrays section: http://www.harding.edu/fmccown/vbnet_csharp_comparison.html
Upvotes: 5