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

`
con.Open()
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

    da.Fill(dt)
    DataGridView1.DataSource = dt

    cmd.Dispose()
End Sub`

Hello,

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".

Still hard to tell, since he didnt tell us what exactly he expects from the query.

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 '='.

so use WHERE clause (not having).

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 .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.