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

  • SEARCH
  • Home
  • 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 8298429
In Process

The Archive Base Latest Questions

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

I realize that this question has been answered before, and I have looked through

  • 0

I realize that this question has been answered before, and I have looked through the answers, trying to find the error of my ways, but am at a standstill. I have the following stored procedure that should return a unique key.

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER   PROCEDURE [dbo].[newid] @tablename AS CHAR(10), @fieldname AS CHAR(15) = '', @ioffset AS INT, @theID as int = 0 output 
AS
BEGIN
  SET NOCOUNT ON

 DECLARE @value AS INT
 IF @ioffset < 1 SET @ioffset = 1
 IF LEN(@fieldname) > 0
  BEGIN
    UPDATE id SET @value = CONVERT(INT,cvalue)+@ioffset-1, cvalue = CONVERT(CHAR,(CONVERT(INT,cvalue)+@ioffset)) WHERE UPPER(ctablename) = UPPER(@tablename) AND UPPER(cfieldname) = UPPER(@fieldname)
   SELECT @value
  END
 ELSE
  BEGIN
   UPDATE id SET @value = CONVERT(INT,cvalue)+@ioffset-1, cvalue = CONVERT(CHAR,(CONVERT(INT,cvalue)+@ioffset)) WHERE UPPER(ctablename) = UPPER(@tablename)
   SELECT @value 
  End
 SET NOCOUNT OFF
set @theID = @value
END

I am using the following code to try to access the procedure:

using(SqlConnection connection = new SqlConnection(connectionString))
using(SqlCommand command = new SqlCommand("dbo.NewID", connection)
     { CommandType = CommandType.StoredProcedure } )
{
     command.Parameters.Add( new SqlParameter("@tablename",table));
     command.Parameters.Add( new SqlParameter("@fieldname",field));
     command.Parameters.Add( new SqlParameter("@ioffset",1));
     SqlParameter key = new SqlParameter("@theID", SqlDbType.Int);
     key.Direction = ParameterDirection.Output;
     command.Parameters.Add(key);

     connection.Open();
     result = command.ExecuteNonQuery();
     connection.Close();
}

key.Value is coming back empty, and result is -1, insinuating that there was an error running the procedure. If anyone sees what I might be doing wrong, I would greatly appreciate the assistance.

Just a note: I am not able to edit the actual procedure, as there are several classes that already make use of it.

Thank you very much in advance for any assistance.

  • 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-08T15:46:07+00:00Added an answer on June 8, 2026 at 3:46 pm

    RESOLVED

    Thank you to all of the many replies that I got. I took Steve’s advice, and ran the procedure from SQL Management Studio, and ran into problems there as well. After fiddling around with my input a bit, I found that I was using

    String table = "dbo.tablename";

    When I changed this to

    String table = "tablename";

    everything started working as expected.

    This was a stupid mistake on my part, cause by not having a firm enough grasp of SQL. Thank you again everyone for your wonderful suggestions.

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

Sidebar

Related Questions

I realize this question has been answered before but couldn't find an answer that
Now, I realize that this question has been answered before, but I just can't
I realize that this question has been asked 100times but none that I have
I realize that this question has been asked and has been answered here but
I realize this question has been asked many times and I am have looked
I realize that this question has been asked before - but none of the
I realize that this type of question has been asked several times before but
While I realize that this question has been asked once or twice ago but
I'm sure this question has been asked and answered before, but my Google-fu is
I realize this question has been asked and answered in the past, but I'm

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.