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

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

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
louisfrCommented:
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
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
louisfrCommented:
None of your methods instantiate a DataReader. ExecuteReader does in both.
I don't understand the requirement.
Is that homework?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.