Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8308677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:52:46+00:00 2026-06-08T18:52:46+00:00

I have a winforms app that includes the following class method: public aSqlQuery(SqlCommand pSqlCom,

  • 0

I have a winforms app that includes the following class method:

public aSqlQuery(SqlCommand pSqlCom, string pMode = "object", bool pGetID = false)
    {
        try
        {
            string strConnection = aSystem.ConnectionString;
            SqlConnection linkToDB = new SqlConnection(strConnection);
            pSqlCom.Connection = linkToDB;

            switch (pMode)
            {
                case "non query":
                    {
                        linkToDB.Open();
                        pSqlCom.ExecuteNonQuery();
                        if (pGetID == true)
                        {
                            SqlCommand sqlCom = new SqlCommand("SELECT @@IDENTITY;", linkToDB);
                            this.LastID = (int)sqlCom.ExecuteScalar();
                        }
                        linkToDB.Close();
                    }
                    break;

plus other switches

The pSqlCom (SqlCommand) executes fine becuase I can see the data written into the database. However the subsequent “SELECT @@IDENTITY” statement gives an invalid cast error
What am I doing wrong and how can I retrieve the new ID created by SQL within my class method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:52:47+00:00Added an answer on June 8, 2026 at 6:52 pm

    The correct answer it turns out was that the SELECT SCOPE_IDENTITY() statement had to form part of the same SqlCommand as the INSERT statement which preceeded it, but which I had contained in the previous SqlCommand ‘pSqlCom’. Once the SELECT SCOPE_IDENTITY() was incuded as part of pSqlCom the code correctly returned the Identity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small WinForms app that utilizes a BackgroundWorker object to perform a
I have a Winforms app in C# that calls calls a method asynchronously and
I have a winforms app that initially displays a window with two file dialog
I have a Winforms app that has a DataGridView that is databound at runtime.
I have a winforms app that checks user credentials as it starts. If autharization
I have a DataGridView in a Winforms app that has about 1000 rows (unbound)
I have a test winforms app with ThreadExceptionHandler that displays a message box when
I have a winforms (VB 2008) based app that I'm developing and I want
I have a winforms app, that's locking up during a web service request I've
I'm working with a WinForms app. I have an RDLC report that will be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.