We help IT Professionals succeed at work.
Get Started

C# convert int to Null

Stanton_Roux
Stanton_Roux asked
on
857 Views
Last Modified: 2012-05-09
I have a few drop down boxes which feed into a SQL query. The query is written so that any of the parameters can be Null and thus excluded from the query. If the selected value of any of the drop down boxes is 0 then i would like to set that parameter to null but I am getting compile time errors.
When I do Convert.ToInt32(null), it still returns 0.

pseudocode: if tddnRegionId.SelectedValue == 0 then make RegionID = null

Code below,
Thanks
protected void btnGo_Click(Object sender, EventArgs e)
    {
        int RegionIDNull = Convert.ToInt32(null);
        int RegionID; 

        RegionID = Convert.ToInt32(tddnRegion.SelectedValue) == 0 ? RegionIDNull : Convert.ToInt32(tddnRegion.SelectedValue);
            
        int Month = Convert.ToInt32(tddnMonth.SelectedValue);

        ResultsManager rm = new ResultsManager();
        try
        {
            DataSet ds = new DataSet();
            ds = rm.getEventDetailsForCalendar(RegionID, Month);

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE