Syntax error converting the varchar value '21, 0' to a column of data type int.

I am trying to run a SP that has an IN statement for selecting records based on a table PK but I keep running into this error?  Any ideas?

Here is my SP:
--------------------------------
CREATE PROCEDURE sp_EmailSent
      @AdminId AS INT,
      @Newsletters AS VarChar(50),
      @NewsletterSubject AS VarChar(100),
      @NewsletterSent AS VarChar(100),
      @Out AS INTEGER OUTPUT
AS
SET NOCOUNT ON
      
      DECLARE @NumSent AS INT
      SELECT @NumSent = COUNT(*) FROM dbo.view_subscribers WHERE NewsletterId IN (@Newsletters)
      INSERT INTO NewslettersSent (NewsletterSubject,NewsletterSent,NumberOfNewslettersSent,AdminId) VALUES (@NewsletterSubject,@NewsletterSent,@NumSent,@AdminId)
      SET @Out = @@IDENTITY

      SELECT CustomerId , EmailAddress, @Out AS SentId   -- use cast to force datatype
         FROM  dbo.view_subscribers  WHERE NewsletterId IN (@Newsletters) AND Bounced<3 AND AdminId=@AdminId
      ORDER BY EmailAddress
GO


Here is the .net code calling the SP
---------------------------------------------------------------
Dim objConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("AliveContactConnectionString").ConnectionString)
Dim cmd As New SqlCommand("sp_EmailSent", objConn)
cmd.Parameters.Add("@AdminId", SqlDbType.Int).Value = Session("AdminId")
cmd.Parameters.Add("@Newsletters", SqlDbType.VarChar).Value = SelectedLists
cmd.Parameters.Add("@NewsletterSubject", SqlDbType.VarChar).Value = EmailSubject.Text.ToString()
cmd.Parameters.Add("@NewsletterSent", SqlDbType.VarChar).Value = DropDownList1.SelectedValue.ToString()
cmd.Parameters.Add("@Out", SqlDbType.Int).Value = 0
cmd.CommandType = CommandType.StoredProcedure

Dim dataAdapter As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dataSet As New DataSet

dataSet = New DataSet
dataAdapter.Fill(dataSet)
objConn.Close()

------------------------------------------------------------------------
Incidentially this sp worked fine before when I was using classic asp?


Thanks for any help!
LVL 2
alivemediaAsked:
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.

alivemediaAuthor Commented:
I forgot to mention SelectedLists comes from a mulitle checkbox list:


Dim i As Object
        Dim SelectedLists As String = ""

        If Newsletters.Items.Count > 1 Then
            For i = 0 To Newsletters.Items.Count - 1
                If Newsletters.Items(i).Selected Then
                    SelectedLists = SelectedLists + Newsletters.Items(i).Value + ", "
                End If
            Next i
        Else
            SelectedLists = Newsletters.Items(0).Value
        End If
        SelectedLists = SelectedLists + "0"
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
create this helper function:

CREATE FUNCTION dbo.ParmsToList (@Parameters varchar(500))
returns @result TABLE (Value varchar(30))
AS  
begin
     DECLARE @TempList table
          (
          Value varchar(30)
          )

     DECLARE @Value varchar(30), @Pos int

     SET @Parameters = LTRIM(RTRIM(@Parameters))+ ','
     SET @Pos = CHARINDEX(',', @Parameters, 1)

     IF REPLACE(@Parameters, ',', '') <> ''
     BEGIN
          WHILE @Pos > 0
          BEGIN
               SET @Value = LTRIM(RTRIM(LEFT(@Parameters, @Pos - 1)))
               IF @Value <> ''
               BEGIN
                    INSERT INTO @TempList (Value) VALUES (@Value) --Use Appropriate conversion
               END
               SET @Parameters = RIGHT(@Parameters, LEN(@Parameters) - @Pos)
               SET @Pos = CHARINDEX(',', @Parameters, 1)

          END
     END    
     INSERT @result
     SELECT value
        FROM @TempList
     RETURN
END    

and use it like this:

CREATE PROCEDURE sp_EmailSent
     @AdminId AS INT,
     @Newsletters AS VarChar(50),
     @NewsletterSubject AS VarChar(100),
     @NewsletterSent AS VarChar(100),
     @Out AS INTEGER OUTPUT
AS
SET NOCOUNT ON
     
     DECLARE @NumSent AS INT
     SELECT @NumSent = COUNT(*) FROM dbo.view_subscribers WHERE NewsletterId IN (@Newsletters)
     INSERT INTO NewslettersSent (NewsletterSubject,NewsletterSent,NumberOfNewslettersSent,AdminId) VALUES (@NewsletterSubject,@NewsletterSent,@NumSent,@AdminId)
     SET @Out = @@IDENTITY

     SELECT CustomerId , EmailAddress, @Out AS SentId   -- use cast to force datatype
        FROM  dbo.view_subscribers  WHERE NewsletterId IN (select value from dbo.ParmsToList (@Newsletters)) AND Bounced<3 AND AdminId=@AdminId
     ORDER BY EmailAddress


http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_21627393.html
0

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
LandyJCommented:
Check the value of the session variable AdminID.  You're sending '21,0' as the value.
0
LandyJCommented:
DOH!  Disregard my post.  I totally missed the " IN (@Newsletters)" part.  Can't use a list that way.

BTW, you'll need angel's "(select value from dbo.ParmsToList (@Newsletters)) " on the "SELECT @NumSent = COUNT(*) FROM dbo.view_subscribers WHERE NewsletterId IN (@Newsletters)" line of code also.





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