InvalidOperationException: The null value cannot be assigned .... when calling SubmitChanges - asp.net

I have a table A with an Identity column.
When I insert a row via visualStudio it failed in the SubmitChanges with the following error:
InvalidOperationException: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type
I searched about this in google and I found some discussions about the same issue. one of them is Here.
it says that it's because the procedure returns a null value.
I did as is wrote there. used sql trace, copy the insert command and run it in sql server.
it realy returns null but the row was inserted correctly!!!
the command as it is in the sql trace:
exec sp_executesql N'INSERT INTO [dbo].[MyName_Tbl]([x], [y], [z], [c], [v], [b], [n], [m], [a], [s], [d], [f], [g], [h], [j], [k], [l], [q], [w], [e], [r])
VALUES (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7, #p8, #p9, #p10, #p11, #p12, #p13, #p14, #p15, #p16, #p17, #p18, #p19, #p20)
SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]',N'#p0 varchar(8000),#p1 varchar(8000),#p2 nvarchar(4000),#p3 nvarchar(4000),#p4 varchar(8000),#p5 nvarchar(4000),#p6 varchar(8000),#p7 varchar(8000),#p8 nvarchar(4000),#p9 nvarchar(4000),#p10 nvarchar(4000),#p11 nvarchar(4000),#p12 nvarchar(4000),#p13 varchar(8000),#p14 varchar(8000),#p15 nvarchar(4000),#p16 varchar(8000),#p17 nvarchar(4000),#p18 nvarchar(4000),#p19 nvarchar(4000),#p20 decimal(5,2)',#p0='406',#p1='Kabala',#p2=N'01/05/2012 13:47:01',#p3=N'k406/00033',#p4='406/00033',#p5=N'xxx',#p6='127.0.0.1',#p7='10',#p8=N'yyy',#p9=N'hh hh',#p10=N'0527159080',#p11=N'',#p12=N'',#p13='4580',#p14='1',#p15=N'Visa',#p16='0115',#p17=N'10',#p18=N'0',#p19=N'0232323',#p20=0
Can you explain me what's the problem and why in sql it executed correctly and in VS I get an error?

The error you are receiving has nothing to do with an invalid SQL statemt which explains why it works fine when you execute it directly on SQL Server.
The error is being thrown on your app and it's simply because the SQL statement is supposed to return an int containing the value of the last id inserted in the table but instead is returning a NULL value, which makes your program choke since null cannot be assigned to an int unless you declare it as a Nullable<int> (int?)

Related

NULL statement doesn't fill the empty spaces in Sqlite

I'm having trouble with NULL statement in SQLITE. I added NULL in cases there is no info to be filled, but once I run the code the IDE throws an error.
CREATE TABLE tenants (
Apartment_Number INT(4),
Family_Name VARCHAR(8) NULL,
Sur_Name VARCHAR(14) NULL,
Home_Number INT(4),
Mobile_Number int(10),
PRIMARY KEY (Apartment_Number )
);
INSERT INTO tenants
VALUES
(101,,,201,0544431263),
(102,,,202,0544431263),
(103,'Shklobin','marta',203,0544431263),
(104,'arman','charles',204,0544431263);
SELECT * FROM tenants;
The empty spaces are where I hope the IDE will fill with NULL values.
The error I receive:
Error: near line 12: near ",": syntax error.
If I remove the NULL statement, the IDE runs the code with no errors.
Official documentation indicates that
The default value of each column is NULL.
The default behavior is also to allow NULL in each column. The behavior only changes if NOT NULL and/or DEFAULT ... constraints are specified. You should get the same error whether or not you have the lone NULL keyword as shown in the question code. My testing shows that the following does not suppress the error as implied in the question--in other words, the following change results in the same error.
Family_Name VARCHAR(8),
Sur_Name VARCHAR(14),
The following alternative INSERT statements will work:
INSERT INTO tenants
(Apartment_Number, Home_Number, Mobile_Number)
VALUES
(101,201,0544431263),
(102,202,0544431263);
INSERT INTO tenants
VALUES
(103,'Shklobin','marta',203,0544431263),
(104,'arman','charles',204,0544431263);
or
INSERT INTO tenants
VALUES
(101, NULL, NULL,201,0544431263),
(102, NULL, NULL,202,0544431263),
(103,'Shklobin','marta',203,0544431263),
(104,'arman','charles',204,0544431263);

OperationalError: near "(": syntax error" at SQLite3

After taking a crash course for SQLite3, I tried to make a db for my first project:
import sqlite3 as db
conn = db.connect('todo.db')
cursor = conn.cursor()
cursor.execute("CREATE TABLE todo(id serial primary key, title text, created
timestamp default now(), done boolean default 'f')")
cursor.execute("INSERT INTO todo (title) VALUES('Learn web.py')")
Unfortunately I receive this error:
OperationalError: near "(": syntax error" at SQLite3
I do not understand what's wrong with the code. Can anyone explain what I am doing wrong?
As shown in the documentation, if the default value is not a simple value, it must be enclosed in parentheses:
CREATE TABLE todo(
...,
created timestamp default (now()),
done boolean default 'f'
);
(And 'f' is not a valid value for a boolean. And now() is not an SQLite function.)

How to handle expection raised from CURSOR declaration

So I have a procedure below that accept a date as an IN parameter. If a wrong type of parameter is passed in, the exception area will not be able to catch it because the parameter is used in the CURSOR declaration section.
PROCEDURE ABC(p_date IN DATE)
IS
CURSTOR cur
IS
SELECT *
FROM table
WHERE table.date = TRUNC(p_date);
BEGIN
do something;
EXCEPTION
WHEN OTHERS THEN
do something;
END;
If the user passes a wrong parameter type:
BEGIN
ABC(123);
END;
I'll get the "wrong number or types of arguments in call to 'ABC'" error.
Is there a way to handle this error under this scenario?
Micklesh is correct. An error in calling the procedure isn't handled by the called procedure, it needs to be handled in the calling procedure.

Highest record in PL/SQL

I have created a query that displays highest returned items from a table. My query it works perfectly with no errors! However, I want an efficient way of converting the query to PL/SQL block. The purpose of conversion is to handle errors.
SELECT ITEM_NO, MAX(QUANTITY) AS MAXIMUM
FROM ITEMS
WHERE CAT_NO >= (
SELECT MAX(ITEM_NUM) FROM ORDER
WHERE STATUS IN('ONE ITEM RETURNED','ALL ITEMS RETURNED')
)
GROUP BY ITEM_NO
ORDER BY ITEM_NO ASC;
Here's a way we do some exception handling in packages. I altered it to use your code as an example. Maybe you can use some ideas from it.
At the top, set a CONSTANT to the name of the procedure, and some variables to catch Oracle SQL error number and message in. Then in the body of the procedure, there is an anonymous block containing the select. First we set a variable to indicate where we are (err_loc) in case there are multiple locations where an error could be caught in one block. Then the select is issued. If an error occurs, it's caught by the EXCEPTION clause. Error info from Oracle is caught in the err* variables, the err_string is built and then emailed via the UTL_MAIL package. RAISE raises the error so the program halts. It's set up like this to be generic as possible, we can drop in the template, change the MBR_NAME, SQL, err_loc and that's it.
PROCEDURE TEST_PROC AS
MBR_NAME CONSTANT VARCHAR2(100) := 'TEST_PROC'; -- For use in error handling. Package member name.
err_nbr NUMBER; -- Holds a SQL error number if an exception occurs.
err_msg VARCHAR2(1000); -- Holds a SQL error message if an exception occurs.
err_string VARCHAR2(2000);
BEGIN
BEGIN
err_loc := 'Selecting max quantity'; -- Email subject
SELECT ITEM_NO, MAX(QUANTITY) AS MAXIMUM
FROM ITEMS
WHERE CAT_NO >= (SELECT MAX(ITEM_NUM)
FROM ORDER
WHERE STATUS IN('ONE ITEM RETURNED','ALL ITEMS RETURNED')
)
GROUP BY ITEM_NO
ORDER BY ITEM_NO ASC;
EXCEPTION
WHEN OTHERS THEN
err_nbr := SQLCODE;
err_msg := SUBSTR(SQLERRM, 1, 1000);
err_string := 'ERROR: ' || err_nbr || ' occurred: ' || err_msg;
-- PKG_NAME and err_email_recip set in the body.
UTL_MAIL.send(sender => PKG_NAME||'.'||MBR_NAME||'#yourcompany.com',
recipients => err_email_recip,
subject => 'ERROR '|| err_loc,
message => CHR(13))||err_string);
RAISE;
END;
END TEST_PROC;
Have a look at cursors and records.
That way you have fetch data from the query and process the line if needed.
I don't have a database by hand to test my code, but this might give you an idea how a cursor and record work.
In order to capture a EXCEPTION you could add an exception handler and let it log the record you where busy with when the exception occured.
DECLARE
CURSOR CursorName IS
SELECT ColumnOne
FROM TableA
WHERE Name = 'Me';
RecordNumber CursorName%ROWTYPE;
BEGIN
-- Fetch the records from the cursor.
OPEN CursorName;
LOOP
FETCH CursorName INTO RecordNumber;
-- Do something with the record.
EXIT WHEN CursorName %NOTFOUND;
END LOOP;
CLOSE CursorName;
END;
/
Adding a error handeling would be done right above END:
EXCEPTION
WHEN OTHERS THEN
-- Log error message.
END;
/
Link: Error handeling
Does that answer you question a bit?

How do I query table type in oracle package

i want to use sql to query an object like following code.
PACKAGE:
PACKAGE DRAWING AS
TYPE AWARD_NUMBER_ROW IS RECORD (
A_NUMBER VARCHAR2 (10),
A_TYPE char (1)
);
TYPE AWARD_ROW IS RECORD (
A_NUMBER VARCHAR2 (10),
A_NAME VARCHAR2 (50)
);
TYPE AWARD_NUMBER_TABLE IS TABLE OF AWARD_NUMBER_ROW INDEX BY PLS_INTEGER;
TYPE AWARD_TABLE IS TABLE OF AWARD_ROW INDEX BY PLS_INTEGER;
AWARD AWARD_TABLE;
AWARD_T AWARD_TABLE;
PROCEDURE DRAWING (AWARD_NUMBER IN AWARD_NUMBER_TABLE);
END DRAWING;
BODY:
create or replace
PACKAGE BODY DRAWING AS
PROCEDURE DRAWING (AWARD_NUMBER IN AWARD_NUMBER_TABLE) AS
BEGIN
EXECUTE IMMEDIATE
'SELECT * FROM AWARD_INF'
BULK COLLECT INTO AWARD_T;
/*
error is from here. AWARD_T does not exist or is marked for delete
*/
SELECT T.* bulk collect into AWARD FROM TABLE(AWARD_T ) T WHERE T.A_NAME = '123456789' ;
END DRAWING;
END INVOICE_DRAWING
here is exception message.
ORA-21700: object does not exist or is marked for delete
"object does not exist or is marked for delete"
*Cause: User attempted to perform an inappropriate operation to
an object that is non-existent or marked for delete.
Operations such as pinning, deleting and updating cannot be
applied to an object that is non-existent or marked for delete.
*Action: User needs to re-initialize the reference to reference an
existent object or the user needs to unmark the object.
how do i fix it??

Resources