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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:41:33+00:00 2026-06-03T23:41:33+00:00

I am trying to insert Data into my Sql-Server database though C#. I’m Calling

  • 0

I am trying to insert Data into my Sql-Server database though C#. I’m Calling a stored procedure and then would like it to add. I’m not sure what changes to make, but ultimately i would like it done in the stored procedure.

My Stored procedure now:

CREATE PROCEDURE [dbo].[InsertTagProcdure]
       @TagID int, 
       @Value nvarchar(200), 
       @TagCount nvarchar(200) 
AS
IF NOT EXISTS (SELECT NULL FROM Tag
                WHERE @TagID = @TagID)
BEGIN
    INSERT INTO 
        Tag 
        (TagID,Value,TagCount) 
        VALUES 
        (@TagID,@Value,@TagCount)
END

And my C# Code:

int TagID = int.Parse(txtTagID.Text); //This should fall away so auto increment.
            String Value = txtValue.Text;
            int TagCount = int.Parse(txtCount.Text); 

            using (var conn = new SqlConnection(Properties.Settings.Default.DBConnectionString))
            using (var cmd = conn.CreateCommand())
            {
                    conn.Open();
                    cmd.CommandText = "InsertTagProcdure";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@TagID", TagID);
                    cmd.Parameters.AddWithValue("@Value", Value);
                    cmd.Parameters.AddWithValue("@TagCount", TagCount);
                    cmd.ExecuteNonQuery();
                }

The Table Create i used: //Cant change this its what the boss gave me.

CREATE TABLE [dbo].[Tag](
    [TagID] [int] IDENTITY(1,1) NOT NULL,
    [Value] [varchar](200) NOT NULL,
    [TagCount] [varchar](200) NULL,
 CONSTRAINT [PK_Tag] PRIMARY KEY CLUSTERED 
(
    [TagID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
  • 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-03T23:41:34+00:00Added an answer on June 3, 2026 at 11:41 pm

    Ideally you would just make TagID an identity field by changing the table definition. If you can’t do that, next best would be:

    CREATE PROCEDURE [dbo].[InsertTagProcdure]
           @Value nvarchar(200), 
           @TagCount nvarchar(200) 
    AS
    BEGIN
        BEGIN TRANSACTION
            DECLARE @TagID int;
            SELECT @TagID = coalesce((select max(TagID) + 1 from Tag), 1)
        COMMIT      
        INSERT INTO 
            Tag 
            (TagID,Value,TagCount) 
            VALUES 
            (@TagID,@Value,@TagCount)
    END
    

    The transaction ensures that you don’t end up with unique TagIDs and the coalesce handles the special case where the table is empty and gives an initial value of 1.

    EDIT:

    Based on the change to your original question, the table already has an identity column so your stored procedure should be:

    CREATE PROCEDURE [dbo].[InsertTagProcdure]
           @Value nvarchar(200), 
           @TagCount nvarchar(200) 
    AS
    BEGIN
        INSERT INTO Tag (Value,TagCount) VALUES (@Value,@TagCount)
    END
    

    and your C# code should be

    int TagID = int.Parse(txtTagID.Text); //This should fall away so auto increment.
    String Value = txtValue.Text;
    int TagCount = int.Parse(txtCount.Text);

            using (var conn = new SqlConnection(Properties.Settings.Default.DBConnectionString))
            using (var cmd = conn.CreateCommand())
            {
                    conn.Open();
                    cmd.CommandText = "InsertTagProcdure";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Value", Value);
                    cmd.Parameters.AddWithValue("@TagCount", TagCount);
                    cmd.ExecuteNonQuery();
                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to insert some text data into a table in SQL Server
Hello I'm trying to insert some XML data into a table on SQL Server
I'm trying to insert data into a database that I have on my server.
I have a form that is trying to insert some data into an SQL
I'm trying to insert data into the SQLite database using get EditText, but I
I am trying to insert some data into a pysqlite database but even tho
I'm trying to insert some binary data into database using 'mysql' gem in ruby.
I am trying to insert the data from this link to my SQL server
I'm trying to insert an HTML blob into our sql-server2005 database. I've been using
I am trying to generate random data for my sql server database. Ive created

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.