Hello Everyone,

In my sql database I am having a country field and a State field with some values and also some null values and I am populating the country values into a dropdown list box in the frontend using database connection

e.g.
Country
India
Indonesia
null
Iran

My requirement is, if I select the particular country in the dropdown list box, I need to display the rspctive state assigned to it, If I select the null value, all the state and country should be displayed. I need the coding for the above null condition in c#.

Thanks
Sasikala

Recommended Answers

All 3 Replies

I'm not certain of what you require. But perhaps you need DBNull.Value this represents a null database value. For example you can comapare a value in a DataSet to DBNull.Value which returns true if the value is null.

if(myDataSet.Rows[0]["columnName"] == DBNull.Value)
         //the value is null.

one method is

string.IsNullOrEmpty(stringtoCheck)

another method is

if(stringtoCheck==DBNull.Value)

both works but difference is you can check string.Empty also in the First method to check for DB nulls you have to use second method

you can also use ISNULL function in your query since your are getting the data from the database

SELECT
ISNULL(COUNTRY, '') AS COUNTRY
FROM TABLENAME

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.