Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i have build grid in that i am using dropdownlist

if i select any item from dropdownlist it should be save to database as intger according to it's Id.


In view i had taken it as string

in model i had taken it as string

but in controller query i need to convert it from "string to short"


ex: res.cat_id = c.category;

but i am getting error that change it to string to integer where i am doing wrong i couldn't able to understand.


please any one give me sample or related type solution.

Thank you,
Posted

You need to use the Int32.Parse() or Int32.TryParse() method, to convert from string to integer.
 
Share this answer
 
Comments
Sukanya Karri 14-Jun-12 7:38am    
Hi i ahve already used this but it's not working actually i have taken type as string in view

and in controller i need to convert it into integer to database and again it should return as string in my output

i am taking three tables inthat 1 table i amgiving id and in other table i have take that id and display the string.
Richard MacCutchan 14-Jun-12 7:56am    
Sorry, I don't understand the above, if you have the string you can convert it to an integer as I mentioned above. If you have an integer you can Format() it into a string.
Sukanya Karri 14-Jun-12 8:09am    
public ActionResult Update(IEnumerable<values> us)
{
foreach (var c in us)


{




var res = (from i in dbContext.My_usr_skills

where i.skill_id == c.Id

select i).FirstOrDefault();

res.skill_name = c.Skills;


res.skill_duration_type_id = Convert.ToInt16(c.duration);

this.dbContext.Add(res);

}
try
{
this.dbContext.SaveChanges();
}
catch
{
return View();
}
return Json(null);
}



this is my controller in this duration is my dropdownlist

i have taken 3 tables 1 is the main table so i ahve to take only id and i have to display string

so in my view page i have written in this way

//view page
duration: { type: "string", validation: { required: true} },

so now i am getting error like cannot implicitly convert string to short

so please send me any example or sample solution
Richard MacCutchan 14-Jun-12 8:18am    
I have given you the example, I have no idea why you cannot understand that you need to convert a string to an integer, it will not happen automatically.
Sukanya, though you should have had taken care of DB design in the initial stage. but as of now if you have gone very far I would suggest you to convert the required data in required format from controller and then send the insert query.

use convert.toint and .tostring() to convert the data into ur desired format.
 
Share this answer
 
Comments
Sukanya Karri 14-Jun-12 8:17am    
hi this is my solution:-


public ActionResult Update(IEnumerable us)
{
foreach (var c in us)


{




var res = (from i in dbContext.My_usr_skills

where i.skill_id == c.Id

select i).FirstOrDefault();

res.skill_name = c.Skills;


res.skill_duration_type_id = Convert.ToInt16(c.duration);

this.dbContext.Add(res);

}
try
{
this.dbContext.SaveChanges();
}
catch
{
return View();
}
return Json(null);
}



this is my controller in this duration is my dropdownlist

i have taken 3 tables 1 is the main table so i ahve to take only id and i have to display string

so in my view page i have written in this way

//view page
duration: { type: "string", validation: { required: true} },

so now i am getting error like cannot implicitly convert string to short

so please send me any example or sample solution
AmitDutta_Bokaro 15-Jun-12 7:11am    
cool .

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