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. Getting InvalidCastException using VS2005 TableAdapter generator to call a stored procedure

Getting InvalidCastException using VS2005 TableAdapter generator to call a stored procedure

Scheduled Pinned Locked Moved Database
databasequestionworkspace
3 Posts 2 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.
  • Brian C HartB Offline
    Brian C HartB Offline
    Brian C Hart
    wrote on last edited by
    #1

    I have a stored procedure defined thusly -- actual variable, database, and table names have been altered for NDA reasons:

    CREATE PROCEDURE [dbo].[CountUniqueBirthdays]
    @PersonID bigint
    AS

    BEGIN

    SET NOCOUNT ON;

    DECLARE @CalendarDaysToSearch int

    SET @CalendarDaysToSearch = 90

    SELECT  COUNT(DISTINCT o.Birthdays) 
    	FROM
    		\[MYDATABASE\].\[dbo\].\[PersonInfo\] n,
    		\[MYDATABASE\].\[dbo\].\[PersonInfo\] o
    	WHERE
    		n.PersonID = @PersonID AND
    		--n.PersonID <> o.PersonID AND
    
    		-- all leads with...
    		n.KeyPerson=o.KeyPersonAND
    		
    		-- within X days of current lead
    		o.CalendarDate between dateadd(dd, (-1 \* @CalendarDaysToSearch ), n.CalendarDate ) and dateadd(dd, 0, n.CalendarDate ) 
    

    END

    For the sake of argument, assume this stored procedure is written correctly and is known to work. Now, I opened up VS2005 (my boss wants me to use it) and in the Data Sources window, I did the whole Add New Data Source/Data Source Configuration Wizard yada yada and added a TableAdapter to my DataSet for PersonInfo (not the real table name again). I configured the TableAdapterto do a SELECT PersonId FROM PersonInfo and then also to call the Stored Procedure, CountUniqueBirthdays, above. ok, so i write the code to grab the records containing only the PersonIDcolumn and then I iterate over the column, passing each ID into the stored procedure one by one to check if the number of distinct persons with birthdays in a certain 90-day period is bigger than 1 -- AND DON'T ASK ME WHY DID I NOT JUST USE A CURSOR, CURSORS ARE SLOW AND WE HATE THEM, as below. You know how you can open .xsd files in the Dataset Designer in VS2005? Well, I did, right-clicked the PersonInfo TableAdapter, clicked Add > New Query... and in the Add New Query Wizard I picked Existing stored procedure, and then configured CountUniqueBirthdays as the stored procedure to use. try { Console.WriteLine("Querying the MYDATABASE.dbo.PersonInfo table...Please wait."); MYDATABASEDataSetTableAdapters.PersonInfoTableAdapter adapter = new MYDATABASEDataSetTableAdapters.PersonInfoTableAdapter(); MYDATABASEDataSet.PersonInfoDataTable table = adapter.GetData(); Console.WriteLine("Finished getting data."); Console.WriteLine("Running the CountUniqueBirthdays stored procdure. Searc

    J 1 Reply Last reply
    0
    • Brian C HartB Brian C Hart

      I have a stored procedure defined thusly -- actual variable, database, and table names have been altered for NDA reasons:

      CREATE PROCEDURE [dbo].[CountUniqueBirthdays]
      @PersonID bigint
      AS

      BEGIN

      SET NOCOUNT ON;

      DECLARE @CalendarDaysToSearch int

      SET @CalendarDaysToSearch = 90

      SELECT  COUNT(DISTINCT o.Birthdays) 
      	FROM
      		\[MYDATABASE\].\[dbo\].\[PersonInfo\] n,
      		\[MYDATABASE\].\[dbo\].\[PersonInfo\] o
      	WHERE
      		n.PersonID = @PersonID AND
      		--n.PersonID <> o.PersonID AND
      
      		-- all leads with...
      		n.KeyPerson=o.KeyPersonAND
      		
      		-- within X days of current lead
      		o.CalendarDate between dateadd(dd, (-1 \* @CalendarDaysToSearch ), n.CalendarDate ) and dateadd(dd, 0, n.CalendarDate ) 
      

      END

      For the sake of argument, assume this stored procedure is written correctly and is known to work. Now, I opened up VS2005 (my boss wants me to use it) and in the Data Sources window, I did the whole Add New Data Source/Data Source Configuration Wizard yada yada and added a TableAdapter to my DataSet for PersonInfo (not the real table name again). I configured the TableAdapterto do a SELECT PersonId FROM PersonInfo and then also to call the Stored Procedure, CountUniqueBirthdays, above. ok, so i write the code to grab the records containing only the PersonIDcolumn and then I iterate over the column, passing each ID into the stored procedure one by one to check if the number of distinct persons with birthdays in a certain 90-day period is bigger than 1 -- AND DON'T ASK ME WHY DID I NOT JUST USE A CURSOR, CURSORS ARE SLOW AND WE HATE THEM, as below. You know how you can open .xsd files in the Dataset Designer in VS2005? Well, I did, right-clicked the PersonInfo TableAdapter, clicked Add > New Query... and in the Add New Query Wizard I picked Existing stored procedure, and then configured CountUniqueBirthdays as the stored procedure to use. try { Console.WriteLine("Querying the MYDATABASE.dbo.PersonInfo table...Please wait."); MYDATABASEDataSetTableAdapters.PersonInfoTableAdapter adapter = new MYDATABASEDataSetTableAdapters.PersonInfoTableAdapter(); MYDATABASEDataSet.PersonInfoDataTable table = adapter.GetData(); Console.WriteLine("Finished getting data."); Console.WriteLine("Running the CountUniqueBirthdays stored procdure. Searc

      J Offline
      J Offline
      John Gathogo
      wrote on last edited by
      #2

      The return type for CountUniqueBirthdays may not be long (or long? for that matter). Have you tried [int]? Or just receive it into an [object] variable and use Watch to see what it contains? i.e.

      object value = adapter.CountUniqueBirthdays(leadID);

      Brian C HartB 1 Reply Last reply
      0
      • J John Gathogo

        The return type for CountUniqueBirthdays may not be long (or long? for that matter). Have you tried [int]? Or just receive it into an [object] variable and use Watch to see what it contains? i.e.

        object value = adapter.CountUniqueBirthdays(leadID);

        Brian C HartB Offline
        Brian C HartB Offline
        Brian C Hart
        wrote on last edited by
        #3

        Thank you for your help. Yes, actually the IntelliSense was off and doing a Build Solution fixed it so that the IntelliSense told me the return type was object, and I was off and running! :D Brian

        Sincerely Yours, Brian Hart

        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