Click here to Skip to main content
15,879,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Error.

Please correct the coding below its around the 'if'

SQL
if (ddl_Moratorium.SelectedItem.Text ="NONE")
                txt_Morat_Mths.Enabled = false;
            else
                txt_Morat_Mths.Enabled = true;



Thanks
Posted
Updated 30-Aug-14 20:36pm
v2

No one has actually pointed out that the problem is a syntax error in the if statement.
A single = means assignment, as in
C#
int i = 0;

In order to compare equality you need to use double ==

So you could correct your code by simply adding a second =
C#
if (ddl_Moratorium.SelectedItem.Text == "NONE")
    txt_Morat_Mths.Enabled = false;
else
    txt_Morat_Mths.Enabled = true;


As has been pointed out, this is not safe as you will get a NullReference error if ddl_Moratorium.SelectedItem is null.
Solution 5 gives you the most readable code.

You can also change your code to a one liner, but it is of course less readable. :)
C#
txt_Morat_Mths.Enabled = ((ddl_Moratorium.SelectedItem != null) && (ddl_Moratorium.SelectedItem != "NONE"));
 
Share this answer
 
Comments
George Jonsson 3-Sep-14 7:05am    
Whoever downvoted my answer is welcome to give me a hint why it is a bad answer, so I can improve it.
SQL
if (ddl_Moratorium.SelectedItem == null)
    txt_Morat_Mths.Enabled = false;
else if (ddl_Moratorium.SelectedItem.Text == "NONE")
    txt_Morat_Mths.Enabled = false;
else
    txt_Morat_Mths.Enabled = true;
 
Share this answer
 
Comments
George Jonsson 3-Sep-14 6:28am    
My vote of 5 for readability.
Bernhard Hiller 3-Sep-14 8:43am    
Thanks.
Try this
C#
if (ddl_Moratorium.SelectedItem.Text == "NONE")
 txt_Morat_Mths.Enabled = false;
else
 txt_Morat_Mths.Enabled = true;


or
C#
if (ddl_Moratorium.SelectedItem.Text.Equals("NONE"))
 txt_Morat_Mths.Enabled = false;
else
 txt_Morat_Mths.Enabled = true;
 
Share this answer
 
v2
Comments
One closing bracket is missing in the 2nd piece of code. :)
Bernhard Hiller 3-Sep-14 2:57am    
Fails with a NullReferenceException when ddl_Moratorium.SelectedItem is null.
Your requirement is so simple you can skip complete if else block and try following.
C#
txt_Morat_Mths.Enabled = ddl_Moratorium.SelectedItem.Text != "NONE";

Or
C#
txt_Morat_Mths.Enabled = !ddl_Moratorium.SelectedItem.Text.Equals("NONE", StringComparison.OrdinalIgnoreCase);
 
Share this answer
 
Comments
Bernhard Hiller 3-Sep-14 2:58am    
Fails with a NullReferenceException when ddl_Moratorium.SelectedItem is null.
DineshMaind 3-Sep-14 3:18am    
ok, try this...
txt_Morat_Mths.Enabled = ddl_Moratorium.SelectedItem != null && ddl_Moratorium.SelectedItem.Text != "NONE";
C#
if(ddl_Moratorium.SelectedItem.Text.Equals("NONE",StringComparison.OrdinalIgnoreCase)
 txt_Morat_Mths.Enabled = false;
else
 txt_Morat_Mths.Enabled = true;
 
Share this answer
 
Comments
Bernhard Hiller 3-Sep-14 2:58am    
Fails with a NullReferenceException when ddl_Moratorium.SelectedItem is null.
[no name] 3-Sep-14 12:08pm    
Okay try ddl_Moratorium.SelectedValue.ToString() or ddl_Moratorium.SelectedItem.ToString() instead of ddl_Moratorium.SelectedItem.Text
For the better clarification use braces to define the blocks in C# Code

if (ddl_Moratorium.SelectedItem.Text == null)
{
   txt_Morat_Mths.Enabled = false;
}
else
{
   txt_Morat_Mths.Enabled = true;
}
 
Share this answer
 
use below code
SQL
if (ddl_Moratorium.SelectedItem.Text =="NONE")
                txt_Morat_Mths.Enabled = false;
            else
                txt_Morat_Mths.Enabled = true;
 
Share this answer
 
Comments
Bernhard Hiller 3-Sep-14 2:58am    
Fails with a NullReferenceException when ddl_Moratorium.SelectedItem is null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900