Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.net Select * From a View with two parameters

Posted on 2013-06-24
3
Medium Priority
?
415 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:Murray Brown
[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:
Meir Rivkin earned 2000 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:Murray Brown
ID: 39270586
Thanks very much
0
 
LVL 49

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

610 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