I tested this method for the string parameter alone, and it worked perfectly. So I am sure there is a mistake in setting up the parameter of type DateTime (datapubl), which I added later. Thank you in advance!
By the way, the CatalogCreateFilmsTest sproc was executed and works OK.
Anna
public static bool CreateFilmTest(string nume, string datapubl)
{
DbCommand com = GenericDataAccess.CreateCommand();
com.CommandText = "CatalogCreateFilmTest";
DbParameter param = com.CreateParameter();
param.ParameterName = "@nume";
param.Value = nume;
param.DbType = DbType.String;
param.Size = 200;
com.Parameters.Add(param);
param = com.CreateParameter();
param.ParameterName = "@datapubl";
param.Value = datapubl;
param.DbType = DbType.DateTime;
com.Parameters.Add(param);
int result = -1;
try
{
result = GenericDataAccess.ExecuteNonQuery(com);
}
catch
{
//
}
return (result >= 1);
}
EDIT: The problem is the stored procedure is not even executed (it should insert rows into a table but doesn’t) . No error, but not the correct result either.
EDIT: Chris, here is the full example:
CREATE PROCEDURE CatalogCreateFilmTest(
@nume nvarchar(1500),
@datapubl datetime
)
AS
INSERT INTO Filme
(nume, datapubl)
VALUES
(@nume, @datapubl)
;
GO
which works for:
EXEC CatalogCreateFilmTest 'achu', '';
I then call CreateFilmTest like this:
bool success = FilmsAccess.CreateFilmTest(newNume.Text, null);
or:
bool success = FilmsAccess.CreateFilmTest(newNume.Text, DateTime.Now.ToString());
In both cases, the ExecuteNonQuery doesn’t run.
I don’t believe you can reuse the same variable due to the reference in the collection…
Also note, that you can use syntax like so with .net 4 (and maybe 3.5 too).
It also might help to know what the exception is by filling in your catch block:
EDIT:
I believe I see the issue now… (and should have seen it before).
Change your method signature to:
And try executing the following:
To pass null, you’ll have to pass
Convert.DbNullprovided your column allows null.In order to pass strings as DateTime values, you’ll have to format them a specific way, i.e. YYYYMMDD however, I recommend letting the underlying DbCommand object take care of that.