Thursday, December 29, 2022

How to send null integer or string values to database SQL ASP.NET C#

 There are two ways

either you can send null value from C#


 public static bool UpdateOpenVacancy(int id, string cadreName, int? cadreID, string instituteName, int? instituteID, string disciplineName, int? disciplineID, int? sanctionedStrength, int? inPosition, int? vacancy, int openVacancy)

        {

            SqlParameter[] param = {

                new SqlParameter("@id", id),

                new SqlParameter("@cadreName", cadreName),

                new SqlParameter("@cadreId",cadreID),

                new SqlParameter("@instituteName", instituteName),

                new SqlParameter("@instituteId", instituteID==null?Convert.DBNull:instituteID),

                new SqlParameter("@disciplineName", disciplineName),

            };

using Convert.DBNull


or in stored procedure pass null value to parameters

ALTER PROCEDURE [dbo].[UpdateOpenVacancy]

@id int,

@cadreName VARCHAR(100),

@cadreID INT = null,

@instituteName VARCHAR(100) = null,


END

or 

int? counts1 = string.IsNullOrEmpty(yarncounts.Text) ? (int?)null : int.Parse(yarncounts.Text);


No comments:

Post a Comment