?
Solved

vb.net, make code function without data reader, possible?

Posted on 2014-10-09
5
Medium Priority
?
208 Views
Last Modified: 2014-10-24
Method 1 below uses data reader and functions okay.

Q1: Is it possible to change this code function without use of data reader (see method 2 below; this method is not working yet)?

Q2: Does datareader offer some advantages such as formatting the gridview cells, etc?

-------------Method 1 using DataReader ------------------------

Dim cnn As SqlConnection = New SqlConnection
Dim strCnn As String = "Data Source=.\SQLEXPRESS12; Database=Northwind;Integrated Security=SSPI"

cnn.ConnectionString = strCnn

Dim cmd As SqlCommand = New SqlCommand("Select * From Products", cnn)
Dim source As New BindingSource

cnn.Open()

Dim dr As SqlDataReader = cmd.ExecuteReader
source.DataSource = dr
Me.DataGridView1.DataSource = source

cnn.Close()


-------------Method 2 without use of DataReader ------------------------

Dim cnn As SqlConnection = New SqlConnection
Dim strCnn As String = "Data Source=.\SQLEXPRESS12; Database=Northwind;Integrated Security=SSPI"

cnn.ConnectionString = strCnn

Dim cmd As SqlCommand = New SqlCommand("Select * From Products", cnn)
Dim source As New BindingSource

cnn.Open()

'source.DataSource = cmd.ExecuteReader
Me.DataGridView1.DataSource = cmd.ExecuteReader 'source

cnn.Close()

Open in new window

0
Comment
Question by:Mike Eghtebas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 40372230
Nothing will format the DataGridView for you. It`s your work to define the formatting, either through the designer or through code.

You need something to retrieve the data from the database, and you have 2 choices:

A DataReader, as you are doing, which is usually the best thing to use when the data will not be modified by the user.

A DataTable, which requires more resources and is a bit slower, but that might makes updates easier if the data is modified and the modifications need to be sent back to the database.
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40372251
re:> Nothing will format the DataGridView for you. It`s your work to define the formatting, either through the designer or through code

DataReader makes it possible to cycle through data and apply conditional formatting, for example. Of course we have to decide how to format a gridview; an ado object will not decide on its own how to format a gridview. I may have described this point poorly.

re:> A DataTable, which requires more resources and is a bit slower,
The main objective from posting this question is to learn how complete the  Method 2 giving in the original post. I wonder if you could help me with that.

I am seeing the following code to populate a gridview on a webpage without using reader or data table. Could you possibly give me either c# or vb.net version of it for winform?

String strCnn = "Data Source=.\SQLEXPRESS12; Database=Northwind;Integrated Security=SSPI";
SqlConnection cnn = new SqlConnection(strCnn );
SqlCommand cmd = new SqlCommand("Select * From Products", cnn);
cnn.Open();

GridView1.DataSource = cmd.ExecuteReader;
GridView1.DataBind();
cnn.Close();

Open in new window


This solution is at: https://www.youtube.com/watch?v=Mo0ECWKVVDU and it is not using data table either. If it is possible to have Method 3 (using data table), that will be great to have as well.

Thank you,

Mike
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40372467
Your two methods use a DataReader. The only difference is you don't use a variable to store it, but you're still using ExecuteReader.
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40372849
louisfr,

Can you complete Method 2 to work? It is not working now (the requirement is to avoid instantiating a data reader object. But it is okay to use cmd.ExecuteReader).

Also, could you develop Method 3 where a data table is used?

Thank you,

Mike
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40372953
None of your methods instantiate a DataReader. ExecuteReader does in both.
I don't understand the requirement.
Is that homework?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question