Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid Question, how to make typed 1

Status
Not open for further replies.

IndyGill

Technical User
Jan 15, 2001
191
GB
Hi

Im try to get my head round datasets being typed and untyped and had a query. Am i write in saying that a typed dataset has to have a schema which comes .xsd file. An untyped dataset does not have a schema and just contains tables, row and columns.

So im just wondering how do I create a strongly typed dataset programmatically, i dont know how to create a .xsd file, however I notice I can write the dataset out to an xml file (does this make it strongly typed). I notice that Visual Studio creates the .xsd file for you easily enough but im trying to stay away from the GUI side of things and doing it all by hand code first.

So for example how would i make the below example typed? And do I need to make sure every dataset I create is typed.

Dim dsDetails As New DataSet()
Dim Conn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("connBSS"))
Dim objAdapter As SqlDataAdapter = New SqlDataAdapter("SELECT SvpsName, SvpsID FROM Services", Conn)

objAdapter.Fill(dsDetails, "Details")

'Then i show the value
lblSvpsName.Text = dsDetails.Tables("Details").Rows(0).Item("SvpsName")
lblSvpsID.Text = dsDetails.Tables("Details").Rows(0).Item("SvpsID")

Thanks in advance

 
The typed dataset simply makes things easier when your coding since you can access the table and columns by name using intellisense rather than remembering them or useing the index number. So you don't need it but it's usefull.
ex.
Rather than
lblSvpsName.Text = dsDetails.Tables("Details").Rows(0).Item("SvpsName")


You could Have
lblSvpsName.Text = dsDetails.Details(0).SvpsName


A typed dataset a fancy class that acts as a data container. Use VS to create a typed dataset, then click the show all files option in the solution explorer. You'll notice that the xsd file has a plus sign next to it. Expand that node and take a look at the two files that are underneath it. The .cs or .vb file is your dataset class. If you want to make them manually follow this format. You'll also need to create the xsd schema file as well.

Personally I find it much easier to let VS build those files for you though you can do it yourself.

Hope I helped

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Thanks Zarcom

So would I be right in doing something like this if I wanted to create my typed dataset by hand coding:

dsDetails.WriteXmlSchema(Server.MapPath("dsDetails.xsd"))

So i would put this line after I had made the dataset. However Im not too sure if I would want this code running everytime the page loads as would it take up time?

If I use this method does this make my dataset strongly typed, I have noticed that it does not create a .vb file but only the .xsx

Should I not use this method? its just I have started building an application which is all hand code and does not use VS to make the datasets. And I wanted to make sure all the datasets were typed or do i not really need to bother? As you can probably tell its my first app and just thought I should get into a good habit rather than a bad one.

Thanks in advance
 
I think that it is a good habit to use typed datasets. It keeps you from making silly index or spelling mistakes.

However, you can't create a typed dataset at run time. Remember it is a class and there isn't much point of having code that writes a class then other code that would try to use it. You can use the WriteXMLSchema method to get the xml schema which you can then base your class off of but you would probably use it within a helper app that you could build to help you create the datasets.

Like I said before I recomend letting the IDE make the datasets for you. If you want to do it by hand to understand what is happening in the background I commend you but would suggest that you do so just as practice and let VS.NET create the datasets for you appliction.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
So Mark, do you find that using typed datasets adds an extra level of managment though to a project?

For instance, instead of just creating a dataset within my code and filling it with data, I'd have to create a permanent dataset just for the sole purpose of being able to access the values by name instead of index?

I can see this working for a small application, but what if you have a larger app that uses a few hundred datasets? Doesn't the management overhead negate the ease of coding?

Just curious

D'Arcy
 
Well as of yet I haven't found any situation that does have too much overhead associated with it.

All of the apps I have built so far haven't had more than 10 or 12 different datasets. Given I am able to use the same dataset in different situations but it seems to work quite well for me. I just find that with the ease VS creates the dataset, it is well worth the management overhead to not have to worry and double check to make sure that I have spelt the columns properly or that I have referenced the correct index.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Ok, so another question: does this work the same way with DataTables?

For instance: I only need a datatable of information (rarely an entire dataset). Can you create just one datatable in the same fashion, or does this only apply to datasets?

D
 
I suppose you could have a typed dataTable. I have never actually tried this before, though theorectically it may work.

However, I often have a dataset with a single table in it. Like I said when you use the dataset generator tools that come with VS, it makes things really simple. I haven't ran across a DataTable generator so haven't ever used it.

Anywho ;-) I hope that is what your asking D I wasn't quite sure where you were going with this.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Heh, yeah, it does help me. I use data tables more often than data sets. Was just curious if it would work with those (i.e. datasets have that kewl WriteXML() or whatever function, but datatables don't, so it has to be within a dataset to do that...thought maybe typing worked the same way).

:)

D
 
you prolly could write a Strongly typed datatable yourself, but I don't know of anyway to create it with a wizzard or tool.

So bored.. Don't know what to do.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top