Maxcancer
Maxcancer

Reputation: 1

The input string was not in the correct format

i'm getting an error in this piece of code with asp.net mvc 4 "input string was not in a correct format" i know why i get this error because im making a search filter by Marca (Brand, im making it in spanish), the dropdownlist where the brands are in a table are in string value, but the row in the Items Table where they are related is an int value. The search results http post tries to do it with string values and i get the error, if i type manually in the url the int id of the brand i want to search the query passes. Here is an example of my code and sorry for my bad english.

public ActionResult Index(string marcas, string search_query)
    {
var MarcaLst = new List<string>();

var MarcaQry = from d in db.Marcas // table Brand
               orderby d.Marca1 // string row of the table Brand
               select d.Marca1;
MarcaLst.AddRange(MarcaQry.Distinct());
ViewBag.marcas = new SelectList(MarcaLst);

var marca = from m in db.Marcas
            select m;

if (!String.IsNullOrEmpty(search_query))
{
    descripcion = descripcion.Where(s => s.Descripcion.ToUpper().Contains(search_query.ToUpper()));
}

if (string.IsNullOrEmpty(marcas))
    return View(descripcion.ToList());
else
{
    int marcasint = Convert.ToInt32(marcas); // I get the error here from a work around to make the page load
    return View(descripcion.Where(x => x.MarcaID == marcasint)); //MarcaID is the int value of the Items table for the Brand
}
}

url/articulos?search_query=a&marcas=BSN //Error

url/articulos?search_query=a&marcas=1 //Pass

Upvotes: 0

Views: 3447

Answers (4)

Kaf
Kaf

Reputation: 33809

Try using TryParse method

 int marcasint;
 if(Int32.TryParse(marcas, out marcasint))
 {
    return View(descripcion.Where(x => x.MarcaID == marcasint));
 }
 else
 {
    //change eles part as required.
    return View(descripcion.Where(x => x.MarcaID == -1));
 }

After reading your comments, I think you don't need to filter records by MarcaID but it's name. So try this and replace BrandName with correct field name.

 return View(descripcion.Where(x => x.BrandName == marcas));

Upvotes: 0

Jani Hyyti&#228;inen
Jani Hyyti&#228;inen

Reputation: 5407

Don't just throw in a string to Convert and assume it's a valid integer if it's not null or empty. Use TryParse instead:

int marcasint;
bool success = Int32.TryParse(marcas, out marcasint);
if (success){
    return View(descripcion.Where(x => x.MarcaID == marcasint));
} else {
    return View(descripcion.ToList());
}

Upvotes: 2

Novice
Novice

Reputation: 2487

You are trying to convert string to integer.So the string must be in the right format.For example, You cannot convert "abc1" to integer. You can use Int32.TryParse(stringVal) to check for possiblity of type conversion and convert. The above method returns boolean.

Upvotes: 2

Sorcerer86pt
Sorcerer86pt

Reputation: 447

try the Int32.TryParse method to try to convert the string to a integer. All major number formats have the TryParse methods:

Documentation: http://msdn.microsoft.com/en-us/library/f02979c7.aspx

Upvotes: 1

Related Questions