Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Thanks its work.
But here i explain what i want.
i have 1 dropdownlist product.
1 text box take price.
1 search button.
if i don't give any product or price it will display all product.
and i call my search method in button click event.here my sample code

C#
int productid = int.Parse(DropDownListProduct.SelectedValue.ToString());
        
            int price;
            if (int.TryParse(txtPrice.Text, out price))
            {
                price = Convert.ToInt32(txtPrice.Text);
            }
            else
            {
                price =null or ""; give me ERROR
            }

ctlDropDown.Items.InsertAt(0, new ListItem("SELECT PRODUCT", null);
Give me Error if i don't select anything.and i put it under databind() form load event
Posted
Updated 9-Jun-12 14:08pm
v2
Comments
Stephen Hewison 11-Jun-12 3:06am    
Then trying modifying the data collection before you bind it.

1 solution

Hi,

int.TryParse wont throw an exception. It returns a boolean containing if the value was successfully parsed. It also takes an int as an output parameter so you still have access to the int value when successful. You can use it in the following way:

C#
int price = 0;
if(int.TryParse(txtPrice.Text, out price)) {
  // Do code here
}


In this example it doesn't throw an exception and only executes the relevant code when the input value is valid.

As for not selecting the first product in the list. Without seeing your code it's difficult to give advice. But my best guess would be that you need to add a place holder. Make the first item in the list "--Select--". Then in your code check for this value and ignore it if it's selected.
 
Share this answer
 
v4
Comments
Member 8932104 9-Jun-12 13:44pm    
thanks for first one.but 2nd one is select all product from product list and i bind it page load event.so, can u please give any example about ---select--
Stephen Hewison 9-Jun-12 15:28pm    
There is a method called InsertAt which is available on most collections and can be used to modify data lists after they've been bound. So after you bind the data try:

ctlDropDown.Items.InsertAt(0, new ListItem("Text", "Value"));

Or:

You can modify the collection before you bind it. But this isn't always viable depending on the way you manage the data in your software.
VJ Reddy 10-Jun-12 5:02am    
Good suggestion to use TryParse. 5!

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