tags:

views:

29

answers:

2

Hi,

I have many TextBOX in asp.net which accepts the value from user and stores it to SQL DB through SQL SERVER.. When the user does not enter the value in the textbox, Which means i can allow the null for that filed in the table, But if its integer variable it stores 0, thats because

int HostelFees ;
HostelFees = TextBox1.Text;
//function called to store the hostel fee
insertFee(hostelFees);

in Database it stores 0, which i dont want but it to be null.. I went through many documentations and understood only slightly about ISDBNULL, But still am confused regarding while inserting how i will insert null. :( and i am not using any stored procedure's.. infact in the function insertFee(hostelFees) i just use command.ExecuteNonQuery to insert it

Please can anyone give me proper explation or link which i refer. Thank you,

+1  A: 

your integer value should be nullable, just add ? after type declaration

int? HostelFees ;
Flakron Bytyqi
+1  A: 

You should consider using int?, which is a nullable version of int. In that case, you can use the HostelFees.HasValue check to see if a null is stored, and HostelFees.Value to use the integer part.

Please note that in your sample you are directly storing a string value into an integer variable. That won't work. What you want is to parse it first, then store null if it's not an integer. Something like this:

int? HostelFees = null;
int parsedValue;

if (int.TryParse(TextBox1.Text, out parsedValue)) {
    HostelFees = parsedValue;
}

//function called to store the hostel fee
insertFee(hostelFees);

And instead of assigning the integer value directly to the DbParameter, you can use the following construction:

parameter.Value = (HostelFees.HasValue ? HostelFees.Value : DbNull.Value);
Prutswonder
@Prutswonder then while retrieving the value from the Database to display it, how will i check it and say not appliacble?i mean u told insertion but then getting the value i need isDBNULL?
Nagaraj Tantri
Added a sample for that.
Prutswonder