Ann K
asked on
Runtime Exceptions when trying to submit data
Where I have to find this error. It shows when I press the submit button. I am using VS 2013 version with SQL Server 2014 in windows 7.
ManageProduct.aspx
ManageProduct.aspx.cs
Error:System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'Price', table 'GarageDB.dbo.Product'; column does not allow nulls. INSERT fails. The statement has been terminated. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.b__c(DbCommand t, DbCommandInterceptionContext`1 c) at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext) at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues) at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() --- End of inner exception stack trace --- at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.b__2(UpdateTranslator ut) at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction) at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update() at System.Data.Entity.Core.Objects.ObjectContext.b__35() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction) at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.b__27() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation) at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction) at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options) at System.Data.Entity.Internal.InternalContext.SaveChanges() --- End of inner exception stack trace --- at System.Data.Entity.Internal.InternalContext.SaveChanges() at System.Data.Entity.Internal.LazyInternalContext.SaveChanges() at System.Data.Entity.DbContext.SaveChanges() at ProductModel.InsertProduct(Product product) in c:\Users\User\Documents\Visual Studio 2013\WebSites\GarageManager\App_Code\Models\ProductModel.cs:line 17
ManageProduct.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ManageProducts.aspx.cs" Inherits="Pages_Management_ManageProducts" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<p>
Name:</p>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
Type:<br />
<p>
<asp:DropDownList ID="ddlType" runat="server" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Id">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GarageDBConnectionString %>" SelectCommand="SELECT * FROM [ProductTypes] ORDER BY [Name]"></asp:SqlDataSource>
</p>
<p>
Price:</p>
<asp:TextBox ID="txtPrice" runat="server"></asp:TextBox>
<br />
Image:<br />
<p>
<asp:DropDownList ID="ddlImage" runat="server">
</asp:DropDownList>
</p>
<p>
Description:</p>
<asp:TextBox ID="txtDescription" runat="server" Height="84px" TextMode="MultiLine" Width="234px"></asp:TextBox>
<br />
<p>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</p>
<p>
<asp:Label ID="lblResult" runat="server"></asp:Label>
</p>
</asp:Content>
ManageProduct.aspx.cs
using System;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Pages_Management_ManageProducts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
GetImages();
}
private void GetImages()
{
try
{
//Get all filepaths
string[] images = Directory.GetFiles(Server.MapPath("~/Images/Products/"));
ArrayList imageList = new ArrayList();
foreach (string image in images)
{
string imageName = image.Substring(image.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
imageList.Add(imageName);
}
// Set the arrayList as the dropdownview's datasource and refresh
ddlImage.DataSource = imageList;
ddlImage.AppendDataBoundItems = true;
ddlImage.DataBind();
}
catch(Exception e)
{
lblResult.Text = e.ToString();
}
}
private Product CreateProduct()
{
Product product = new Product();
product.Name = txtName.Text;
//product.Price = Convert.ToDouble(txtPrice.Text);
product.TypeId = Convert.ToInt32(ddlType.SelectedValue);
product.Description = txtDescription.Text;
product.Image = ddlImage.SelectedValue;
return product;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
ProductModel productModel = new ProductModel();
Product product = CreateProduct();
lblResult.Text = productModel.InsertProduct(product);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try this...
Can you please tell me what is the value you are passing in txtPrice.Text.
Replace the error line with this..
float.Parse((txtPrice.Text ).Replace( ",", ".") , System.Globalization.Cultu reInfo.Inv ariantCult ure);
Hope it helps !
Can you please tell me what is the value you are passing in txtPrice.Text.
Replace the error line with this..
float.Parse((txtPrice.Text
Hope it helps !
Can you post the Product class.
I'am thinking that Price in the Product class is defined as a int data type and not as double.
Can you please tell me what is the value you are passing in txtPrice.Text.
Are you converting double to introduce.
Are you converting double to introduce.
ASKER
ProductModel.cs
ProductTypeModel.cs
CartModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ProductModel
/// </summary>
public class ProductModel
{
public string InsertProduct(Product product)
{
try
{
GarageDBEntities db = new GarageDBEntities();
db.Products.Add(product);
db.SaveChanges();
return product.Name + "was successfully inserted";
}
catch (Exception e)
{
return "Error:" + e;
}
}
public string UpdateProduct(int id, Product product)
{
try
{
GarageDBEntities db = new GarageDBEntities();
// Fetch object from db
Product p = db.Products.Find(id);
p.Name = product.Name;
p.Price = product.Price;
p.TypeId = product.TypeId;
p.Description = product.Description;
p.Image = product.Image;
db.SaveChanges();
return product.Name + "was successfully updated";
}
catch (Exception e)
{
return "Error:" + e;
}
}
public string DeleteProduct(int id)
{
try
{
GarageDBEntities db = new GarageDBEntities();
Product product = db.Products.Find(id);
db.Products.Attach(product);
db.Products.Remove(product);
db.SaveChanges();
return product.Name + "was successfully deleted";
}
catch (Exception e)
{
return "Error:" + e;
}
}
public Product GetProduct(int id)
{
try
{
using (GarageDBEntities db = new GarageDBEntities())
{
Product product = db.Products.Find(id);
return product;
}
}
catch (Exception)
{
return null;
}
}
public List<Product> GetAllProducts()
{
try
{
using (GarageDBEntities db = new GarageDBEntities())
{
List<Product> products = (from x in db.Products
select x).ToList();
return products;
}
}
catch (Exception)
{
return null;
}
}
public List<Product> GetProductsByType(int typeId)
{
try
{
using (GarageDBEntities db = new GarageDBEntities())
{
List<Product> products = (from x in db.Products
where x.TypeId == typeId
select x).ToList();
return products;
}
}
catch (Exception)
{
return null;
}
}
}
ProductTypeModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ProductTypeTypeModel
/// </summary>
public class ProductTypeModel
{
public string InsertProductType(ProductType productType)
{
try
{
GarageDBEntities db = new GarageDBEntities();
db.ProductTypes.Add(productType);
db.SaveChanges();
return productType.Name + "was successfully inserted";
}
catch (Exception e)
{
return "Error:" + e;
}
}
public string UpdateProductType(int id, ProductType productType)
{
try
{
GarageDBEntities db = new GarageDBEntities();
// Fetch object from db
ProductType p = db.ProductTypes.Find(id);
p.Name = productType.Name;
db.SaveChanges();
return productType.Name + "was successfully updated";
}
catch (Exception e)
{
return "Error:" + e;
}
}
public string DeleteProductType(int id)
{
try
{
GarageDBEntities db = new GarageDBEntities();
ProductType productType = db.ProductTypes.Find(id);
db.ProductTypes.Attach(productType);
db.ProductTypes.Remove(productType);
db.SaveChanges();
return productType.Name + "was successfully deleted";
}
catch (Exception e)
{
return "Error:" + e;
}
}
}
CartModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for CartModel
/// </summary>
public class CartModel
{
public string InsertCart(Cart cart)
{
try
{
GarageDBEntities db = new GarageDBEntities();
db.Carts.Add(cart);
db.SaveChanges();
return cart.DatePurchased + "was successfully inserted";
}
catch (Exception e)
{
return "Error:" + e;
}
}
public string UpdateCart(int id, Cart cart)
{
try
{
GarageDBEntities db = new GarageDBEntities();
// Fetch object from db
Cart p = db.Carts.Find(id);
p.DatePurchased = cart.DatePurchased;
p.ClientID = cart.ClientID;
p.Amount = cart.Amount;
p.IsInCart = cart.IsInCart;
p.ProductID = cart.ProductID;
db.SaveChanges();
return cart.DatePurchased + "was successfully updated";
}
catch (Exception e)
{
return "Error:" + e;
}
}
public string DeleteCart(int id)
{
try
{
GarageDBEntities db = new GarageDBEntities();
Cart cart = db.Carts.Find(id);
db.Carts.Attach(cart);
db.Carts.Remove(cart);
db.SaveChanges();
return cart.DatePurchased + "was successfully deleted";
}
catch (Exception e)
{
return "Error:" + e;
}
}
}
ASKER
>Can you please tell me what is the value you are passing in txtPrice.Text.
Just trying to pass a number into a table.
Just trying to pass a number into a table.
From your last post I do not see a class called Product.
ASKER
sorry. Its an autogenerated class.
public partial class Product
{
public int Id { get; set; }
public int TypeId { get; set; }
public string Name { get; set; }
public Nullable<int> Price { get; set; }
public string Description { get; set; }
public string Image { get; set; }
public virtual ProductType ProductType { get; set; }
}
Below should be like
public Nullable<int> Price { get; set; } ----- > public float Price { get; set; }
public Nullable<int> Price { get; set; } ----- > public float Price { get; set; }
ASKER
I tried it with the change and it gives this error?
Error:System.Data.Entity.Core.MappingException: The type 'Edm.Int32' of the member 'Price' in the conceptual side type 'GarageModel.Product' does not match with the type 'System.Single' of the member 'Price' on the object side type 'Product'. at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.ValidateMembersMatch(EdmMember edmMember, EdmMember objectMember) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadEntityTypeOrComplexTypeMapping(ObjectTypeMapping objectMapping, EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadObjectMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadTypeMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadAssociationTypeMapping(ObjectTypeMapping objectMapping, EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadObjectMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadTypeMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadEntityTypeOrComplexTypeMapping(ObjectTypeMapping objectMapping, EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadObjectMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadTypeMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadAssociationTypeMapping(ObjectTypeMapping objectMapping, EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadObjectMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadTypeMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadEntityTypeOrComplexTypeMapping(ObjectTypeMapping objectMapping, EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadObjectMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadObjectMapping(EdmType cdmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.GetDefaultMapping(EdmType cdmType, EdmType clrType) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.TryGetMap(String identity, DataSpace typeSpace, Boolean ignoreCase, MappingBase& map) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.TryGetMap(String identity, DataSpace typeSpace, MappingBase& map) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.TryGetMap(GlobalItem item, MappingBase& map) at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.TryGetMap(GlobalItem item, DataSpace dataSpace, MappingBase& map) at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.TryGetEdmSpaceType[T](T objectSpaceType, T& edmSpaceType) at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.GetEdmSpaceType[T](T objectSpaceType) at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.GetEdmSpaceType(StructuralType objectSpaceType) at System.Data.Entity.Core.Metadata.Edm.MetadataOptimization.UpdateEntitySetMappings() at System.Data.Entity.Core.Metadata.Edm.MetadataOptimization.TryUpdateEntitySetMappingsForType(Type entityType) at System.Data.Entity.Internal.InternalContext.TryUpdateEntitySetMappingsForType(Type entityType) at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType) at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity) at System.Data.Entity.DbSet`1.Add(TEntity entity) at ProductModel.InsertProduct(Product product) in c:\Users\User\Documents\Visual Studio 2013\WebSites\GarageManager\App_Code\Models\ProductModel.cs:line 16
ASKER
ManageProduct.aspx.cs
Partial class Product
using System;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Pages_Management_ManageProducts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
GetImages();
}
private void GetImages()
{
try
{
//Get all filepaths
string[] images = Directory.GetFiles(Server.MapPath("~/Images/Products/"));
ArrayList imageList = new ArrayList();
foreach (string image in images)
{
string imageName = image.Substring(image.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
imageList.Add(imageName);
}
// Set the arrayList as the dropdownview's datasource and refresh
ddlImage.DataSource = imageList;
ddlImage.AppendDataBoundItems = true;
ddlImage.DataBind();
}
catch(Exception e)
{
lblResult.Text = e.ToString();
}
}
private Product CreateProduct()
{
Product product = new Product();
product.Name = txtName.Text;
//product.Price = Convert.ToDouble(txtPrice.Text);
product.Price =float.Parse(txtPrice.Text, System.Globalization.CultureInfo.InvariantCulture);
product.TypeId = Convert.ToInt32(ddlType.SelectedValue);
product.Description = txtDescription.Text;
product.Image = ddlImage.SelectedValue;
return product;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
ProductModel productModel = new ProductModel();
Product product = CreateProduct();
lblResult.Text = productModel.InsertProduct(product);
}
}
Partial class Product
public partial class Product
{
public int Id { get; set; }
public int TypeId { get; set; }
public string Name { get; set; }
//public Nullable<int> Price { get; set; }
public float Price { get; set; }
public string Description { get; set; }
public string Image { get; set; }
public virtual ProductType ProductType { get; set; }
}
The data type in the database is not matching what you have in code which was my suspicion let me look at your last post and get back to you.
What happens when you make the Product class like this
public partial class Product
{
public int Id { get; set; }
public int TypeId { get; set; }
public string Name { get; set; }
//public Nullable<int> Price { get; set; }
public Double Price { get; set; }
public string Description { get; set; }
public string Image { get; set; }
public virtual ProductType ProductType { get; set; }
}
ASKER
It gives that error.
Error:System.Data.Entity.Core.MappingException: The type 'Edm.Int32' of the member 'Price' in the conceptual side type 'GarageModel.Product' does not match with the type 'System.Double' of the member 'Price' on the object side type 'Product'. at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.ValidateMembersMatch(EdmMember edmMember, EdmMember objectMember) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadEntityTypeOrComplexTypeMapping(ObjectTypeMapping objectMapping, EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadObjectMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadTypeMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadAssociationTypeMapping(ObjectTypeMapping objectMapping, EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadObjectMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadTypeMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadEntityTypeOrComplexTypeMapping(ObjectTypeMapping objectMapping, EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadObjectMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadTypeMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadAssociationTypeMapping(ObjectTypeMapping objectMapping, EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadObjectMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadTypeMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadEntityTypeOrComplexTypeMapping(ObjectTypeMapping objectMapping, EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadObjectMapping(EdmType edmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection, Dictionary`2 typeMappings) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.LoadObjectMapping(EdmType cdmType, EdmType objectType, DefaultObjectMappingItemCollection ocItemCollection) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.GetDefaultMapping(EdmType cdmType, EdmType clrType) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.TryGetMap(String identity, DataSpace typeSpace, Boolean ignoreCase, MappingBase& map) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.TryGetMap(String identity, DataSpace typeSpace, MappingBase& map) at System.Data.Entity.Core.Mapping.DefaultObjectMappingItemCollection.TryGetMap(GlobalItem item, MappingBase& map) at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.TryGetMap(GlobalItem item, DataSpace dataSpace, MappingBase& map) at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.TryGetEdmSpaceType[T](T objectSpaceType, T& edmSpaceType) at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.GetEdmSpaceType[T](T objectSpaceType) at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.GetEdmSpaceType(StructuralType objectSpaceType) at System.Data.Entity.Core.Metadata.Edm.MetadataOptimization.UpdateEntitySetMappings() at System.Data.Entity.Core.Metadata.Edm.MetadataOptimization.TryUpdateEntitySetMappingsForType(Type entityType) at System.Data.Entity.Internal.InternalContext.TryUpdateEntitySetMappingsForType(Type entityType) at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType) at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity) at System.Data.Entity.DbSet`1.Add(TEntity entity) at ProductModel.InsertProduct(Product product) in c:\Users\User\Documents\Visual Studio 2013\WebSites\GarageManager\App_Code\Models\ProductModel.cs:line 16
public partial class Product
{
public int Id { get; set; }
public int TypeId { get; set; }
public string Name { get; set; }
//public Nullable<int> Price { get; set; }
public Double Price { get; set; }
public string Description { get; set; }
public string Image { get; set; }
public virtual ProductType ProductType { get; set; }
}
using System;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Pages_Management_ManageProducts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
GetImages();
}
private void GetImages()
{
try
{
//Get all filepaths
string[] images = Directory.GetFiles(Server.MapPath("~/Images/Products/"));
ArrayList imageList = new ArrayList();
foreach (string image in images)
{
string imageName = image.Substring(image.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
imageList.Add(imageName);
}
// Set the arrayList as the dropdownview's datasource and refresh
ddlImage.DataSource = imageList;
ddlImage.AppendDataBoundItems = true;
ddlImage.DataBind();
}
catch(Exception e)
{
lblResult.Text = e.ToString();
}
}
private Product CreateProduct()
{
Product product = new Product();
product.Name = txtName.Text;
product.Price = Convert.ToDouble(txtPrice.Text);
//product.Price =float.Parse(txtPrice.Text, System.Globalization.CultureInfo.InvariantCulture);
product.TypeId = Convert.ToInt32(ddlType.SelectedValue);
product.Description = txtDescription.Text;
product.Image = ddlImage.SelectedValue;
return product;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
ProductModel productModel = new ProductModel();
Product product = CreateProduct();
lblResult.Text = productModel.InsertProduct(product);
}
}
In the database did you change the data type for Price in the Product table?
ASKER
It won't allow me to change.
I don't know if you can but if you can zip up the complete project as well as the database and place it somewhere it can be downloaded I will take a look at it.
ASKER
When I download the zip file and unzipped it all I found was two files and the package folder which contains the EF dll code but no project files. Also zipping the database is the same as any other file just go to the location of the database on the file system and zip it or add it to the zip file.
ASKER
I am using 2013 version. I don't know where exactly the .cs files are. Try this one name FILES.ZIP. Also can you tell me where exactly SQL Server save the database? I am using 2014 version.
http://ge.tt/2duvu2i2
http://ge.tt/2duvu2i2
Sorry but that was the same as your previous zip file and still no database.
ASKER
I created an email address on yahoo. Try to download from their.
wilsonrita803@yahoo.com
Password> Tishem12
wilsonrita803@yahoo.com
Password> Tishem12
ASKER
I just find a query to alter table.
UPDATE [dbo].[Product]
SET [Price] = CAST([Price] as decimal(28, 6))
WHERE [Price] - FLOOR([Price]) < 0.000001;
ALTER TABLE [dbo].[Product] ALTER COLUMN [Price] DECIMAL(28,6);
UPDATE [dbo].[Product]
SET [Price] = CAST([Price] as decimal(28, 6))
WHERE [Price] - FLOOR([Price]) < 0.000001;
ALTER TABLE [dbo].[Product] ALTER COLUMN [Price] DECIMAL(28,6);
Lets deal with the database first. The best way to find where the database files are located on the system is to run the Microsoft application called Microsoft SQL Server Management Studio. When you have logged in expanding the Database node will show all the databases attached to the SQL Server. Right click on the database name that you are using in the application and select the Properties option short cut menu. When the Properties window opens select Files under "Select a page" and note the Path and File Name columns.
To get the location of the files in the project in Visual Studio go to the Solution Explorer window and right click on the Solution Name, top most text in the tree view and select "Open folder in File Explorer" in the short cut menu. When the file explorer opens go up one directory and that directory should contain the complete project.
To get the location of the files in the project in Visual Studio go to the Solution Explorer window and right click on the Solution Name, top most text in the tree view and select "Open folder in File Explorer" in the short cut menu. When the file explorer opens go up one directory and that directory should contain the complete project.
ASKER