Answered by:
Insert audio in database in asp.net mvc 3

Question
-
I am inserting audio in database using entity framework. Here's a description:-
In upload page user can upload their song by selecting type like Hindi, Punjabi, Old and English. If user select Punjabi then that audio and data will save into Punjabi database that i create in sql server 2008 and it will also save in folder under "Media/Punjabi". The problem is its not inserting into database. It not showing any error or run-time exception. Can anyone please help.
Here's a code:-Model Class:-
public class Upload_song
{
[Required(ErrorMessage = "Please enter singer name")]
[DisplayName("Enter singer name")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter email id")]
[DataType(DataType.EmailAddress)]
[DisplayName("Enter your emailid")]
public string Email { get; set; }
[Required(ErrorMessage = "Please enter your country")]
[DisplayName("Enter your Country")]
public string Country { get; set; }
[Required(ErrorMessage = "Please enter your state")]
[DisplayName("Enter your State")]
public string State { get; set; }
[Required(ErrorMessage = "Please enter your city")]
[DisplayName("Enter your City")]
public string City { get; set; }
[Required(ErrorMessage = "Please enter your song")]
[DisplayName("Enter your Song")]
public HttpPostedFileBase Song { get; set; }
[Required(ErrorMessage = "Please enter the type")]
[DisplayName("Enter type of song")]
public string Song_type { get; set; }
}HomeController.cs
public ActionResult Upload(Upload_song us)
{
return View();
}[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
Upload_song us=new Upload_song();
if (us.Song_type == "Punjabi")
{
if (file.ContentLength > 0)
{
var filename = Path.GetFileName(file.FileName);
var audiopath = Path.Combine(Server.MapPath("~/Media/Punjabi"), filename);
file.SaveAs(audiopath);
ae.AddToPunjabis(us);
ae.SaveChanges();
}
ViewBag.Message = "Upload successfully";
}
return View();
}Upload.csHtml
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype="multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Upload Song</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Country)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Country)
@Html.ValidationMessageFor(model => model.Country)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.State)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.State)
@Html.ValidationMessageFor(model => model.State)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.City)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.City)
@Html.ValidationMessageFor(model => model.City)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Song)
</div>
<div class="editor-field">
<input type="file" name="song" id="file" />
</div>
<div class="editor-label">
<select name="song type" class="upload_text">
<option>-Select song type-</option>
<option>Old is Gold</option>
<option>Hindi songs</option>
<option>Punjabi songs</option>
<option>English songs</option>
</select>
</div>
<p>
<input type="submit" value="Create" style="font-size:medium" />
</p>Can anyone help me regarding this question.
- Moved by Caillen Monday, April 13, 2015 7:03 AM
Saturday, April 11, 2015 8:04 AM
Answers
-
Hi Ankush,
Please post in ASP.NET MVC forums here for better answers:
http://forums.asp.net/1146.aspx/1?MVC
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.Monday, April 13, 2015 6:55 AM
All replies
-
Hi, Ankush
In action Upload you have initialized the object Upload_song=new Upload_song() and then you are checking it, if its song type is "Punjabi" or not. It will not get the values from View and instead work with default values.
Instead you must pass the model inside parameter along with HttpPostedFileBase parameter from View to Upload Action.
Your action should look like:
public ActionResult Upload(HttpPostedFileBase file, Upload_song us) { if (us.Song_type == "Punjabi") { if (file.ContentLength > 0) { var filename = Path.GetFileName(file.FileName); var audiopath = Path.Combine(Server.MapPath("~/Media/Punjabi"), filename); file.SaveAs(audiopath); ae.AddToPunjabis(us); ae.SaveChanges(); ViewBag.Message = "Upload successfully"; } else { ViewBag.Message = "Error!!!"; } } else { ViewBag.Message = "Error! Song Type Not Found!"; } return View(); }
- Edited by Sanyam_Jain Saturday, April 11, 2015 10:54 AM
Saturday, April 11, 2015 10:49 AM -
Hi Ankush,
Please post in ASP.NET MVC forums here for better answers:
http://forums.asp.net/1146.aspx/1?MVC
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.Monday, April 13, 2015 6:55 AM -
Monday, April 13, 2015 4:12 PM