Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

947 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

20 Experts available now in Live!

Get 1:1 Help Now