Solved

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

Posted on 2014-10-09
5
193 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

18 Experts available now in Live!

Get 1:1 Help Now