Solved

VB.net Select * From a View with two parameters

Posted on 2013-06-24
3
397 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

744 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

14 Experts available now in Live!

Get 1:1 Help Now