Accessing a column by name in dataset

pachjo

Well-known member
Joined
Dec 12, 2006
Messages
370
Programming Experience
10+
I have setup some code as follows but cannot seem to access the column named fld in the table by name but can by ordinal, can someone explain why please?

cjard, notice no silly names and use of inbuilt code rather than crearting it all myself ;)

VB.NET:
    Dim ds As New MyFinanceDataSet

    Dim ta As New MyFinanceDataSetTableAdapters.MFTableAdapter

    ta.Fill(ds.MF)
    
    ' this works...

    tmpVerify = System.Text.Encoding.Default.GetBytes(ds.MF(0).ToString) 

    ' but this does not...

    tmpVerify = System.Text.Encoding.Default.GetBytes(ds.MF.fldColumn.ToString)

    ' and this does not work as .fld does not appear in intelli sense?

    tmpVerify = System.Text.Encoding.Default.GetBytes(ds.MF.fld)
 
erm.. hangon a sec

ds is a dataset
MF is hence a typed datatable
.Rows is the default property of a datatable

MF(0).ToString() hence refers to ROW ZERO. NOT A COLUMN.

MF.fldColumn refers to the column of the datatable. It doesnt have values, but it has things like what type of data is in the column, the expression property, whether the column allows nulls

To get a value of data, you need to give both row and column:

Either:

ds.MF(0)(0).ToString() 'untyped access, returns Object, less desirable hence need for ToString
ds.MF(0).fld 'typed access, returns value of fld as whatever type fld is

this assumes fld is the first column, if it were the 10th you would say:

ds.MF(0)(9).ToString()

But as noted, using numbers is less desirable


It makes sense to usenumbers for rows because, well.. rows dont have names.

in full:

ds.MF(0)(9)
ds.MF.Rows(0).Item(9)

better:
ds.MF(0).fld
ds.MF.Rows(0).fld


dig it? :D

Ps, fld is a property, it should be Fld.. but the IDE generates that code and it adopts the case form the database.. Its such bloody hard work this naming convention stuff isnt it? :D
 
;) thanks cjard, yes the naming convention is a pain, but for now I will stick to my 'norm'

I am trying to get to grips with doing data access in the manner you tutored me and for the most part it does make much more sense....as well as being easier to achieve :D
 
The fact I have to ask this shows something just isn't sinking in!

VB.NET:
ro = ds.TransactionDescriptions.NewTransactionDescriptionsRow

' why do I have to do this

 ro.Item("description") = T.Text

' instead of this?

ro.description = T.Text

If a dataset is typed does it not mean that the second method should work?

Thanks......for you patience :eek:
 
If a dataset is typed does it not mean that the second method should work?

Indeed it should.. BUT did you Dim ro as a DataRow or a TransactionsDescriptionsDataRow?


You see this is valid:

Dim ro as DataRow = MyTable.NewMyRow


Because myRow is a inherited child of datarow.. but once you put the type specific child in the parent generic box, you only see the generic parents methods

You must dim the variable as the child:

Dim ro as MyDataSet.MyXXXChildRow = myDataSetInstance.myTableInstance.NewXXXChildRow()

I have used the MS naming conventions for type and instance here and also put the word instance to help clarify
 
Last edited:
I tried this but still .Description does not appear?

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ds [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] MyFinanceDataSet
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ro [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataRow = ds.TransactionDescriptions.NewTransactionDescriptionsRow
[/SIZE]
 
Forgot to ask this:

I realise I can use the dataset on my form rather than creating an instance of it. This way as I enter new records the bindingsourced controls on the form will be updated :)

However, I do this:

VB.NET:
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].MyFinanceDataSet1.TransactionDescriptions.NewTransactionDescriptionsRow()
[/SIZE]

Which creates a new row, but how do I neatly refer to that row to append the new data.

Then I presume I can call the update command ot the tableadpater also on the form to write backl to my database:)
 
I tried this but still .Description does not appear?

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ds [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] MyFinanceDataSet[/SIZE]
[SIZE=7][COLOR=red][B]Dim ro As DataRow[/B][/COLOR][/SIZE][SIZE=2] = ds.TransactionDescriptions.NewTransactionDescriptionsRow[/SIZE]
 
 
.

Did you read any of my post?
 
Which creates a new row, but how do I neatly refer to that row to append the new data.

NewXXXRow() is a method that returns a new row. It does not add it to the datatable!
You must use it as a Factory (something that manufactures an object), edit the product, then put it in the table:

Dim ro as XXDataRow = tbl.NewXXXRow
ro.This = that
tbl.AddXXXRow(ro)

Then I presume I can call the update command ot the tableadpater also on the form to write backl to my database:)
Not without putting it in the table first, you cant ;) ;)
 
Did you read any of my post?

Yes and no.

Yes in that I read it, no in that I missed the bit about childrow.

However when I do this:

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ro [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] MyFinanceDataSet.
[/SIZE]

I do NOT get any entry that mentions childrow of any sort. All entries for my tables in the dataset that refer to rows end in row only, no child is mentioned.
 
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ro [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] MyFinanceDataSet.TransactionDescriptionsRow
ro.Description = [/SIZE][SIZE=2][COLOR=#a31515]"I think I Got IT!"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].MyFinanceDataSet1.TransactionDescriptions.Rows.Add(ro)
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].TransactionDescriptionsTableAdapter.Update([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].MyFinanceDataSet1.TransactionDescriptions)
[/SIZE]
 
Back
Top