Question

I have web application in asp.net 3.5. I just want to add record to 3 tables and one table contains multiple file details that I associated with table primary key value.

For more info I include this code:

protected void submit_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            int user_id = 0;
            var query = from u in db.Users
                        where u.Username == (String)Session["Username"]
                        select new
                        {
                            Id = u.Id
                        };
            foreach (var item in query)
            {
                if (item != null)
                {
                    user_id = int.Parse(item.Id.ToString());
                    break;
                }
            }
            Post myPost = new Post();
            myPost.Title = txt_ComName.Text.Trim();
            myPost.Category_id = int.Parse(DDL_Categorynames.SelectedItem
                                .Value.ToString());
            myPost.Description = txt_ComName1.Text.Trim();
            myPost.User_id = user_id;
            myPost.ToUser_id = user_id;
            if(file_upload.HasFile)
            {
                myPost.IsFileAttached = true;
            }
            else
            {
                myPost.IsFileAttached = false;
            }
            db.Posts.InsertOnSubmit(myPost);
            db.SubmitChanges();
            int newId = myPost.Id;
            Flag myFlag = new Flag();
            myFlag.IsRead = false;
            myFlag.IsImportant = false;
            myFlag.IsRemoved = false;
            myFlag.User_id = user_id;
            myFlag.Post_History_id = newId;
            db.Flags.InsertOnSubmit(myFlag);
            db.SubmitChanges();
            File myFile = new File();
            HttpFileCollection fileCollection = Request.Files;
            for (int i = 0; i < fileCollection.Count; i++)
            {
                HttpPostedFile uploadfile = fileCollection[i];
                string fileName = Path.GetFileName(uploadfile.FileName);
                string fileType = System.IO.Path.GetExtension(fileName)
                                 .ToString().ToLower();
                myFile.Post_History_id = newId;
                myFile.File_name = fileName;
                myFile.File_ext = fileType;
                myFile.File_Size = uploadfile.ContentLength.ToString();
                if (uploadfile.ContentLength > 0)
                {
                    uploadfile.SaveAs(Server.MapPath("~/PostFiles/")
                                                           + fileName);
                    db.Files.InsertOnSubmit(myFile);
                    db.SubmitChanges();
                }
            }
            Panel_AddNew.Visible = false;
            Panel_ViewPostList.Visible = true;
            this.FillGrid();
        }
    }
}

However, with this scenario first file that was selected that was inserted and after that second file iterated then error occurred like:

Cannot add an entity that already exists.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Cannot add an entity that already exists.

Source Error:

Line 248: {
Line 249: uploadfile.SaveAs(Server.MapPath("~/PostFiles/") + FileName);
Line 250: db.Files.InsertOnSubmit(myFile);
Line 251: db.SubmitChanges();
Line 252: }

Source File: f:\EasyWeb\EndUser\Post_History.aspx.cs Line: 250

please help me...

Was it helpful?

Solution

Create a new File in each iteration of the foreach loop:

for (int i = 0; i < fileCollection.Count; i++)
{
    File myFile = new File();
    HttpPostedFile uploadfile = fileCollection[i];
    ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top