| | |
Viewing Data in Datagrid
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2006
Posts: 90
Reputation:
Solved Threads: 0
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:
The SqlCommand statement within the line
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!
Have a look at this code:
VB Syntax (Toggle Plain Text)
Private Sub ViewCustomers(ByVal vRegion As String) conn.Open() Dim SQLComm = New SqlCommand("SELECT * FROM ViewCustomers('" & vRegion & "')", conn) Dim r As SqlDataReader r = SQLComm.ExecuteReader() dgCustomers.DataSource = r conn.Close() 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!
•
•
Join Date: Jul 2007
Posts: 276
Reputation:
Solved Threads: 37
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
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
•
•
Join Date: Jul 2007
Posts: 276
Reputation:
Solved Threads: 37
Did you happen to see this code in your searching?
*taken from http://www.vbdotnetheaven.com/Upload...aGridSamp.aspx
VB.NET Syntax (Toggle Plain Text)
Imports System.Data Imports System.Data.OleDb ' some code here Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' create a connection string Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Northwind.mdb" Dim myConnection As OleDbConnection = New OleDbConnection myConnection.ConnectionString = connString ' create a data adapter Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from Customers", myConnection) ' create a new dataset Dim ds As DataSet = New DataSet ' fill dataset da.Fill(ds, "Customers") ' write dataset contents to an xml file by calling WriteXml method ' Attach DataSet to DataGrid DataGrid1.DataSource = ds.DefaultViewManager End Sub End Class
Last edited by rapture; Feb 2nd, 2009 at 4:24 pm.
•
•
Join Date: Sep 2006
Posts: 90
Reputation:
Solved Threads: 0
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:
Here is a look at what I executed:
VB.NET Syntax (Toggle Plain Text)
Private Sub ViewCustomers(ByVal vRegion As String) conn.Open() 'Create Data adapter Dim da As New SqlDataAdapter("SELECT * FROM ViewCustomers('" & vRegion & "')", conn) 'Create and fill dataset Dim ds As New DataSet da.Fill(ds, "Region") dgCustomers.DataSource = ds.DefaultViewManager conn.Close() End Sub
Last edited by Ancient Dragon; Feb 4th, 2009 at 9:34 am. Reason: fixing tags -- me too :)
•
•
Join Date: Jul 2007
Posts: 276
Reputation:
Solved Threads: 37
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)
* from http://www.vb-helper.com/howto_net_datagrid.html
(just in case, here is some sample code for the sqlAdapter)
VB.NET Syntax (Toggle Plain Text)
Private Const SELECT_STRING As String = _ "SELECT * FROM Contacts ORDER BY LastName, FirstName" Private Const CONNECT_STRING As String = _ "Data Source=Bender\NETSDK;Initial " & _ "Catalog=Contacts;User Id=sa" ' The DataSet that holds the data. Private m_DataSet As DataSet ' Load the data. Private Sub Form1_Load(ByVal sender As Object, ByVal e As _ System.EventArgs) Handles MyBase.Load Dim data_adapter As SqlDataAdapter ' Create the SqlDataAdapter. data_adapter = New SqlDataAdapter(SELECT_STRING, _ CONNECT_STRING) ' Map Table to Contacts. data_adapter.TableMappings.Add("Table", "Contacts") ' Fill the DataSet. m_DataSet = New DataSet() data_adapter.Fill(m_DataSet) ' Bind the DataGrid control to the Contacts DataTable. dgContacts.SetDataBinding(m_DataSet, "Contacts") End Sub
Last edited by rapture; Feb 2nd, 2009 at 4:53 pm.
•
•
Join Date: Jul 2007
Posts: 276
Reputation:
Solved Threads: 37
Here is the code I've worked with on page load
VB.NET Syntax (Toggle Plain Text)
'import sql server connection namespace Imports System.Data.SqlClient Public Class WebForm1 Inherits System.Web.UI.Page 'inherit sql server client Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here Dim conITRequest As SqlConnection Dim cmdSelect As SqlCommand Dim dtrRequests As SqlDataReader lblError.Text = "" Try 'Create new connection object conITRequest = New SqlConnection 'define connection string to connect to database conITRequest.ConnectionString = "Server=SERVERNAME;UID=ITRequestUser;PWD=PASSWORD;Database=ITRequest" 'create command object to get data from database cmdSelect = New SqlCommand 'populate command text for command to issue cmdSelect.CommandText = "select RequestID, RequestUser , RequestDate , RequestName , PriorityID , DateDue , StatusID , PercentComplete , AssignedUser, ClosedDate , TimeSpent from Request" 'assign connection to Command Object cmdSelect.Connection = conITRequest 'open database connection conITRequest.Open() 'get data from database using command object into a datareader dtrRequests = cmdSelect.ExecuteReader() 'set datasource on grid - this associates the datasource with the grid dgrdRequest.DataSource = dtrRequests 'bind to datagrid - the databind function is what actually ties the data to the grid dgrdRequest.DataBind() Catch ex As Exception lblError.Text = ex.Message Finally conITRequest.Close() End Try End Sub End Class
Last edited by rapture; Feb 2nd, 2009 at 4:54 pm.
•
•
Join Date: Sep 2006
Posts: 90
Reputation:
Solved Threads: 0
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:
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....
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:
VB.NET Syntax (Toggle Plain Text)
conn.Open() 'Create Data adapter 'Dim da As New SqlDataAdapter("SELECT * FROM ViewCustomers('" & vRegion & "')", conn) Dim SQLComm = New SqlCommand("SELECT * FROM ViewCustomers('" & vRegion & "')", conn) Dim r As SqlDataReader r = SQLComm.ExecuteReader() While r.Read() MessageBox.Show("ID: " & r(0).ToString & " Name: " & r(1).ToString) End While 'dgCustomers.DataSource = ds.DefaultViewManager 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....
![]() |
Similar Threads
- Search Access Records (VB.NET)
- how to display data from db (VB.NET)
Other Threads in the VB.NET Forum
- Previous Thread: Visual Basic Employee Clock
- Next Thread: Procedure or function insertCashBook has too many arguments specified.
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net .net2008 2005 2008 access account application arithmetic array arrays basic bing button buttons c# center check checkbox code convert crystalreport data database datagrid datagridview date dissertation dissertations dropdownlist excel fade file-dialog filter ftp generatetags google gridview hardcopy images inline input insert intel internet listview mobile monitor ms net networking objects output panel passingparameters picturebox picturebox1 port position print printing problem project read remove save searchbox searchvb.net select serial shutdown soap sorting survey table tcp temperature text textbox timer timespan toolbox trim update user validation vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio2008 web webbrowser winforms wpf year





