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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:41:10+00:00 2026-06-06T03:41:10+00:00

i have a composite key( col1, col2 ) in my table i get the

  • 0

i have a composite key(col1, col2) in my table
i get the “cannot insert null value to primary key” error on col1 but actually im not trying to insert null value, i’ve checked in debug mode, the value is not null
any ideas?

this is my stored procedure:

CREATE PROCEDURE [dbo].[proc1]
@col1 char(11) = NULL,
@col2 nchar(50) = NULL,
@col3 real = NULL
AS
BEGIN
SET NOCOUNT ON;

-- Insert statements for procedure here
update dbo.table1 set col2 = @col2 where col1 = @col1 AND col3 = @col3
if @@ROWCOUNT=0
insert into dbo.table1 ( col1, col2, col3 ) values ( @col1, @col2, @col3 )

this is my code:

SqlConnection conn = create_conn();
conn.Open();
SqlCommand command = new SqlCommand("another_proc", conn);
command.Parameters.Add("col1", SqlDbType.Char).Value = col1;
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);

command = new SqlCommand("proc1", conn);

// here i set the value of the parameter
**command.Parameters.Add("col1", SqlDbType.Char).Value = col1;**

command.Parameters.Add("col2", SqlDbType.NChar);
command.Parameters.Add("col3", SqlDbType.Real);
double y, yk, ym, yy;
int k, m;
string col2;
foreach (DataRow dr in ds.Tables[0].Rows)
{
    col2 = dr["col2"].ToString().Trim();
    yk = Double.Parse(dr["yk"].ToString());
    ym = Double.Parse(dr["ym"].ToString());
    yy = Double.Parse(dr["yy"].ToString());
    k = Int32.Parse(dr["k"].ToString());
    m = Int32.Parse(dr["m"].ToString());
    y = Double.Parse(dr["y"].ToString());
    double col3 = ...some calculation here...;
    command.Parameters["col2"].Value = col2;
    command.Parameters["col3"].Value = col3;
    command.ExecuteNonQuery();
}
conn.Close();

updated code:

SqlConnection conn = create_conn();
conn.Open();
SqlCommand command = new SqlCommand("another_proc", conn);
command.Parameters.Add("col1", SqlDbType.Char).Value = col1;
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);

command.CommandText = "proc1";
command.Parameters.Add("col2", SqlDbType.NChar);
command.Parameters.Add("col3", SqlDbType.Real);
double y, yk, ym, yy;
int k, m;
string col2;
foreach (DataRow dr in ds.Tables[0].Rows)
{
    col2 = dr["col2"].ToString().Trim();
    yk = Double.Parse(dr["yk"].ToString());
    ym = Double.Parse(dr["ym"].ToString());
    yy = Double.Parse(dr["yy"].ToString());
    k = Int32.Parse(dr["k"].ToString());
    m = Int32.Parse(dr["m"].ToString());
    y = Double.Parse(dr["y"].ToString());
    double col3 = k * (yk / 100) + m * (ym / 100) + y * (yy / 100);
    command.Parameters["col2"].Value = col2;
    command.Parameters["col3"].Value = col3;
    command.ExecuteNonQuery();
}
conn.Close();
  • 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-06T03:41:11+00:00Added an answer on June 6, 2026 at 3:41 am

    Col1 and col2 should not be nullable as they are making composite key.
    Change

    @col1 char(11) = NULL,
    @col2 nchar(50) = NULL,
    

    To

    @col1 char(11) ,
    @col2 nchar(50),
    

    Also change the parameter addition for all parameters to sql procedure

    command.Parameters.Add("col1", SqlDbType.Char).Value = col1;
    

    To

    command.Parameters.Add("@col1", SqlDbType.Char).Value = col1;
    

    or

     command.Parameters.Add("@col1", SqlDbType.Char);
     command.Parameters["@col1"].Value = col1;
    

    EDIT

    The error “Procedure or function ‘proc1’ expects parameter ‘@col1’, which was not supplied.” you got was misleading, The problem was due to not setting of CommandType. Create new command and add the statement below before you execute command.

    SqlCommand command = new SqlCommand("proc1", conn);
    command1.CommandType = CommandType.StoredProcedure;
    

    Also clear the parameter collection in the loop.

    for (int i = 0; i < 10; i++)
    {
       col1 = "Col1 value " + i;
       col2 = "Col2 value " + i;
       col3 = "Col3 value" + i;
    
       command.Parameters.Clear();
    
       command.Parameters.Add("@col1", SqlDbType.VarChar).Value = col1;
       command.Parameters.Add("@col2", SqlDbType.VarChar).Value = col2;
       command.Parameters.Add("@col3", SqlDbType.VarChar).Value = col3;
    
       command1.ExecuteNonQuery();
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a composite primary key for the site table defined below. Functionally, this
I have a table with composite primary key. I am trying to load data
I have a composite primary key in 1 table in oracle. I want to
I have a composite primary key table (Maintenance Items). I would like to create
How to identify composite primary key in any Mysql Database table? or EDIT 2
I have created a composite key, and it is working, but ideals I would
I have a table that has a composite key that consists of two int
I have a composite primary key for my object.How can i use a jpa
I have one table with composite key attribute.. For that i have made 2
Is it better to have a single primary key, or use composite primary keys

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.