Solved

VB.net Select * From a View with two parameters

Posted on 2013-06-24
3
408 Views
Last Modified: 2013-06-24
Hi

I have to populate a DataGridView with a SQL View that has two parameters
The View looks as follows:
Select * From View1 Where Date BETWEEN (@StartDate) AND (@EndDate)

How would I do this in VB.net if I had the following two variables
oStartDate = "12/12/12" and end date = "12/12/13"

Thanks
0
Comment
Question by:murbro
3 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39270584
here an example:

Dim strSQLconnection As String = "Data Source=dbServer;Initial Catalog=testDB;Integrated Security=True"
Dim sqlConnection As New SqlConnection(strSQLconnection)
Dim sqlCommand As New SqlCommand(string.Format("select * from View1 where Date between {0} and {1}", oStartDate ,oEndDate  ), sqlConnection)
sqlConnection.Open()

Dim reader As SqlDataReader = sqlCommand.ExecuteReader()

DataGridView1.DataSource = reader
DataGridView1.DataBind()

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 39270586
Thanks very much
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39270607
PLEASE be careful here...

BETWEEN "12/12/12" and  "12/12/13"
is not exactly one year!

THE safest and most reliable method in sql for an accurate date range is a combination of >= with <
like this

Dim sqlCommand As New SqlCommand(string.Format("select * from View1 where Date >= {0} and Date < {1}", oStartDate ,oEndDate  ), sqlConnection)

for more on this topic please see: "Beware of Between"
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

813 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

15 Experts available now in Live!

Get 1:1 Help Now