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();
Col1 and col2 should not be nullable as they are making composite key.
Change
To
Also change the parameter addition for all parameters to sql procedure
To
or
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.
Also clear the parameter collection in the loop.