Viewing Data in Datagrid

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2006
Posts: 90
Reputation: bajanpoet is an unknown quantity at this point 
Solved Threads: 0
bajanpoet bajanpoet is offline Offline
Junior Poster in Training

Viewing Data in Datagrid

 
0
  #1
Feb 2nd, 2009
I've read up on attaching a dataset to a datagrid and followed all the code snippets I can, but I am still having problems viewing data.

Have a look at this code:

  1. Private Sub ViewCustomers(ByVal vRegion As String)
  2. conn.Open()
  3. Dim SQLComm = New SqlCommand("SELECT * FROM ViewCustomers('" & vRegion & "')", conn)
  4. Dim r As SqlDataReader
  5. r = SQLComm.ExecuteReader()
  6. dgCustomers.DataSource = r
  7. conn.Close()
  8. End Sub

The SqlCommand statement within the line Dim SQLComm = New SqlCommand("SELECT * FROM ViewCustomers('" & vRegion & "')", conn) references a multistatement table function (ViewCustomers(vRegion)) that works when I run the SELECT statement within SQL itself.

I am trying to view the table returned from the function in the datagrid, but it is not updating. What am I missing? I even rewrote the Sub to try to run the code in a dataadapter rather than an SqlCommand, but no results.

Thanks for your help!
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 276
Reputation: rapture has a spectacular aura about rapture has a spectacular aura about 
Solved Threads: 37
rapture rapture is offline Offline
Posting Whiz in Training

Re: Viewing Data in Datagrid

 
0
  #2
Feb 2nd, 2009
Do you bind the data to the datagrid somewhere? I'm newer but have successfully used the datagrid on a previous project. (Although I used a stored procedure to make the SQL call)

I do know I had to bind the info to the datagrid . . .
I actually think it was this thread that got me on the binding
http://www.daniweb.com/forums/thread10004.html
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 90
Reputation: bajanpoet is an unknown quantity at this point 
Solved Threads: 0
bajanpoet bajanpoet is offline Offline
Junior Poster in Training

Re: Viewing Data in Datagrid

 
0
  #3
Feb 2nd, 2009
Thanks Rapture....

I'm reading the suggested thread, so I'm researching data binding for VB.NET as apposed to C#. Keep you posted!
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 90
Reputation: bajanpoet is an unknown quantity at this point 
Solved Threads: 0
bajanpoet bajanpoet is offline Offline
Junior Poster in Training

Re: Viewing Data in Datagrid

 
0
  #4
Feb 2nd, 2009
I'm not able to ascertain how to bind this datagrid... I was trying [icode] dgCustomers.setdatabinding(ds, "region")[\icode] but it says that "SetDatabinding' is not a member of 'System.Windows.Forms.DataGridView'... I'm a little lost
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 276
Reputation: rapture has a spectacular aura about rapture has a spectacular aura about 
Solved Threads: 37
rapture rapture is offline Offline
Posting Whiz in Training

Re: Viewing Data in Datagrid

 
0
  #5
Feb 2nd, 2009
Did you happen to see this code in your searching?

  1.  
  2. Imports System.Data
  3. Imports System.Data.OleDb
  4. ' some code here
  5. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  6. ' create a connection string
  7. Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Northwind.mdb"
  8. Dim myConnection As OleDbConnection = New OleDbConnection
  9. myConnection.ConnectionString = connString
  10. ' create a data adapter
  11. Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from Customers", myConnection)
  12. ' create a new dataset
  13. Dim ds As DataSet = New DataSet
  14. ' fill dataset
  15. da.Fill(ds, "Customers")
  16. ' write dataset contents to an xml file by calling WriteXml method
  17. ' Attach DataSet to DataGrid
  18. DataGrid1.DataSource = ds.DefaultViewManager
  19. End Sub
  20. End Class
*taken from http://www.vbdotnetheaven.com/Upload...aGridSamp.aspx
Last edited by rapture; Feb 2nd, 2009 at 4:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 90
Reputation: bajanpoet is an unknown quantity at this point 
Solved Threads: 0
bajanpoet bajanpoet is offline Offline
Junior Poster in Training

Re: Viewing Data in Datagrid

 
0
  #6
Feb 2nd, 2009
Looks familiar, but I don't think I used this code on this occasion... it does look like I saw it while researching another day, though. The [icode]DataGrid1.DataSource = ds.DefaultViewManager[\icode] didn't seem to work for me when I tried it last. Will try it again and see what happens...
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 90
Reputation: bajanpoet is an unknown quantity at this point 
Solved Threads: 0
bajanpoet bajanpoet is offline Offline
Junior Poster in Training

Re: Viewing Data in Datagrid

 
0
  #7
Feb 2nd, 2009
Tried to run the application and it compiled and started without a problem, but when I choose the radio buttons that would fire the required code, nothing shows up in the datagrid.

Here is a look at what I executed:
  1. Private Sub ViewCustomers(ByVal vRegion As String)
  2. conn.Open()
  3.  
  4. 'Create Data adapter
  5. Dim da As New SqlDataAdapter("SELECT * FROM ViewCustomers('" & vRegion & "')", conn)
  6.  
  7. 'Create and fill dataset
  8. Dim ds As New DataSet
  9. da.Fill(ds, "Region")
  10. dgCustomers.DataSource = ds.DefaultViewManager
  11. conn.Close()
  12. End Sub
Last edited by Ancient Dragon; Feb 4th, 2009 at 9:34 am. Reason: fixing tags -- me too :)
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 276
Reputation: rapture has a spectacular aura about rapture has a spectacular aura about 
Solved Threads: 37
rapture rapture is offline Offline
Posting Whiz in Training

Re: Viewing Data in Datagrid

 
0
  #8
Feb 2nd, 2009
Again, I'm newer to programming so forgive me if I ask a dumb question. First of all, let me apologize as to not seeing that you were using sqlAdapter instead of oleDbDataAdapter. Now, you do have the database connection string formatted properly somewhere right?

(just in case, here is some sample code for the sqlAdapter)
  1. Private Const SELECT_STRING As String = _
  2. "SELECT * FROM Contacts ORDER BY LastName, FirstName"
  3. Private Const CONNECT_STRING As String = _
  4. "Data Source=Bender\NETSDK;Initial " & _
  5. "Catalog=Contacts;User Id=sa"
  6.  
  7. ' The DataSet that holds the data.
  8. Private m_DataSet As DataSet
  9.  
  10. ' Load the data.
  11. Private Sub Form1_Load(ByVal sender As Object, ByVal e As _
  12. System.EventArgs) Handles MyBase.Load
  13. Dim data_adapter As SqlDataAdapter
  14.  
  15. ' Create the SqlDataAdapter.
  16. data_adapter = New SqlDataAdapter(SELECT_STRING, _
  17. CONNECT_STRING)
  18.  
  19. ' Map Table to Contacts.
  20. data_adapter.TableMappings.Add("Table", "Contacts")
  21.  
  22. ' Fill the DataSet.
  23. m_DataSet = New DataSet()
  24. data_adapter.Fill(m_DataSet)
  25.  
  26. ' Bind the DataGrid control to the Contacts DataTable.
  27. dgContacts.SetDataBinding(m_DataSet, "Contacts")
  28. End Sub
* from http://www.vb-helper.com/howto_net_datagrid.html
Last edited by rapture; Feb 2nd, 2009 at 4:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 276
Reputation: rapture has a spectacular aura about rapture has a spectacular aura about 
Solved Threads: 37
rapture rapture is offline Offline
Posting Whiz in Training

Re: Viewing Data in Datagrid

 
0
  #9
Feb 2nd, 2009
Here is the code I've worked with on page load

  1. 'import sql server connection namespace
  2. Imports System.Data.SqlClient
  3. Public Class WebForm1
  4. Inherits System.Web.UI.Page
  5. 'inherit sql server client
  6.  
  7.  
  8.  
  9. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  10. 'Put user code to initialize the page here
  11.  
  12. Dim conITRequest As SqlConnection
  13. Dim cmdSelect As SqlCommand
  14. Dim dtrRequests As SqlDataReader
  15. lblError.Text = ""
  16. Try
  17. 'Create new connection object
  18. conITRequest = New SqlConnection
  19. 'define connection string to connect to database
  20. conITRequest.ConnectionString = "Server=SERVERNAME;UID=ITRequestUser;PWD=PASSWORD;Database=ITRequest"
  21. 'create command object to get data from database
  22. cmdSelect = New SqlCommand
  23. 'populate command text for command to issue
  24. cmdSelect.CommandText = "select RequestID, RequestUser , RequestDate , RequestName , PriorityID , DateDue , StatusID , PercentComplete , AssignedUser, ClosedDate , TimeSpent from Request"
  25. 'assign connection to Command Object
  26. cmdSelect.Connection = conITRequest
  27. 'open database connection
  28. conITRequest.Open()
  29. 'get data from database using command object into a datareader
  30. dtrRequests = cmdSelect.ExecuteReader()
  31. 'set datasource on grid - this associates the datasource with the grid
  32. dgrdRequest.DataSource = dtrRequests
  33. 'bind to datagrid - the databind function is what actually ties the data to the grid
  34. dgrdRequest.DataBind()
  35. Catch ex As Exception
  36. lblError.Text = ex.Message
  37. Finally
  38. conITRequest.Close()
  39. End Try
  40.  
  41. End Sub
  42.  
  43. End Class
Last edited by rapture; Feb 2nd, 2009 at 4:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 90
Reputation: bajanpoet is an unknown quantity at this point 
Solved Threads: 0
bajanpoet bajanpoet is offline Offline
Junior Poster in Training

Re: Viewing Data in Datagrid

 
0
  #10
Feb 2nd, 2009
Yeah the connection is built in a module, so that's why my code only has the conn.open() in this snippet. I call the module in the Form_Load of this form...

To verify that data is being returned, I changed the code to bring up MessageBoxes to show the data that has been read, like this:
  1. conn.Open()
  2.  
  3. 'Create Data adapter
  4. 'Dim da As New SqlDataAdapter("SELECT * FROM ViewCustomers('" & vRegion & "')", conn)
  5. Dim SQLComm = New SqlCommand("SELECT * FROM ViewCustomers('" & vRegion & "')", conn)
  6. Dim r As SqlDataReader
  7. r = SQLComm.ExecuteReader()
  8. While r.Read()
  9. MessageBox.Show("ID: " & r(0).ToString & " Name: " & r(1).ToString)
  10. End While
  11. 'dgCustomers.DataSource = ds.DefaultViewManager
  12. conn.Close()

This shows message boxes with the relevant information in... so I know the function is returning values. It also tests my connection module. It just won't show up in the datagrid....
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC