Click here to Skip to main content
15,905,614 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can't seem to insert data into my table using a service-based database in C# Express. I am using Entity-Framework and I have the following code. But when I refresh the table in the database explorer, no data is found.
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CoffeeShopProject
{
    public partial class AddProduct : Form
    {
        private CoffeeShopDatabaseEntities cse = new CoffeeShopDatabaseEntities();
        private Byte[] byteBLOBData;

        public AddProduct()
        {
            InitializeComponent();
            cboCategory.DataSource = cse.tblProductTypes;
            cboCategory.DisplayMember = "Description";
            cboCategory.ValueMember = "ProductType";
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                FileStream fsBLOBFile = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                byteBLOBData = new Byte[fsBLOBFile.Length];
                fsBLOBFile.Read(byteBLOBData, 0, byteBLOBData.Length);
                fsBLOBFile.Close();
                MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
                pxbImage.Image = Image.FromStream(stmBLOBData);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            tblProduct products = new tblProduct();
            products.Description = txtDescription.Text;
            products.Price = int.Parse(txtPrice.Text);
            products.Image = byteBLOBData;
            products.ProductType = (int)cboCategory.SelectedValue;
            cse.SaveChanges();
            MessageBox.Show("Record Saved!");
        }
    }
}


What I have tried:

I have not tried anything yet. Just this. I have been searching online for solutions since last night but to no avail I have found none.
Posted
Updated 30-Aug-16 20:26pm

1 solution

The windows forms applications are regenerate bin folder every build. If do you every run in visual studio, regenerate bin folder. Also your saved data into database to under bin folder, but you looking to database in project folder! Service based databases are copy to bin folder every build as clearly.
 
Share this answer
 
Comments
Member 11075289 31-Aug-16 3:30am    
I am looking at the Database within the Database Explorer. I have also looked at the database within the bin folder and still there is no data within it.
Halit Yurttaş 31-Aug-16 5:26am    
Can you move to database non virtual path, e.g. c:\ and edit connection string for the path. Because the project based service databases are recreate risks, this way we cannot be sure. Best way is use moved out of project database

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