Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is My Procedure..
SQL
alter PROCEDURE Sp_AssignProjectToUser
(

 @Ref_UserID int =  null
, @Ref_ProjectID int  = null
, @ActiveFlag bit  =null
)
AS
  BEGIN
    INSERT INTO UserMappingProject (Ref_UserID
    , Ref_ProjectID
    , ActiveFlag)
      VALUES (@Ref_UserID, @Ref_ProjectID, @ActiveFlag)
  END


This Is C# code
C#
public int AssignProjectToUser(DataTable dtUserProjectMap, Int32 batchSize)
       {
int Result = 0;
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("Sp_AssignProjectToUser", con);

            cmd.Parameters.AddWithValue("@Ref_UserID", Ref_UserID);
            cmd.Parameters.AddWithValue("@Ref_ProjectID", ProjectID);
            cmd.Parameters.AddWithValue("@ActiveFlag ", flag);
            con.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            Result = cmd.ExecuteNonQuery();
            con.Close();
            return Result;
            SqlDataAdapter adapter = new SqlDataAdapter();

            // Set the UPDATE command and parameters.
            adapter.UpdateCommand = new SqlCommand("Sp_AssignProjectToUser", con);
            adapter.UpdateCommand.Parameters.Add("@Ref_UserID", SqlDbType.Int, 5, "Ref_UserID");
            adapter.UpdateCommand.Parameters.Add("@Ref_ProjectID", SqlDbType.Int, 4, "Ref_ProjectID");
            adapter.UpdateCommand.Parameters.Add("@ActiveFlag", SqlDbType.Bit, 1, "ActiveFlag");
            adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
            adapter.UpdateBatchSize = 4;


             Set the INSERT command and parameter.
            adapter.InsertCommand = new SqlCommand("Sp_AssignProjectToUser", con);
            adapter.InsertCommand.Parameters.AddWithValue("@Ref_UserID","Ref_UserID");
            adapter.InsertCommand.Parameters.Add("@Ref_UserID", SqlDbType.Int, 5, "Ref_UserID");
            adapter.InsertCommand.Parameters.Add("@Ref_ProjectID", SqlDbType.Int, 4, "Ref_ProjectID");
            adapter.InsertCommand.Parameters.Add("@ActiveFlag", SqlDbType.Bit, 1, "ActiveFlag");

            adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;

     
            // Set the batch size.
            adapter.UpdateBatchSize = batchSize;

           adapter.Update(dtUserProjectMap);
            return Result;


Showing That Error When I Insert Records.

SQL
Cannot insert the value NULL into column 'Ref_UserID', table 'ZP505_VersionControlTracker.dbo.UserMappingProject'; column does not allow nulls. INSERT fails.
Cannot insert the value NULL into column 'Ref_UserID', table 'ZP505_VersionControlTracker.dbo.UserMappingProject'; column does not allow nulls. INSERT fails.
Posted
Updated 10-Feb-15 23:16pm
v2

1 solution

We can't help you on that: the error message is pretty explicit in both cases:
Cannot insert the value NULL into ... column does not allow nulls. INSERT fails.

Your column is configured as a "no null values" column - and given that it has the suffix "ID" it's probably a primary key column which can never have null values.

Which means the problem is elsewhere in your code: the value you are using to set your column is null:
C#
cmd.Parameters.AddWithValue("@Ref_UserID", Ref_UserID);

So put a breakpoint on that line, and look at Ref_UserID and confirm it.
Then look at how your code got there, and why it contains no value.
But we can't do that for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900