Click here to Skip to main content
15,915,336 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Private Sub dept_num_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dept_num.TextChanged
        Dim con As SqlConnection = New SqlConnection("Data Source=S-MHAMEED\SQL2008RTWO;Initial Catalog=comp.task;User ID=..;Password=...")
        Using (con)

            Dim dt As New DataTable
            Dim sqlCom As New SqlCommand()
            Dim sqlDA As New SqlDataAdapter()

            sqlCom.Connection = con
            sqlCom.CommandText = "insert_emp"
            sqlCom.CommandType = CommandType.StoredProcedure
            sqlCom.Parameters.AddWithValue("DEBT_NUM", Val(dept_num.Text))
            sqlDA = New SqlDataAdapter(sqlCom)
            sqlDA.Fill(dt)

            If dt.Rows.Count Then
                dept_name.Text = dt.Rows(0).Item("debt_name")
                emp_num.Text = dt.Rows(0).Item("debt_emp")
            End If
        End Using
    End Sub


What I have tried:

Cannot insert the value NULL into column 'emp_num', table 'comp.task.dbo.Employee'; column does not allow nulls. INSERT fails. The statement has been terminated.
Posted
Updated 12-Feb-18 5:06am
v2
Comments
F-ES Sitecore 12-Feb-18 11:07am    
The error message is self-explanatory, you are trying to enter NULL into the emp_num field of the Employee table, probably inside your insert_emp stored proc. We don't know your inputs or the stored proc's text so there is no specific advice we can give. Look at the stored proc for where it inserts the emp_num, see what it is inserting and trace things back from there.
Shaddow>dark 13-Feb-18 1:41am    
ALTER PROCEDURE [dbo].[insert_emp]
@EMP_NUM numeric(18) = null,
@DEBT_NUM numeric(18) ,
@EMP_NAME varchar(150) =null,
@EMP_P_DATE smalldatetime =null,
@EMP_WORK_DATE smalldatetime =null,
@EMP_SAL NUMERIC(18) =null,
@EMP_DESC VARCHAR(250) =null,
@DEBT_NAME VARCHAR(150) =null
AS
BEGIN
IF NOT EXISTS(SELECT 1 FROM Employee WHERE EMP_NUM = @EMP_NUM )
BEGIN
INSERT INTO DBO.EMPLOYEE ( DEPT_NUM ,EMP_NAME ,EMP_P_DATE ,EMP_WORK_DATE ,EMP_SAL ,EMP_DESC,DEBT_NAME )
VALUES (@DEBT_NUM,@EMP_NAME,@EMP_P_DATE,@EMP_WORK_DATE,@EMP_SAL,@EMP_DESC,@DEBT_NAME )
END
ELSE
BEGIN
UPDATE dbo.Employee
SET emp_num =@EMP_NUM ,dept_num =@DEBT_NUM ,emp_name =@EMP_NAME ,emp_p_date =@EMP_P_DATE ,emp_work_date =@EMP_WORK_DATE ,emp_sal =@EMP_SAL ,emp_desc =@EMP_DESC ,debt_name =@DEBT_NAME
WHERE emp_num =@EMP_NUM

END
F-ES Sitecore 13-Feb-18 4:13am    
You only pass debt_num to your sp so all those other fields are null. We don't know your business logic so we don't know what you want to happen when you only have one field, only you can answer that. Maybe you intend emp_num to be an IDENTITY field? If it's the table's unique identifier and you make it IDENTITY you don't need to give it a value, it will generate its own in sequence.
Ziee-M 12-Feb-18 11:15am    
You just have to fill all others 'In' parameters of you Stored procedure. In your case, you have added only the DEBT_NUM field (emp_num is one of them).
You will have somthing like this :
sqlCom.Parameters.AddWithValue("DEBT_NUM", Val(dept_num.Text))
sqlCom.Parameters.AddWithValue("someOtherField", Val(field.Text))
...
sqlCom.Parameters.AddWithValue("emp_num", Val(empNum.Text))
Hope it helps.

1 solution

The error message is pretty explicit:
Cannot insert the value NULL into column 'emp_num', table 'comp.task.dbo.Employee'; column does not allow nulls.

The emp_num column of the Employee table is defined as NOT NULL, but your INSERT operation is not providing a value.

Either provide a value (recommended) or change the table definition.
 
Share this answer
 
Comments
Shaddow>dark 12-Feb-18 11:06am    
in your opinion whats the best way to do ???
OriginalGriff 12-Feb-18 11:32am    
I have no idea! I can't tell what your SP looks like, your table definition is, or even what "emp_num" is - much less where you get it from...
Shaddow>dark 13-Feb-18 1:47am    
ALTER PROCEDURE [dbo].[insert_emp]
@EMP_NUM numeric(18) = null,
@DEBT_NUM numeric(18) ,
@EMP_NAME varchar(150) =null,
@EMP_P_DATE smalldatetime =null,
@EMP_WORK_DATE smalldatetime =null,
@EMP_SAL NUMERIC(18) =null,
@EMP_DESC VARCHAR(250) =null,
@DEBT_NAME VARCHAR(150) =null
AS
BEGIN
IF NOT EXISTS(SELECT 1 FROM Employee WHERE EMP_NUM = @EMP_NUM )
BEGIN
INSERT INTO DBO.EMPLOYEE ( DEPT_NUM ,EMP_NAME ,EMP_P_DATE ,EMP_WORK_DATE ,EMP_SAL ,EMP_DESC,DEBT_NAME )
VALUES (@DEBT_NUM,@EMP_NAME,@EMP_P_DATE,@EMP_WORK_DATE,@EMP_SAL,@EMP_DESC,@DEBT_NAME )
END
ELSE
BEGIN
UPDATE dbo.Employee
SET emp_num =@EMP_NUM ,dept_num =@DEBT_NUM ,emp_name =@EMP_NAME ,emp_p_date =@EMP_P_DATE ,emp_work_date =@EMP_WORK_DATE ,emp_sal =@EMP_SAL ,emp_desc =@EMP_DESC ,debt_name =@DEBT_NAME
WHERE emp_num =@EMP_NUM

END
OriginalGriff 13-Feb-18 2:24am    
So if you don't pass a value to the SP - and you don't - it's NULL.
And the column can't hold NULL values.

Do you see what you have done here?

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