I have columns in my database called ModifyUser and ModifyDate that are supposed to show the username and date and time of the last person that has updated a specific product somehow.
I thought if I just added the second query then I would be fine, but for some reason the application wants to add ProductName into the Product table too. Why isn’t the application just inserting the 2 values I told it to insert in the Product table INSERT?
Error message: Cannot insert the value NULL into column 'ProductName', table
'Products.dbo.Product'; column does not allow nulls. INSERT fails.
The statement has been terminated.
'SQL INSERT: CompanyLink Table
Dim strSQL As String = "INSERT INTO CompanyLink (ProductID, CompanyID)
VALUES (@ProductID, @CompanyID);INSERT INTO Product (ModifyDate, ModifyUser)
VALUES (getdate(), @ModifyUser)"
Using cn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString)
Using cmd As New SqlCommand(strSQL, cn)
cmd.Parameters.Add(New SqlParameter("@ProductID", ProductID.Value))
cmd.Parameters.Add(New SqlParameter("@CompanyID", company.Value))
cmd.Parameters.Add(New SqlParameter("@ModifyUser",
System.Web.HttpContext.Current.User.Identity.Name))
cn.Open()
cmd.ExecuteNonQuery()
The error message is pretty clear: your are not providing
ProductName, which is a required (NOT NULL) field.In order to fix it, you have to provide the
ProductName, modifying this:and add this:
Update
Since what you really need is an update, your statement will be:
Remember to provide all parameters values for the query.