Data Binding Issue

Energizeer

New member
Joined
Jan 19, 2007
Messages
2
Programming Experience
1-3
Hello,
I'm new to VS 2005 and have been learning for only a few weeks so please forgive me if I seem like a newbie.
I got a number of Videos from LearnVisualStudios.net and they walked me through the drag and drob database set up. I have datagrids which work well and display the data just fine. The thing is, I want to customize the layout of the way the data is shown and edited. I have read many articles about the subject and am now pretty familiar with ADO.NET and all that. However, much of the info I have found doesn't apply to my set up in VS.NET.

VS set up my dataset and the contained DataTables and Table adapters for me. Basically all I want to do is set the text in a textbox or label to a field in my database. This is the code I was trying to use:
VB.NET:
textBox1.Text = DataSet1.Tables["MainTable"].Rows[0].ItemArray[2].ToString();

Dataset1s intelisence doesn't give me a Tables option so this code won't work.

From what I have learned, VS uses Typed DataSets which don't seem to have the .Tables method like the old ADO.
I have now tried the following:
VB.NET:
DataSet1 NewDataSet = new DataSet1();
String Testout = NewDataSet.MainTableTBL.Rows[0].ItemArray[0].ToString();
TextBox1.Text = Testout;

But I get an error that there is no row at position 0.

I would really appreciate some help on this matter,

Thanks.

P.S. Please don't tell me to read a Book. I don't have access to a VS.NET book at the moment. Thanks :)
 
There appear to be something wrong with the posted code, it's C# ;) .... which we don't do here at the VB.Net Forums site.

PS: Even if you don't have a book, you still have access to the MSDN documentation with all its articles and walk-throughs. There are also lots of C# forums that will most certainly help you with C# coding.
 
Basically all I want to do is set the text in a textbox or label to a field in my database. This is the code I was trying to use:
VB.NET:
textBox1.Text = DataSet1.Tables["MainTable"].Rows[0].ItemArray[2].ToString();

Dataset1s intelisence doesn't give me a Tables option so this code won't work.

Well, er.. yes. Youre right, it wont work, because DataSet1 is the name of the class. If it were the name of the instance (created by the form designer), it would be called dataSet1. This is the rule in C#.

ClassName, AnotherClass, ThirdClass, AndSoOn
instanceName, anotherInstance, thirdInstance, andSoOn

Now, if you ndont know the difference between classes and instances, go read up now :D


Also, your DataSet1 is TYPED, so dont access it in an untyped way. Do this:

dataSet1.MainTable[0].COLUMN_NAME (whatever column 2 is)


From what I have learned, VS uses Typed DataSets which don't seem to have the .Tables method like the old ADO.
Typed DataSets inherit from untyped ones. You can still use .Tables if you want, but the IDE wont be able to help you with intellisense, for obvious reasons.

I have now tried the following:
VB.NET:
DataSet1 NewDataSet = new DataSet1();
String Testout = NewDataSet.MainTableTBL.Rows[0].ItemArray[0].ToString();
TextBox1.Text = Testout;
DataSet1 is the type, here you made an instance, rather than addressing the type as above.. hence the reason why this one "works" - because youre addressing the instance.

If you were doing this nin vb it would be even more confusing, because the instances are named identically to the classes, static or otherwise. Again, dont use the .Rows collection when working with typed sets, because it inherits from untyped, and hence it returns a untyped generic DataRow. Use the default property of the table:

myDataSet.MyTableName[0] //good
myDataSet.MyTableName.Rows[0] //bad!

Similarly dont use ItemArray. Just use the column nname if working with typed or default property if you work with untyped:

myDataSet.MyTableName[0].ColumnNameWhichIsAlreadyAString //good
myDataSet.MyTableName.Rows[0][2].ToString() //bad, but you dont have a choice!

But I get an error that there is no row at position 0.

You get that with pretty much any collection that doesnt have an element at the referenced position. Your collection has no rows, hence there is no row at position 0. I suggest you try putting some rows in there first

I would really appreciate some help on this matter,

Well, aside from what i rwote above, there is another nugget of advice..

This isnt how we do data binding at all. This isnt how we show in a text box what we find in a row in a database.. Youre making it very hard work for yourself..

P.S. Please don't tell me to read a Book. I don't have access to a VS.NET book at the moment. Thanks :)
I'm afraid I'm going to. Data access in .NET is such a huge topic, that you havent scratched the surface yet. Forget tutorials you read anywhere else, get them straight from the horses mouth. Take a look at the DW2 link in my signature. It has C# versions of all code too.

Just to reinforce john's point - the VB in vbdotnetforums.com is most poignant - the forums are targeted at vb programmers.
I dont mind you posting in C#, but thats cause Im a C# fanboy who cant actually see any difference in the .net syntaxes any more.
Other people (with bigger hammers to swing around) might get upset if you persist, so you can either make your future posts in vb, or take a look at the C# forums over on codeguru - I recommend them for a variety of reasons, not least becuase I swing more of a hammer there :D
 
Cjard,

Wow thanks for that very detailed reply to my question!
There is a lot there for me to take in!
Again, dont use the .Rows collection when working with typed sets, because it inherits from untyped, and hence it returns a untyped generic DataRow.
I see what you mean here, I was having major difficulties with conflicting types of data being used.
I'm beginnig to get a handle on things now. I am used to Java and VB. C# is nice and I'm hoping to stick with it.
I'm might be posting some questions over on code guru soon so I might see you there,
Thanks again,
 
Back
Top