In my datagrid my sql statement uses a have clause to get set information and i am having countless errors

My code is as shown below, were am i going wrong

cmd.Connection = con

    Dim da As New SqlDataAdapter("SELECT        MONTH(Head.SalesDate) AS Month, Detail.ProductID, Detail.Qty, SUM (Detail.Qty) AS TotalSold, SUM(Detail.Qty / 30) AS Average FROM            Head INNER JOIN Detail ON Head.HeadID = Detail.HeadID GROUP BY MONTH(Head.SalesDate),Detail.ProductID, Detail.Qty  HAVING MONTH(Head.SalesDate)=  " & TextBox2.Text & "", con)

   Dim dt As New DataTable

    DataGridView1.DataSource = dt

End Sub`
4 Years
Discussion Span
Last Post by M.Waqas Aslam
Featured Replies


The having command takes place after the inital query is complete and you need to referr to the fields by the name in the result. try
HAVING Month = " & TextBox2.Text &"
and it should work. If not post the erorr you are getting.


I'm a little rusty on "having" but I think "having" is used with aggregate functions as in "having SUM(field) > value". I think you
want to use "where" instead of "having".


hey sorry people i was having problem loging in using my details

Wat i am trying to do is be able to select productid qty and date , where the date (month ) is defined by the user
it says

Incorrect syntax near '='.


the where clause wont work it says it is a wrong function

the sql query is correct its just wen i need to define the parameter


i think you have to use the where clause instead of having .just for your information WHERE is used to filter rows. HAVING is usually used to filter rows after performing an aggregation.now in your case you are filtering rows and want to showrecords those fulfil your criteria . you have to use where clause .

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.