Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP.NET (Non-Forum Related)
 Querying a DataTable in .Net
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

paulnoe
Starting Member

USA
48 Posts

Posted - 24 May 2005 :  13:56:24  Show Profile  Visit paulnoe's Homepage
Hi - I'm new to .Net and need a little help.

I'm using a SQL query to create a DataTable. I'm able to connect to my SQL Server and display the records in a standard loop using a SQLDataReader forward-only recordset.

Now I want to query that same table based on the value of one of the fields, so I believe I need to use a rowfilter on the datatable or something similar.

Do I need to use the DataView or DataAdapter? I'm kind of stuck and any help would be appreciated -

Thanks - Paul

Paul Noe
paul@thenoes.com

pdrg
Support Moderator

United Kingdom
2897 Posts

Posted - 25 May 2005 :  04:51:38  Show Profile  Send pdrg a Yahoo! Message
I think the biggest take-home you can make about working with data in ADO.NET is that is is completely different - it's inside-out from where it used to be...! Worth getting into some good tutorials and searching on www.asp.net

Rather than thinking in terms of iterating through a recordset (ADO style) consider using the ASP.NET databound controls connected to your dataset/datatable. you can then populate the dataset/datatable once only, and use some of the methods (that escape me now) to filter, as you say.

Not teribly helpful, sorry, but once I got my head around ADO.NET, I got a job where I never use it!
Go to Top of Page

paulnoe
Starting Member

USA
48 Posts

Posted - 25 May 2005 :  12:56:43  Show Profile  Visit paulnoe's Homepage
Hey PDRG, thanks for the link. I'll check out the tutorials - it can only help!

Paul Noe
paul@thenoes.com
Go to Top of Page

pdrg
Support Moderator

United Kingdom
2897 Posts

Posted - 26 May 2005 :  04:35:10  Show Profile  Send pdrg a Yahoo! Message
Yep, they .NET comminity is pretty quiet on this forum, the ASP.NET one is usually buzzing though :)

Good luck!
Go to Top of Page

Ghostnetworks
New Member

95 Posts

Posted - 26 May 2005 :  11:53:35  Show Profile  Visit Ghostnetworks's Homepage
DataAdapter is what you use to fill your DataTable. DataView is what's used to filter, sort etc...
You're better off using a full DataSet than DataTable though. But I guess that depends on your particular application. DataSet has better sort/filter/databind options than just a DataTable

Filling a DataSet :

Dim myDataSet As DataSet
Dim myConn As SqlConnection
Dim myDataAdapter As SqlDataAdapter
Dim myView As DataView

'myConn = Your connection here

myDataAdapter = New SqlDataAdapter ("SELECT myField1, myField2 FROM myTable", myConn)
myDataSet = New DataSet()

'Single line to fill the dataset. Very simple.
myDataAdapter.Fill(myDataSet)

'Now you can use whatever DataView you like
myView = myDataSet.Tables("myTable").DefaultView
myView.RowFilter = "myField2 = 'SpecificValue'" 'Or some other SQL
myView.Sort = "myField1 DESC"

'Now you can apply it to a DataGrid and display it on your page.
'No mess, no fuss ;)

Dim myDataGrid As DataGrid
myDataGrid.DataSource = DataView

'***Very important. 
'You're applying the DataView to the DataGrid, not the DataAdapter.

'Now just bind it
myDataGrid.DataBind()


The advantage is that you have a ready-made table you can play around with in a DataGrid.
Plus at a later date, you can even add in specific sort options etc directly to the DataGrid itself.
There are no "Response.Write()" lines you have to mess with.

Sorry about the rough code, it's just some on-the-spot stuff.
If you need any detailed examples, check out the Microsoft QuickStart Tutorials

Go to Top of Page

paulnoe
Starting Member

USA
48 Posts

Posted - 14 June 2005 :  13:52:33  Show Profile  Visit paulnoe's Homepage
Thanks Ghost - sorry it took so long to get back to this - I appreciate your help! Paul

Paul Noe
paul@thenoes.com
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000 Version 3.4.07