Hi i have a class where i'm using a drop down to populate hobbies from other table
while selecting and entering data i dnt face any problem but while saving it i get above error
myclass
public virtual int HobbyDetailID { get; set; }
public virtual int HobbyId { get; set; }// this is the primary key of class HobbyMaster contain data for Hobbyas Hobby name and id, mapping done in mapping classs
public virtual string StudyMedium { get; set; }
public virtual decimal Fees { get; set; }
public virtual char ForWhom { get; set; }
in controller:
ViewBag.Hobby = (ICollection<HobbyMasters>)(new HobbyDetailService().GetAllhobby());//through which i'm populating dropdown
in view :
@Html.DropDownListFor(model => model.hobbydetail.HobbyId, new SelectList(ViewBag.Hobby, "HobbyId", "HobbyName"))
in my function:
public bool SaveHobbyDetailWithHobbyHome(HobbyHome home)
{
log.Debug("Start");
if (home == null)
{
throw new ArgumentNullException("home");
}
ISession session = DataAccessLayerHelper.OpenWriterSession();
ITransaction transaction = session.BeginTransaction();
bool saved = false;
try
{
session.Save(home.hobbydetail);
session.SaveOrUpdate("HobbyDetail", home.hobbydetail);//getting internal error as"Exception occurred getter of HobbyHomes.Model.HobbyMasters.HobbyId "
Shouldn't "HobbyHome" be "HobbyMaster" instead? Seems like you are submitting a HobbyMaster object and trying to cast it as HobbyHome.
Also, I'm not understanding what you are trying to accomplish here. Are you presenting a list of hobbies for the user to select, and then submitting the selected hobby to your controller action? I feel your action code could be simplified without all the
session and transaction vars (unless there are details you are leaving out, giving reason to have such calls).
First of all thank you so much sir for replying....
JohnLocke
Shouldn't "HobbyHome" be "HobbyMaster" instead?
exactlywere are you pointig by this ??
JohnLocke
I'm not understanding what you are trying to accomplish here
well in my project HobbyHome is master table and othr tables like HobbyDetails, HobbyMaster are storing their primary key in it...
hence i need towrite this code....
in my HobbyHome i created objectof other table bt by saving only HobbyHome's object does not show me any error bt also dont save the data.....strange bt true....
session.Save(home.hobbydetail);
session.SaveOrUpdate("HobbyDetail", home.hobbydetail);//getting internal error as"Exception occurred getter of
Can you tell me why you are invoking Save and SaveOrUpdate?
Basically Save method will insert an entry if the identifier doesn’t exist, else it will throw error. If the primary key already present in the table, it cannot be inserted and the SaveOrUpdate calls save() or update() based on the operation. If the identifier
exists, it will call update method else the save method will be called.
Post your full view and action. It's hard to say what all you are trying to submit that you'd need to use HobbyHome as the model, when you only show the dropdownlist, which only represents an int type.
Why are you storing certain values of a single hobby in different classes? It would appear easier if you had one class "HobbyClass" that contained all the details of the hobby. Then you wouldn't have to deal with multiple action parameters, nor bother
with multiple methods of updating all the parameters into different tables.
I feel I can't help you with your current setup, so I'll provide my solution and you can use it or ignore it:
public class HobbyClass
{
public int HobbyId {get;set;}
public string Name {get;set}
public string StudyMedium {get;set;}
public decimal Fees {get;set;}
public string ForWhom {get;set;}
public bool InclusiveStudyMaterial {get;set;}
public int AgeTo {get;set;}
public int AgeFrom {get;set;}
public string HobbyInfo {get;set;}
public List<SelectListItem> GetYesNo()
{
List<SelectListItem> yesNoItems = new List<SelectListItem>();
yesNoItems.Add(new SelectListItem { Text = "Yes", Value = "true" });
yesNoItems.Add(new SelectListItem { Text = "No", Value = "false" });
return yesNoItems;
}
public List<SelectListItem> GetMediums()
{
List<SelectListItem> mediumItems = new List<SelectListItem>();
mediumItems.Add(new SelectListItem { Text = "Marathi", Value = "marathi" });
mediumItems.Add(new SelectListItem { Text = "English", Value = "english" });
return mediumItems;
}
public List<SelectListItem> GetForWhom()
{
List<SelectListItem> whomItems = new List<SelectListItem>();
whomItems.Add(new SelectListItem { Text = "Male", Value = "m" });
whomItems.Add(new SelectListItem { Text = "Female", Value = "f" });
return whomItems;
}
public ActionResult Edit(int HobbyId)
{
var hobby = getHobbyById(HobbyId); // you'll need to create a method for getting a hobby by it's ID
ViewBag.Yesno = GetYesNo();
ViewBag.StudyMed = GetMediums();
ViewBag.Whom = GetForWhom();
return View(hobby);
}
[HttpPost]
public ActionResult Edit(HobbyClass model)
{
if (ModelState.IsValid)
{
try {
var hobby = getHobbyById(model.HobbyId);
UpdateModel(hobby);
return View("EditSuccess"); // whatever you want to return if successful
}
catch (Exception ex) {
ModelState.AddModelError("", ex.message);
}
}
else
{
ModelState.AddModelError("", "Correct the following errors");
}
ViewBag.Yesno = GetYesNo();
ViewBag.StudyMed = GetMediums();
ViewBag.Whom = GetForWhom();
return View(model); // this returns the form with validation errors
}
And again, you don't have to use this, but I feel it's a much easier way to manage your hobbies without having to update multiple tables. Hope this helps.
priya77
Member
49 Points
70 Posts
getting error as "Object does not match target type. "
Apr 06, 2012 12:56 PM|LINK
Hi i have a class where i'm using a drop down to populate hobbies from other table
while selecting and entering data i dnt face any problem but while saving it i get above error
myclass
public virtual int HobbyDetailID { get; set; }
public virtual int HobbyId { get; set; }// this is the primary key of class HobbyMaster contain data for Hobbyas Hobby name and id, mapping done in mapping classs
public virtual string StudyMedium { get; set; }
public virtual decimal Fees { get; set; }
public virtual char ForWhom { get; set; }
in controller:
ViewBag.Hobby = (ICollection<HobbyMasters>)(new HobbyDetailService().GetAllhobby());//through which i'm populating dropdown
in view :
@Html.DropDownListFor(model => model.hobbydetail.HobbyId, new SelectList(ViewBag.Hobby, "HobbyId", "HobbyName"))
in my function:
public bool SaveHobbyDetailWithHobbyHome(HobbyHome home)
{
log.Debug("Start");
if (home == null)
{
throw new ArgumentNullException("home");
}
ISession session = DataAccessLayerHelper.OpenWriterSession();
ITransaction transaction = session.BeginTransaction();
bool saved = false;
try
{
session.Save(home.hobbydetail);
session.SaveOrUpdate("HobbyDetail", home.hobbydetail);//getting internal error as"Exception occurred getter of HobbyHomes.Model.HobbyMasters.HobbyId "
transaction.Commit();
saved = true;
}
catch (SessionException ex)
{
if (transaction != null && transaction.IsActive)
transaction.Rollback();
log.Error(ex);
}
finally
{
if (transaction != null)
transaction.Dispose();
if (session != null && session.IsConnected)
session.Close();
log.Debug("End");
}
return saved;
}
i debugged my project and found that both the fields hold the same (i.e. int value)
can you help me ??
NHibernate mvc3
JohnLocke
Contributor
3216 Points
710 Posts
Re: getting error as "Object does not match target type. "
Apr 06, 2012 01:32 PM|LINK
Shouldn't "HobbyHome" be "HobbyMaster" instead? Seems like you are submitting a HobbyMaster object and trying to cast it as HobbyHome.
Also, I'm not understanding what you are trying to accomplish here. Are you presenting a list of hobbies for the user to select, and then submitting the selected hobby to your controller action? I feel your action code could be simplified without all the session and transaction vars (unless there are details you are leaving out, giving reason to have such calls).
priya77
Member
49 Points
70 Posts
Re: getting error as "Object does not match target type. "
Apr 06, 2012 01:38 PM|LINK
First of all thank you so much sir for replying....
exactlywere are you pointig by this ??
well in my project HobbyHome is master table and othr tables like HobbyDetails, HobbyMaster are storing their primary key in it...
hence i need towrite this code....
in my HobbyHome i created objectof other table bt by saving only HobbyHome's object does not show me any error bt also dont save the data.....strange bt true....
Bimalvv
Contributor
2356 Points
478 Posts
Re: getting error as "Object does not match target type. "
Apr 06, 2012 01:39 PM|LINK
Can you tell me why you are invoking Save and SaveOrUpdate?
Basically Save method will insert an entry if the identifier doesn’t exist, else it will throw error. If the primary key already present in the table, it cannot be inserted and the SaveOrUpdate calls save() or update() based on the operation. If the identifier exists, it will call update method else the save method will be called.
NHibernate mvc3
Bimal
JohnLocke
Contributor
3216 Points
710 Posts
Re: getting error as "Object does not match target type. "
Apr 06, 2012 01:44 PM|LINK
Post your full view and action. It's hard to say what all you are trying to submit that you'd need to use HobbyHome as the model, when you only show the dropdownlist, which only represents an int type.
NHibernate mvc3
priya77
Member
49 Points
70 Posts
Re: getting error as "Object does not match target type. "
Apr 06, 2012 01:45 PM|LINK
In my HobbyHome class i have created foreign keys of other classes
and in every controller i am saving or updating some of them.....
so i wrote save or update.....
correct me if i am wrong....
NHibernate mvc3
priya77
Member
49 Points
70 Posts
Re: getting error as "Object does not match target type. "
Apr 06, 2012 01:49 PM|LINK
here is my view:
@using (Html.BeginForm("Edit","HobbyHomes",FormMethod.Post))
{
@Html.ValidationSummary(false)
<table>
<tr>
<td style="width:25%">
@Html.LabelFor(model=>model.Name)//this is coming from class inherited by HobbyHome
</td>
<td style="width:1%">:</td>
<td>
@Html.EditorFor(model=>model.Name)
</td>
</tr>
<tr>
<td style="width:25%">
@Html.LabelFor(model => model.hobbydetail.HobbyId)
</td>
<td style="width:1%">:</td>
<td>
@Html.DropDownListFor(model => model.hobbydetail.HobbyId, new SelectList(ViewBag.Hobby, "HobbyId", "HobbyName"))
</td>
</tr>
<tr>
<td style="width:25%">
@Html.LabelFor(model => model.hobbydetail.StudyMedium)
</td>
<td style="width:1%">:</td>
<td>
<input type="radio" name="medium" value="marathi">Marathi</input>
<input type="radio" name="medium" value="english">English</input>
</td>
</tr>
<tr>
<td style="width:25%">
@Html.LabelFor(model => model.hobbydetail.Fees)
</td>
<td style="width:1%">:</td>
<td>@Html.EditorFor(model => model.hobbydetail.Fees)</td>
</tr>
<tr>
<td style="width:25%">
@Html.LabelFor(model => model.hobbydetail.ForWhom)
</td>
<td style="width:1%">:</td>
<td>
<input type="radio" name="forwhom" value="m">Male</input>
<input type="radio" name="forwhom" value="f">Female</input>
</td>
</tr>
<tr>
<td style="width:25%">
@Html.LabelFor(model => model.hobbydetail.InclusiveStudyMaterial)
</td>
<td style="width:1%">:</td>
<td>
<input type="radio" name="studymaterial" value="true">Yes</input>
<input type="radio" name="studymaterial" value="false">No</input>
</td>
</tr>
<tr>
<td style="width:25%">
@Html.LabelFor(model => model.hobbydetail.AgeTo)
</td>
<td style="width:1%">:</td>
<td>@Html.EditorFor(model => model.hobbydetail.AgeTo)</td>
</tr>
<tr>
<td style="width:25%">
@Html.LabelFor(model => model.hobbydetail.AgeFrom)
</td>
<td style="width:1%">:</td>
<td>@Html.EditorFor(model => model.hobbydetail.AgeFrom)</td>
</tr>
<tr>
<td style="width:25%">
@Html.LabelFor(model => model.hobbydetail.HobbyInfo)
</td>
<td style="width:1%">:</td>
<td>@Html.EditorFor(model => model.hobbydetail.HobbyInfo)</td>
</tr>
</table>
</fieldset>
<center>
<p >
<input type="submit" name=@Resources.lblbtnSave value="@Resources.lblbtnSave " id="Save" />
</p>
</center>
}
and in action::
public ActionResult Edit()
{
ViewBag.Hobby = (ICollection<HobbyMasters>)(new HobbyDetailService().GetAllhobby());
return View();
}
[HttpPost]
//[ValidateOnlyIncomingValues]
public ActionResult Edit(HobbyHome homes, string forwhom, bool studymaterial, string medium)
{
ViewBag.Hobby = (ICollection<HobbyMasters>)(new HobbyDetailService().GetAllhobby());
Person person = FillProfile();
HobbyDeveloper developers = new HobbyDeveloper();
developers.LoginAccount = new LoginAccount();
developers.LoginAccount = person.LoginAccount;
developers.BasicProfile = person.BasicProfile;
homes.developer = developers;
//HobbyHomeAddress address = new HobbyHomeAddress();
if (ModelState.IsValid)
{
homes.hobbydetail.ForWhom = Convert.ToChar(forwhom);
homes.hobbydetail.StudyMedium = medium;
homes.hobbydetail.InclusiveStudyMaterial = studymaterial;
HobbyDetailService hobby = new HobbyDetailService();
hobby.SaveHobbyDetailWithHobbyHome(homes);
hobby.SaveHobbyDeveloperlWithHobbyHome(homes);
hobby.SaveHobbyHome(homes);
return RedirectToAction("Detail");
}
NHibernate mvc3
JohnLocke
Contributor
3216 Points
710 Posts
Re: getting error as "Object does not match target type. "
Apr 06, 2012 01:54 PM|LINK
Your foreign keys should never change so long as the primary key doesn't change. I'm looking over your view code. Give me a moment.
JohnLocke
Contributor
3216 Points
710 Posts
Re: getting error as "Object does not match target type. "
Apr 06, 2012 02:49 PM|LINK
Why are you storing certain values of a single hobby in different classes? It would appear easier if you had one class "HobbyClass" that contained all the details of the hobby. Then you wouldn't have to deal with multiple action parameters, nor bother with multiple methods of updating all the parameters into different tables.
I feel I can't help you with your current setup, so I'll provide my solution and you can use it or ignore it:
public class HobbyClass
{
public int HobbyId {get;set;}
public string Name {get;set}
public string StudyMedium {get;set;}
public decimal Fees {get;set;}
public string ForWhom {get;set;}
public bool InclusiveStudyMaterial {get;set;}
public int AgeTo {get;set;}
public int AgeFrom {get;set;}
public string HobbyInfo {get;set;}
}
YOUR VIEW:
@model YourApp.Models.HobbyClass
@using (Html.BeginForm("Edit","HobbyHomes",FormMethod.Post))
{
@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.HobbyId)
<table>
<tr>
<td>@Html.LabelFor(model => model.Name)</td>
<td>@Html.TextBoxFor(model =>model.Name)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.StudyMedium)</td>
<td>@Html.DropDownListFor(model => model.StudyMedium, new SelectList(ViewBag.StudyMed, "Value", "Text"))</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.Fees)</td>
<td>@Html.TextBoxFor(model =>model.Fees)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.ForWhom)</td>
<td>@Html.DropDownListFor(model => model.ForWhom, new SelectList(ViewBag.Whom, "Value", "Text"))</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.InclusiveStudyMaterial)</td>
<td>@Html.DropDownListFor(model => model.InclusiveStudyMaterial, new SelectList(ViewBag.Yesno, "Value", "Text"))</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.AgeTo)</td>
<td>@Html.TextBoxFor(model =>model.AgeTo)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.AgeFrom)</td>
<td>@Html.TextBoxFor(model =>model.AgeFrom)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.HobbyInfo)</td>
<td>@Html.TextBoxFor(model =>model.HobbyInfo</td>
</tr>
</table>
<input type="Submit" value="Update" />
}
YOUR CONTROLLER:
public List<SelectListItem> GetYesNo()
{
List<SelectListItem> yesNoItems = new List<SelectListItem>();
yesNoItems.Add(new SelectListItem { Text = "Yes", Value = "true" });
yesNoItems.Add(new SelectListItem { Text = "No", Value = "false" });
return yesNoItems;
}
public List<SelectListItem> GetMediums()
{
List<SelectListItem> mediumItems = new List<SelectListItem>();
mediumItems.Add(new SelectListItem { Text = "Marathi", Value = "marathi" });
mediumItems.Add(new SelectListItem { Text = "English", Value = "english" });
return mediumItems;
}
public List<SelectListItem> GetForWhom()
{
List<SelectListItem> whomItems = new List<SelectListItem>();
whomItems.Add(new SelectListItem { Text = "Male", Value = "m" });
whomItems.Add(new SelectListItem { Text = "Female", Value = "f" });
return whomItems;
}
public ActionResult Edit(int HobbyId)
{
var hobby = getHobbyById(HobbyId); // you'll need to create a method for getting a hobby by it's ID
ViewBag.Yesno = GetYesNo();
ViewBag.StudyMed = GetMediums();
ViewBag.Whom = GetForWhom();
return View(hobby);
}
[HttpPost]
public ActionResult Edit(HobbyClass model)
{
if (ModelState.IsValid)
{
try {
var hobby = getHobbyById(model.HobbyId);
UpdateModel(hobby);
return View("EditSuccess"); // whatever you want to return if successful
}
catch (Exception ex) {
ModelState.AddModelError("", ex.message);
}
}
else
{
ModelState.AddModelError("", "Correct the following errors");
}
ViewBag.Yesno = GetYesNo();
ViewBag.StudyMed = GetMediums();
ViewBag.Whom = GetForWhom();
return View(model); // this returns the form with validation errors
}
And again, you don't have to use this, but I feel it's a much easier way to manage your hobbies without having to update multiple tables. Hope this helps.