Solved

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

Posted on 2014-10-09
5
198 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
  • 2
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 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 33

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 33

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

816 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now