I have a subroutine to insert a record to sql server table. The code:
public void Insert_Met(int Counts, char Gender)
{
Dictionary
int count = !(count == 0)? count ? (object)DBNull.Value
-If, count is not zero save the count value to database -If it is zero save
You could use int? counts
instead of int counts
, and check the value within your method:
public void InsertMet(int? counts, char gender)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@Counts", counts.HasValue ? counts.Value : (object)DBNull.Value);
parameters.Add("@Gender", gender);
// run a stored procedure ExecuteNonQuery
}
it is a numericupdown control in windows form. I feel that it is hard to assign a text to an int variable. How to change it?
In order to set the count value appropriately for the above, you could do:
int value = (int)numberUpdowncontrol1.Value;
int? counts = !string.IsNullOrEmpty(numberUpdowncontrol1.Text) ? value : (int?)null;
InsertMet(counts, 'M');
Following statement will always produce not null value
int counts = Convert.ToInt32(null); // Result is 0