Ann K
asked on
Data is not showing from images
Can you tell me why its not showing data from images?
Index.aspx.cs
Index.aspx.cs
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Get a list of all products in DB
ProductModel model = new ProductModel();
List<Product> products = model.GetAllProducts();
//Meke sure products exist in the database
if (products != null)
{
//Create a new Panel with an ImageButton and 2 labels for each Product
foreach (Product product in products)
{
Panel productPanel = new Panel();
ImageButton imageButton = new ImageButton
{
ImageUrl = "~/Images/Products/" + product.Image,
CssClass = "productImage",
PostBackUrl = string.Format("~/Pages/Product.aspx?id={0}", product.Id)
};
Label lblName = new Label
{
Text = product.Name,
CssClass = "productName"
};
Label lblPrice = new Label
{
Text = "$ " + product.Price,
CssClass = "productPrice"
};
productPanel.Controls.Add(imageButton);
productPanel.Controls.Add(new Literal { Text = "<br/>" });
productPanel.Controls.Add(lblName);
productPanel.Controls.Add(new Literal { Text = "<br/>" });
productPanel.Controls.Add(lblPrice);
//Add dynamic controls to static control
pnlProducts.Controls.Add(productPanel);
}
}
else
//No products found
pnlProducts.Controls.Add(new Literal { Text = "No products found!" });
}
}
ASKER
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;
}
}
}
Hi Ann,
Could you please debug the project and tell me what is coming in the products?
List<Product> products = model.GetAllProducts();
I suspect that GetAllProducts() is returning NULL.
Hope it helps!
Could you please debug the project and tell me what is coming in the products?
List<Product> products = model.GetAllProducts();
I suspect that GetAllProducts() is returning NULL.
Hope it helps!
Hi Ann,
Could you please debug below function and give me the screen shot. This is returning NULL thats why we are getting NULL in products.
public List<Product> GetAllProducts()
I suspect some connection issue.
Hope it helps !!
Could you please debug below function and give me the screen shot. This is returning NULL thats why we are getting NULL in products.
public List<Product> GetAllProducts()
I suspect some connection issue.
Hope it helps !!
Hi Ann,
Please put the debug point at below mentioned point and then provide me the value after the execution.
try
{
using (GarageDBEntities db = new GarageDBEntities())
{
Product product = db.Products.Find(id);
return product;
}
} /*pawan*/
Please put the debug point at below mentioned point and then provide me the value after the execution.
try
{
using (GarageDBEntities db = new GarageDBEntities())
{
Product product = db.Products.Find(id);
return product;
}
} /*pawan*/
ASKER
It didn't stop at this line.
Ohhh , So issue must be at the below bold line. Place the debug at below bold line and PRESS F11 then the cursor will mode inside of GarageDBEntities and then check where the error is coming and what error is coming.
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> GetAllProducts()
{
try
{
using (GarageDBEntities db = new GarageDBEntities())
{
List<Product> products = (from x in db.Products
select x).ToList();
return products;
}
}
catch (Exception)
{
return null;
}
}
Hi,
What did you press at using (GarageDBEntities db = new GarageDBEntities())
F10 or F11 ?
You have to press F11. With F11 it will go inside of GarageDBEntities. Can you provide me code of GarageDBEntities.
Your code is not able to connect to the database. Thats the issue.
Hope it helps!
What did you press at using (GarageDBEntities db = new GarageDBEntities())
F10 or F11 ?
You have to press F11. With F11 it will go inside of GarageDBEntities. Can you provide me code of GarageDBEntities.
Your code is not able to connect to the database. Thats the issue.
Hope it helps!
ASKER
I use F11
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class GarageDBEntities : DbContext
{
public GarageDBEntities()
: base("name=GarageDBEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Cart> Carts { get; set; }
public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<ProductType> ProductTypes { get; set; }
}
Hi,
EDMX mapping may be the issue. Delete and recreate that and try.
Hope it helps!
EDMX mapping may be the issue. Delete and recreate that and try.
Hope it helps!
ASKER
How to I delete and recreate that?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
List<Product> products = model.GetAllProducts();
products are coming NULL that why it is showing No Products Found.. Since it going in the else part. It is not going in the block where we are setting in the images.
Can you please why you are getting NULL in products, What query you are using in the background.
Hope it helps!