Insert Images into database using MVC4

Posted by Mandlaa under ASP.NET MVC on 11/13/2013 | Points: 10 | Views : 4272 | Status : [Member] | Replies : 3
This is my table

Id name Img Imag1 Img2

MYCODE:

[HttpPost, ValidateInput(false)]

public ActionResult Create(FormCollection collection, ProductsConyers2 p, HttpPostedFileBase[] MultipleFiles,TagTable tb)
{
try
{
// TODO: Add insert logic here

var Tagid = collection["TagNo"];
var qry = (from c in db.ProductsConyers2s where c.Id ==Id select c).Count();
if (qry != 1)
{
foreach (var fileBase in MultipleFiles)
{
if (fileBase.ContentLength > 0)
{
Microsoft.WindowsAzure.StorageClient.CloudBlobContainer blobContainer = _myBlobStorageService.GetCloudBlobContainer();
CloudBlob blob = blobContainer.GetBlobReference(fileBase.FileName);
blob.UploadFromStream(fileBase.InputStream);
}
}
List<string> blobs = new List<string>();

foreach (var fileBase in MultipleFiles)
{
Microsoft.WindowsAzure.StorageClient.CloudBlobContainer blobContainer1 = _myBlobStorageService.GetCloudBlobContainer();

CloudBlob blob1 = blobContainer1.GetBlobReference(fileBase.FileName);

blobs.Add(blob1.Uri.ToString());
}
p.Img = blobs.ElementAt(0).ToString();
p.Img1 = blobs.ElementAt(1).ToString();
p.Img2 = blobs.ElementAt(2).ToString();

db.ProductsConyers2s.InsertOnSubmit(p);
db.SubmitChanges();
return RedirectToAction("Products");

}

}

MYVIEW:

@using (Html.BeginForm("Create", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<legend>ProductsHondaofConyer</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ID)
@Html.ValidationMessageFor(model => model.ID)
</div>
<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.Img)
</div>
<div class="editor-field">
@*@Html.EditorFor(model => model.Img)*@
<input type="file" name="MultipleFiles" />
@Html.ValidationMessageFor(model => model.Img)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.Img1)
</div>
<div class="editor-field">
@*@Html.EditorFor(model => model.Img1)*@
<input type="file" name="MultipleFiles" />
@Html.ValidationMessageFor(model => model.Img1)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.Img2)
</div>
<div class="editor-field">
@*@Html.EditorFor(model => model.Img2)*@
<input type="file" name="MultipleFiles" />
@Html.ValidationMessageFor(model => model.Img2)
</div>

}

I am doing this working fine,

I can upload three images successfully uploaded

But I can upload 2 images 1 image not upload?

Anyone can change my code logic





Responses

Posted by: Bandi on: 11/13/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
May be this?

var qry = (from c in db.ProductsConyers2s where c.TagNo ==Tagid select c).Count(); 

if (qry != 0 ) // here you are checking for count=1... I think that should be 0

{


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 11/13/2013 [Member] Starter | Points: 25

Up
0
Down
var qry = (from c in db.ProductsConyers2s where c.TagNo ==Tagid select c).Count(); 

this code just check Id exit or not
This id already exist in database not insert data,


Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/13/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
>>But I can upload 2 images 1 image not upload?

Debug the code by putting break point to get know where the problem occurred ?

Not uploading means ? Are you getting any error or incorrect data into database?
What is the structure of table?

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response