binding a datagrid

T

Tonya

Hi,

I wanted to know how i can bind the results of my stored
procedure to a datagrid. all the SProc is doing is a
simple select of 6 columns from 2 tables.

Im not fully sure what to write as my book only gives 1
example. This is what i have attempted:

Dim Conn As New SqlConnection("initial
catalog=TestCaraDBmsde;integrated security=SSPI;persist
security info=False;workstation id=USER;packet size=4096")
Dim cmd2 As New SqlCommand("SPSelAllCaraDetails", Cn)
cmd2.CommandType = CommandType.StoredProcedure

Try
Conn.Open()
Dim reader As SqlDataReader = cmd2.ExecuteReader()
DGridCaraSel.SetDataBinding(reader.?????)

Catch ex As Exception
MsgBox(ex.Message)
Finally
Conn.Close()
End Try
End Sub

any help is appreciated
 
W

William \(Bill\) Vaughn

Is this a Web form app (ASP) or Windows form app? If the latter, you can't
bind to a DataReader. I would use the Fill method to execute the SP and bind
to the resultant DataTable.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
T

Tonya

I am building a windows appliaction.
Does this mean i have to use unconnected meathods to fill
the datagrid (i.e. Dataset)??
 
W

William \(Bill\) Vaughn

Windows does not support binding to a DataReader so, yes, you'll need to
bind to a DataTable. The DataReader is not any more "connected" than the
DataTable--it simply exposes a data stream from the query. The DataAdapter
Fill also uses a DataReader, but does so behind the scenes.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
T

Tonya

This is where im getting confused.... i read in a book
that the datatable just represents the results of the
query that is run against the database at a certain time
and that it is not the database itself. Am i right in
saying this?

Also, if i updated the datatable, the underlying database
is not directly changed until the changes are pushed from
the datatable to the database via a dataadpater. Is this
correct?

Is there no way of connecting the database directly to
the datagrid, so that any updates that are made to the
databse can be instantly reflected in the datagrid? I do
not want the data in my datatable to be 'out of date'.

Can i use a datatable without a dataset?

Is there any code examples for binding a datagrid to a
datatable?? The book i have only shows a asp.net project
and not a windows app.

Sorry for the mass of questions. Im just starting to
learn all this and am having a hard time gettin my head
around it all.


Thx for your time.
 
W

William \(Bill\) Vaughn

See >>> inline comments.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

Tonya said:
This is where im getting confused.... i read in a book
that the datatable just represents the results of the
query that is run against the database at a certain time
and that it is not the database itself. Am i right in
saying this?
write to the actual files managed by the database. In all cases you simply
get to see a copy of the data as it exists at a given point in time.
Also, if i updated the datatable, the underlying database
is not directly changed until the changes are pushed from
the datatable to the database via a dataadpater. Is this
correct?
architecture is not unique to ADO.NET, but it does not support a more direct
"server-side cursor" approach that passes through changes as they are made.
Sure, you can execute the Update method after each change, but this is
inefficient as it costs an expensive round trip to do so.
Is there no way of connecting the database directly to
the datagrid, so that any updates that are made to the
databse can be instantly reflected in the datagrid? I do
not want the data in my datatable to be 'out of date'.
access to the data. It can only be accessed through the relational SQL
Server (or whatever) engine. However, when you sense a change (in your
code), you can execute the Update to post the changes and make the database
table(s) reflect what the client sees. However, unless you use some more
sophisticated coding, the server has no way to tell you when the rows you
are looking at have changed. There are some alternatives. One is
"Notification Services" which can inform you that a table has changed.
Another is the Whidbey/Yukon release which will have the ability to tell you
when a rowset has changed on the server--but we won't see that released for
some time. That said, I often use a timer in my applications to poll the
server to see if changes have been made (that are interesting to my app).
When the timer goes off, I query a "delta" table that is updated by a
server-side trigger that fires when there's activity on the target table(s).
I use this approach as it's faster than requerying the rows.
 
T

Tonya

Thx for your help William, It has definatly cleared up
alot of confusion.

Can you tell me what book you wrote? i definatly need to
purchase some more books on this and the examples would
be useful. Is your book ok for a beginner?

thx once again
 
W

William \(Bill\) Vaughn

I'm glad I can help. I have written lots of books--all are shown on my
website www.betav.com. Sure, some beginners can dig right in--others might
struggle at first, but there are lots of easy-to-read examples.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top