Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Must declare the scalar variable error::PleaseHelp

Must declare the scalar variable error::PleaseHelp

Scheduled Pinned Locked Moved Database
helpcsharpdatabasevisual-studiosecurity
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R rzvme

    public DataSet Get_Friends_Birthday(DateTime d) { User=0; DataSet ds = new DataSet(); SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + '"' + @"C:\Users\LadyLara\Documents\Visual Studio 2008\Projects\Private Notebook WebService\Private Notebook WebService\App_Data\Private Notebook Database.mdf" + '"' + ";Integrated Security=True;User Instance=True"); string scom = "SELECT FriendID, FFirstName, FLastName, FBirthday, FE_mail1, FE_mail2, FE_mail3, FE_mail4, FPhone_Number1, FPhone_Number2, FPhone_Number3, FPhone_Number4, UserID FROM Friends WHERE (UserID = "+"@IDUS)"+" AND (FBirthday = "+"@Date)"; SqlCommand sqlcom = new SqlCommand(scom,con); sqlcom.Parameters.Add("IDUS", SqlDbType.Int).Value=User.ToString(); sqlcom.Parameters.Add("DATE", SqlDbType.SmallDateTime).Value=d.ToString(); SqlDataAdapter adapt = new SqlDataAdapter(scom, con); con.Open(); adapt.Fill(ds); return ds; } when it reaches adapt.fill(ds) i get the folowing error. "System.Data.SqlClient.SqlException: Must declare the scalar variable "@IDUS"." Please Help!

    rzvme

    P Offline
    P Offline
    pmarfleet
    wrote on last edited by
    #2

    rzvme wrote:

    sqlcom.Parameters.Add("IDUS", SqlDbType.Int).Value=User.ToString(); sqlcom.Parameters.Add("DATE", SqlDbType.SmallDateTime).Value=d.ToString();

    Prefix your parameter names with @ symbols. BTW, you should consider not using inline SQL statements. Use stored procedures instead.

    Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

    R 1 Reply Last reply
    0
    • P pmarfleet

      rzvme wrote:

      sqlcom.Parameters.Add("IDUS", SqlDbType.Int).Value=User.ToString(); sqlcom.Parameters.Add("DATE", SqlDbType.SmallDateTime).Value=d.ToString();

      Prefix your parameter names with @ symbols. BTW, you should consider not using inline SQL statements. Use stored procedures instead.

      Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

      R Offline
      R Offline
      rzvme
      wrote on last edited by
      #3

      i thought about using stored procedures only i don't know exactly how to do this.(oracle fan) and by the way....even with the @ symbol in front of the parametername i still get the exact same error.

      rzvme

      P 1 Reply Last reply
      0
      • R rzvme

        i thought about using stored procedures only i don't know exactly how to do this.(oracle fan) and by the way....even with the @ symbol in front of the parametername i still get the exact same error.

        rzvme

        P Offline
        P Offline
        pmarfleet
        wrote on last edited by
        #4

        string scom = "SELECT FriendID, FFirstName, FLastName, FBirthday, FE_mail1, FE_mail2, FE_mail3, FE_mail4, FPhone_Number1, FPhone_Number2, FPhone_Number3, FPhone_Number4, UserID FROM Friends WHERE (UserID = "+"@IDUS)"+" AND (FBirthday = "+"@Date)"; You don't need to do any string concaternation here. You should still really consider using stored procedures instead - you wouldn't have these problems if you did.

        Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

        R P 2 Replies Last reply
        0
        • P pmarfleet

          string scom = "SELECT FriendID, FFirstName, FLastName, FBirthday, FE_mail1, FE_mail2, FE_mail3, FE_mail4, FPhone_Number1, FPhone_Number2, FPhone_Number3, FPhone_Number4, UserID FROM Friends WHERE (UserID = "+"@IDUS)"+" AND (FBirthday = "+"@Date)"; You don't need to do any string concaternation here. You should still really consider using stored procedures instead - you wouldn't have these problems if you did.

          Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

          R Offline
          R Offline
          rzvme
          wrote on last edited by
          #5

          ALTER PROCEDURE [dbo].[uspGetBirthdayFriends] @CRK INT @BDATE SMALLDATETIME AS begin select * from Friends where UserID=@CRK and FBirthday =@BDATE RETURN end same error incorrect sintax near '@BDATE' must declare scalar variable "@crk"

          rzvme

          P 1 Reply Last reply
          0
          • R rzvme

            ALTER PROCEDURE [dbo].[uspGetBirthdayFriends] @CRK INT @BDATE SMALLDATETIME AS begin select * from Friends where UserID=@CRK and FBirthday =@BDATE RETURN end same error incorrect sintax near '@BDATE' must declare scalar variable "@crk"

            rzvme

            P Offline
            P Offline
            pmarfleet
            wrote on last edited by
            #6

            You have made a syntax error. Each parameter declaration should be separated by a comma.

            Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

            R 1 Reply Last reply
            0
            • P pmarfleet

              You have made a syntax error. Each parameter declaration should be separated by a comma.

              Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

              R Offline
              R Offline
              rzvme
              wrote on last edited by
              #7

              Thank you. That worked. But now i get another error and i can't seem to determine the cause after i invoke adapt.Fill(ds); /*This is the code of the procedure*/ ALTER PROCEDURE dbo.uspGetBirthdayFriends @CRK INT, @BDATE SMALLDATETIME AS begin declare @ID int declare @BDATE smalldatetime select * from Friends where UserID=@ID and FBirthday =@BDATE end RETURN i get the following error "Procedure uspGetBirthdayFriends has no parameters and arguments were supplied." Thank you

              rzvme

              P P 2 Replies Last reply
              0
              • R rzvme

                Thank you. That worked. But now i get another error and i can't seem to determine the cause after i invoke adapt.Fill(ds); /*This is the code of the procedure*/ ALTER PROCEDURE dbo.uspGetBirthdayFriends @CRK INT, @BDATE SMALLDATETIME AS begin declare @ID int declare @BDATE smalldatetime select * from Friends where UserID=@ID and FBirthday =@BDATE end RETURN i get the following error "Procedure uspGetBirthdayFriends has no parameters and arguments were supplied." Thank you

                rzvme

                P Offline
                P Offline
                pmarfleet
                wrote on last edited by
                #8

                You've declared @BDATE twice in your SP - once as a parameter and secondly as a variable. This code won't complile - remove your variable declaration.

                Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

                1 Reply Last reply
                0
                • R rzvme

                  Thank you. That worked. But now i get another error and i can't seem to determine the cause after i invoke adapt.Fill(ds); /*This is the code of the procedure*/ ALTER PROCEDURE dbo.uspGetBirthdayFriends @CRK INT, @BDATE SMALLDATETIME AS begin declare @ID int declare @BDATE smalldatetime select * from Friends where UserID=@ID and FBirthday =@BDATE end RETURN i get the following error "Procedure uspGetBirthdayFriends has no parameters and arguments were supplied." Thank you

                  rzvme

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #9

                  You should really pass @ID in. Change it to:

                  dbo.uspGetBirthdayFriends
                  
                  
                  @ID INT, 
                  @BDATE SMALLDATETIME
                  
                  AS
                  begin
                  select * from Friends where UserID=@ID and FBirthday =@BDATE
                  end
                  RETURN
                  

                  From what I saw of your earlier post, you also seem to have a problem with the way you are handling your parameter. When you add in the SqlParameter, you need to put the parameter in with an @ in front of the name, e.g. command.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));

                  Deja View - the feeling that you've seen this post before.

                  My blog | My articles

                  R 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    You should really pass @ID in. Change it to:

                    dbo.uspGetBirthdayFriends
                    
                    
                    @ID INT, 
                    @BDATE SMALLDATETIME
                    
                    AS
                    begin
                    select * from Friends where UserID=@ID and FBirthday =@BDATE
                    end
                    RETURN
                    

                    From what I saw of your earlier post, you also seem to have a problem with the way you are handling your parameter. When you add in the SqlParameter, you need to put the parameter in with an @ in front of the name, e.g. command.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));

                    Deja View - the feeling that you've seen this post before.

                    My blog | My articles

                    R Offline
                    R Offline
                    rzvme
                    wrote on last edited by
                    #10

                    Thank you very much. Both of you. I finaly solved the problem.

                    rzvme

                    1 Reply Last reply
                    0
                    • P pmarfleet

                      string scom = "SELECT FriendID, FFirstName, FLastName, FBirthday, FE_mail1, FE_mail2, FE_mail3, FE_mail4, FPhone_Number1, FPhone_Number2, FPhone_Number3, FPhone_Number4, UserID FROM Friends WHERE (UserID = "+"@IDUS)"+" AND (FBirthday = "+"@Date)"; You don't need to do any string concaternation here. You should still really consider using stored procedures instead - you wouldn't have these problems if you did.

                      Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

                      P Offline
                      P Offline
                      Paul Conrad
                      wrote on last edited by
                      #11

                      pmarfleet wrote:

                      You don't need to do any string concaternation here.

                      Yes, that is dangerous.

                      "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups