1

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();
    }

5 Answers 5

6

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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;
}        

1 Comment

Thanks guys. You all are my savior!
1

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)

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.