Solved

VB.net Select * From a View with two parameters

Posted on 2013-06-24
3
412 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

734 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