Solved

VB.net Select * From a View with two parameters

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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

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 article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 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

26 Experts available now in Live!

Get 1:1 Help Now