Solved

VB.net Problem creating SQL Views with parameters

Posted on 2013-06-24
4
308 Views
Last Modified: 2013-06-24
Hi

I am trying to create a view with two parameters in the following code
but get the error:
SqlDbTypes is not declared
Baz is not declared

What do I need to alter to make this query work? Thanks


    Sub oCreate_View()

        Dim sSQL As String
        sSQL = "SELECT Work_Hours.EmployeeID, Work_Hours.Hours, Work_Hours.Status"
        sSQL = sSQL & " FROM Work_Hours"
        sSQL = sSQL & " WHERE Work_Hours.Date > @sdate And Work_Hours.Date < @edate"
        sSQL = sSQL & " GROUP BY Work_Hours.EmployeeID, Work_Hours.Status"


        Dim connection As New SqlConnection(My.Settings.CS_Setting)
        Dim cmd As New SqlCommand(sSQL, connection)
        cmd.Parameters.Add("@sdate", SqlDbTypes.VarChar, 50).Value = Baz
        cmd.Parameters.Add("@edate", SqlDbTypes.VarChar, 50).Value = Baz

        connection.Open()
        cmd.ExecuteNonQuery()
        connection.Close()

    End Sub
0
Comment
Question by:murbro
  • 2
  • 2
4 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39270615
import the System.Data namespace
and use:
    Sub oCreate_View()

        Dim sSQL As String
        sSQL = "SELECT Work_Hours.EmployeeID, Work_Hours.Hours, Work_Hours.Status"
        sSQL = sSQL & " FROM Work_Hours"
        sSQL = sSQL & " WHERE Work_Hours.Date > @sdate And Work_Hours.Date < @edate"
        sSQL = sSQL & " GROUP BY Work_Hours.EmployeeID, Work_Hours.Status"


        Dim connection As New SqlConnection(My.Settings.CS_Setting)
        Dim cmd As New SqlCommand(sSQL, connection)
        cmd.Parameters.Add("@sdate", System.Data.SqlDbType.VarChar, 50).Value = Baz
        cmd.Parameters.Add("@edate", System.Data.SqlDbType.VarChar, 50).Value = Baz

        connection.Open()
        cmd.ExecuteNonQuery()
        connection.Close()

    End Sub

Open in new window


in regards to Baz, did u forgot to declare it?
u set it as value to the sql parameters, where did it come from?
0
 

Author Comment

by:murbro
ID: 39270632
Sorry. I am confused. I though that this code was to just create the View with parameters.
I don't know why Baz is there
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39270728
instead of Baz, u need to put the start and end date values.
0
 

Author Closing Comment

by:murbro
ID: 39270733
Thanks
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

18 Experts available now in Live!

Get 1:1 Help Now