Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

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

dropdownlist
==============================================================
Select Year:
<asp:DropDownList ID="ddlYear" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlYear_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
<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>
</asp:DropDownList>

   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)
                adapPatientBills.Fill(dtVolumeOrder)
            End Using
        End Using
        'iRowsCount = dtVolumeOrder.Rows.Count - 1
        uxItemDetailGrid.DataSource = dtVolumeOrder
        uxItemDetailGrid.DataBind()
    End Sub
0
westdh
Asked:
westdh
1 Solution
 
myrotarycarCommented:
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?

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
0
 
Allister_ReidCommented:
Hi,
Can you post the asp for the DroplistData and ddlMth controls?


Thanks
0
 
É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"

0
 
myrotarycarCommented:
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

msgbox(numResult)
msgbox(txtResult)

Open in new window

0
 
westdhAuthor Commented:
Thanks much , I did not notice or even think about the 's deliminting the string.. Many thanks
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now