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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:33:37+00:00 2026-05-27T07:33:37+00:00

I create a table in SQL Server Management Studio with this code: CREATE TABLE

  • 0

I create a table in SQL Server Management Studio with this code:

CREATE TABLE contact(
ID INT IDENTITY NOT NULL,
FIRSTNAME VARCHAR(100),
LASTNAME VARCHAR(100)
)

and in C# I used this code:

SqlConnection sc = new SqlConnection("Data Source=.\\SQLSERVER; Initial Catalog=BOSS; Integrated Security=TRUE");

SqlDataAdapter sd = new SqlDataAdapter();

sd.InsertCommand = new SqlCommand("INSERT INTO contact VALUES(@ID, @FIRSTNAME, @LASTNAME)");

sd.InsertCommand.Parameters.Add("@ID", SqlDbType.Int).Value = textBox1.Text;
sd.InsertCommand.Parameters.Add("@FIRSTNAME", SqlDbType.VarChar).Value = textBox2.Text;
sd.InsertCommand.Parameters.Add("@LASTNAME", SqlDbType.VarChar).Value = textBox3.Text;

sc.Open();

sd.InsertCommand.ExecuteNonQuery();

sc.Close();

but when I add the values to the database I get the error:

“ExecuteNonQuery: Connection property has not been initialized”

and I fixed it by adding sc to my first insertcommand, but when I run the program I got another error :

An explicit value for the identity column in table ‘contact’ can only
be specified when a column list is used and IDENTITY_INSERT is ON.

  • 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-05-27T07:33:38+00:00Added an answer on May 27, 2026 at 7:33 am

    Do it this way:

    using(SqlConnection sc = new SqlConnection("Data Source=.\\SQLSERVER; Initial Catalog=BOSS; Integrated Security=TRUE"))
    {
    
      using(SqlCommand command = new SqlCommand())
      {
        command.CommandText = "INSERT INTO contact (FirstName, LastName) VALUES(@FIRSTNAME , @LASTNAME"); 
        command.CommandType = CommandType.Text;
        command.Connection = sc;
    
        command.Parameters.AddWithValue("@FIRSTNAME", textBox2.Text); 
        command.Parameters.AddWithValue("@LASTNAME", textBox3.Text); 
    
        sc.Open();
    
        command.ExecuteNonQuery();
    
      }
    
    }
    

    Important things to note:

    1) Set your table up to have the Id column as an identity column and set autoincrement to true. This will automatically generate a numeric id when you insert

    2) You are trying to insert into an identity column – you can’t actually do this unless you enable identity inserts. I wouldn’t bother – just use an autoincrement column and let the database control the id generation step.

    You can generate your table this way:

    CREATE TABLE Contact
     (
     Id int PRIMARY KEY IDENTITY,
     FirstName varchar(100),
     LastName varchar(100)
     )
    

    to get an autoincrementing primary key.

    3) You don’t need the SqlDataAdapter.

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

Sidebar

Related Questions

I have a sql server table: CREATE TABLE [Workflow].[MilestoneDate]( [MilestoneDateId] [int] IDENTITY(1,1) NOT NULL,
Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int
I'm having newsequentialid() learning problems in sql server management studio. Create a table with
Please consider the following SQL Server table and stored procedure. create table customers(cusnum int,
I have a SQL Server 2005 table like this: create table Taxonomy( CategoryId integer
SQl Server 2005: Option: 1 CREATE TABLE #test (customerid, orderdate, field1 INT, field2 INT,
Why does SQL server express 2008 give me this error? CREATE TABLE model (
I use the SQL Management Studio for create database I create a table and
in SQL Server Management Studio (SSMS) 2008 R2 Dev on the table (because I
Is there an easy way to create a table in SQL Server (2005) from

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.