sql/server select statement - Incorrect syntax near 's'. when I change the index on dropdownlist

I am getting this error when when I change the index on the dropdownlist

 Incorrect syntax near 's'.
Unclosed quotation mark after the character string ') Order by e.dtmEvent'.

Select Year:
<asp:DropDownList ID="ddlYear" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlYear_SelectedIndexChanged">
<asp:ListItem Value="2009">2009</asp:ListItem>
<asp:ListItem Value="2010">2010</asp:ListItem>
<asp:ListItem Value="2011">2011</asp:ListItem>
<asp:ListItem Value="2012">2012</asp:ListItem>
<asp:ListItem Value="2013">2013</asp:ListItem>
<asp:ListItem Value="2014">2014</asp:ListItem>
<asp:ListItem Value="2015">2015</asp:ListItem>

   Protected Sub ddlYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim dtVolumeOrder As New DataTable()
        Dim strSelectCommand As String = "SELECT e.dtmEvent,e.strTitle,r.strName,r.strEmail,u.strRegistrationUserName,u.strBadgeName, r.fltOrderAmount,r.strRegistrationStatusID,r.dtmCreated FROM i2Integration_EventRegv45_Event e,i2Integration_EventRegv45_Registration r, i2Integration_EventRegv45_RegistrationUser u where e.intEventID = r.intEventID and r.intRegistrationID = u.intRegistrationID and r.blnEmailSent=1 and (YEAR(e.dtmEvent) = '" + ddlYear.SelectedValue + "' AND MONTH(e.dtmEvent) = '" + ddlMth.SelectedValue + "' AND e.strTitle = '" + DroplistData.SelectedValue + "') Order by e.dtmEvent"
        Using sqlConn As New SqlConnection(ConfigurationManager.ConnectionStrings("SiteSqlServer").ConnectionString)
            Using adapPatientBills As New SqlDataAdapter(strSelectCommand, sqlConn)
            End Using
        End Using
        'iRowsCount = dtVolumeOrder.Rows.Count - 1
        uxItemDetailGrid.DataSource = dtVolumeOrder
    End Sub
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Well, syntactically, everything looks alright. Is it possible that one of these
+ DroplistData.SelectedValue +
+ ddlMth.SelectedValue +
+ ddlYear.SelectedValue +
is returning a character such as ' " or something along those lines. Have you checked the query results using the filters causing the exception?

      i2Integration_EventRegv45_Event e,
      i2Integration_EventRegv45_Registration r,
      i2Integration_EventRegv45_RegistrationUser u
      e.intEventID = r.intEventID
      and r.intRegistrationID = u.intRegistrationID
      and r.blnEmailSent=1
      AND (
            YEAR(e.dtmEvent) = '" + ddlYear.SelectedValue + "'
            AND MONTH(e.dtmEvent) = '" + ddlMth.SelectedValue + "'
            AND e.strTitle = '" + DroplistData.SelectedValue + "'
Order by
Can you post the asp for the DroplistData and ddlMth controls?

Éric MoreauSenior .Net ConsultantCommented:
you probably have a value with 's in your DroplistData

try this:

ddlMth.SelectedValue + "' AND e.strTitle = '" + DroplistData.SelectedValue.Tostring.replace("'","''") + "') Order by e.dtmEvent"


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
One more thing you oughta try to apply on your .NET code: try using "&" to concatenate instead of "+"

& will perform an implicity cast/conversion if necessary. See code below.

numResult will return the sum of both variables if numeric (3)
txtResult will return the concatenation of both variables, regardless of data type (21)
dim txtResult as string
dim numResult as integer

dim avalue as integer
dim bvalue as integer

bvalue = 1
avalue = 2

txtResult = avalue&bvalue
numResult = avalue+bvalue


Open in new window

westdhAuthor Commented:
Thanks much , I did not notice or even think about the 's deliminting the string.. Many thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.