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 103037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:03:37+00:00 2026-05-11T01:03:37+00:00

(resolved: see bottom) I have the following code snippet: Protected Sub SqlDataSource1_Inserted(ByVal sender As

  • 0

(resolved: see bottom)

I have the following code snippet:

Protected Sub SqlDataSource1_Inserted(ByVal sender As Object,  ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Inserted     affected = CInt(DirectCast(e.Command.Parameters('@affected'), IDbDataParameter).Value)     newID = CInt(DirectCast(e.Command.Parameters('@newID'), IDbDataParameter).Value) End Sub 

Where @newID is defined like this in the SQL string:

'INSERT INTO x(a,b,c) VALUES (@a,@b,@c); SELECT @affected = @@rowcount, @newID = SCOPE_IDENTITY(); 

The parameters are defined using ASP.NET as follows:

The strange thing about it is that this works 90% of the time, but every once and a while it throws an InvalidCastException saying that ‘Conversion from type ‘DBNull’ to type ‘Integer’ is not valid.’ Any ideas on what could be causing this value to be null? I don’t have any triggers set on the table, and the only thing my query is doing is running a plain insert into 1 table.

Edit: Based on the suggestions here, I added an affected parameter. I set a breakpoint, and affected = 1 but I still got the exception. However, I then figured out that I had SELECT @newID before SELECT @affected. I switched the order, and now @affected = 0. So it appears to be a problem with my insert statement after all. Thanks for your help!

  • 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. 2026-05-11T01:03:37+00:00Added an answer on May 11, 2026 at 1:03 am

    From MSDN

    After an INSERT, SELECT INTO, or bulk copy statement is completed, @@IDENTITY contains the last identity value that is generated by the statement. If the statement did not affect any tables with identity columns, @@IDENTITY returns NULL.

    Do you have a check to see if the insert has succeeded? It may be possible that the record you are trying to insert fails to insert anything for some reason, therefore the id is null.

    Are there any triggers set up on the table? You may be retrieving the id from a trigger procedure using @@IDENTITY

    I would suggest either using SCOPE_IDENTITY() or an output parameter to get the id

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

Sidebar

Related Questions

RESOLVED From the developer: the problem was that a previous version of the code
I recently resolved an issue my VB6 application had when saving large binary objects
This is a perennial question for me that I've never really resolved so I'd
How do you resolve a domain name to an IP address with .NET/C#?
How do you resolve an NT style device path, e.g. \Device\CdRom0 , to its
I am trying to resolve Euler Problem 18 -> http://projecteuler.net/index.php?section=problems&id=18 I am trying to
I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
What is the best way to resolve a conflict when doing a git svn
What can be done to resolve the exception: CrystalDecisions.CrystalReports.Engine.InternalException: Failed to open a rowset.

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.