Com.Man.Do.Girl
Com.Man.Do.Girl

Reputation: 177

c# if else statement

I have a problem here because my coding is not working(error) and I don't know how to correct it.Can you guys check if this statement right or wrong? My conditions is 1)if textbox productname is null or empty and dropdownlist1 not selected, text will null. 2)if textbox productname is filled(string) then text will filled in 3)if if textbox productname is null or empty and dropdownlist1 selected, text will select value. Refer bold text.THANKS!!

if (String.IsNullOrEmpty(txtSearchProductname.Text) == true)
    {
        if (**DropDownList1.SelectedValue.ToString == null**)
        {
            txtSearchProductname.Text = " ";
        }
        else 
        {
            SqlProductmaster.InsertParameters["ProductName"].DefaultValue = DropDownList1.SelectedValue.ToString();
        }                
     }
    else 
    {
        SqlProductmaster.InsertParameters["ProductName"].DefaultValue = txtProductName.Text.ToString();
    }

Upvotes: 1

Views: 4457

Answers (5)

Darren Reid
Darren Reid

Reputation: 2322

You are using the SelectedValue of the DropDownList with a ToString() which is not needed. See below.

if (String.IsNullOrEmpty(txtSearchProductname.Text) == true) 
{ 
    if (string.IsNullOrEmpty(DropDownList1.SelectedValue)) 
    { 
        txtSearchProductname.Text = " "; 
    } 
    else 
    { 
        SqlProductmaster.InsertParameters["ProductName"].DefaultValue = DropDownList1.SelectedValue;        
    }
} 
else 
{ 
    SqlProductmaster.InsertParameters["ProductName"].DefaultValue = txtProductName.Text.ToString(); 
}

HTH

Upvotes: 1

IAmTimCorey
IAmTimCorey

Reputation: 16757

The first thing I see is that you have a ToString method without the parenthesis. It should look like this:

if (DropDownList1.SelectedValue.ToString() == null)

As others have pointed out, the second issue is the comparison to null after converting the item to a string. Converting a null to a string will cause an error (the string representation of a null doesn't exist). Instead, as they indicated, you should remove the ToString() entirely and compare the SelectedValue to null like so:

if (DropDownList1.SelectedValue == null)

Upvotes: 1

Adam Robinson
Adam Robinson

Reputation: 185643

Two issues:

  1. You have ToString, not ToString(). ToString refers to the function itself; you need the parentheses to invoke the method
  2. You should not be calling ToString() at all, since the value may be null; this will generate a NullReferenceException. Just check if DropDownList1.SelectedValue == null.

This should be all you need:

if (String.IsNullOrEmpty(txtSearchProductname.Text))
{
    if (DropDownList1.SelectedValue == null)
    {
        txtSearchProductname.Text = " ";
    }
    else 
    {
        SqlProductmaster.InsertParameters["ProductName"].DefaultValue = DropDownList1.SelectedValue;
    }                
 }
else 
{
    SqlProductmaster.InsertParameters["ProductName"].DefaultValue = txtProductName.Text;
}

Upvotes: 6

Alex Aza
Alex Aza

Reputation: 78457

You don't need that many ToString. If DropDownList1.SelectedValue is null, then DropDownList1.SelectedValue.ToString() will throw an exception.

if (string.IsNullOrEmpty(txtSearchProductname.Text) == true)
{
    if (DropDownList1.SelectedValue == null)
    {
        txtSearchProductname.Text = " ";
    }
    else 
    {
        SqlProductmaster.InsertParameters["ProductName"].DefaultValue = DropDownList1.SelectedValue;
    }                
    }
else 
{
    SqlProductmaster.InsertParameters["ProductName"].DefaultValue = txtProductName.Text;
}        

Upvotes: 1

Karl Knechtel
Karl Knechtel

Reputation: 61515

.ToString is a method. You want to check the result of calling that method, so you need to call it (hence, .ToString()).

Upvotes: 1

Related Questions