Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ADO.NET: Send procedure parameter with null value

I need to send a parameter a null value to the procedure, but I get an error:

System.Data.SqlClient.SqlException: ‘Procedure or function ‘sel_mizanyeni’ expects parameter ‘@subeno’, which was not supplied.’

When I run the same procedure with the same parameters in SQL Server, it does not cause an error. When I run it with ADO.NET in C#, I get that error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Running state in SQL Server:

exec sel_mizanyeni 
     @subeno=null,
     @tarih='2021-12-28 00:00:00',
     @kirilimlevel=8,
     @detaylevel=3,
     @hesap1=N'342',
     @hesap2=N'343',
     @dovizcinsi=0,
     @bakiyeverenler=1,
     @optnetbakiye=1,
     @optdisticsir=0,
     @opteval=0,
     @kurtarihi='2021-12-28 00:00:00',
     @yilsonu=0,
     @firmano=NULL,
     @ekno=NULL,
     @ikitariharasi=0,
     @bittarih='2021-12-28 00:00:00',
     @migration=1

This code is not working in C#:

SqlCommand cmd = new SqlCommand("sel_mizanyeni", baglanti);
cmd.CommandType = CommandType.StoredProcedure;

// I need to pass NULL here, but I get the mentioned error
cmd.Parameters.AddWithValue("@subeno", null);

cmd.Parameters.AddWithValue("@tarih", "2021-12-28 00:00:00");
cmd.Parameters.AddWithValue("@kirilimlevel", 8);
cmd.Parameters.AddWithValue("@detaylevel", 3);
cmd.Parameters.AddWithValue("@hesap1", "342");
cmd.Parameters.AddWithValue("@hesap2", "343");
cmd.Parameters.AddWithValue("@dovizcinsi", 0);
cmd.Parameters.AddWithValue("@bakiyeverenler", 1);
cmd.Parameters.AddWithValue("@optnetbakiye", 1);
cmd.Parameters.AddWithValue("@optdisticsir", 0);
cmd.Parameters.AddWithValue("@opteval", 0);
cmd.Parameters.AddWithValue("@kurtarihi", "2021-12-28 00:00:00");
cmd.Parameters.AddWithValue("@yilsonu", 0);

cmd.Parameters.AddWithValue("@firmano", null);
cmd.Parameters.AddWithValue("@ekno", null);

cmd.Parameters.AddWithValue("@ikitariharasi", 0);
cmd.Parameters.AddWithValue("@bittarih", "2021-12-28 00:00:00");
cmd.Parameters.AddWithValue("migration", 1);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

dataGridView1.DataSource = ds.Tables[0];

>Solution :

From Specifying parameter data types – ADO.NET documentation,

When you send a null parameter value to the server, you must specify DBNull, not null (Nothing in Visual Basic). The null value in the system is an empty object that has no value. DBNull is used to represent null values.

Hence, use DBNull.Value instead of null for the parameter’s value.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading