//Get the Image of Product for CRM Online.
public static DataTable GetProductWithImages()
{
try
{
OrganizationService _service = new OrganizationService(CRMConnection);
DataTable dTable = new DataTable();
dTable.Columns.Add("Name");
dTable.Columns.Add("Id");
dTable.Columns.Add("Image",typeof(byte[]));
string Fetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='product'>
<attribute name='name' />
<attribute name='producttypecode' />
<attribute name='productnumber' />
<attribute name='entityimage' />
<attribute name='productid' />
<order attribute='name' descending='false' />
<filter type='and'>
<filter type='or'>
<condition attribute='statecode' operator='eq' value='0' />
<condition attribute='statecode' operator='in'>
<value>3</value>
<value>0</value>
</condition>
</filter>
</filter>
</entity>
</fetch>";
EntityCollection Product = Common._service.RetrieveMultiple(new FetchExpression(Fetch));
if (Product != null && Product.Entities.Count > 0)
{
foreach (Entity ent in Product.Entities)
{
DataRow dRow = dTable.NewRow();
if (ent.Attributes.Contains("name"))
dRow["Name"] = ent.Attributes["name"].ToString();
if (ent.Attributes.Contains("productid"))
{
string ProductId = ent.Attributes["productid"].ToString();
dRow["Id"] = ProductId;
}
if (en.Attributes.Contains("entityimage"))
{
dRow["Image"] = en.Attributes["entityimage"] as byte[];
}
else
dRow["Image"] = null;
dTable.Rows.Add(dRow);
}
}
return dTable;
}
catch (Exception ex)
{
return null;
}
}
//**** now to display in asp gridview do the follwing:
// **** In ASPX*****
<asp:GridView ID="grvProduct" runat="server" AutoGenerateColumns="False" Width="90%">
<Columns>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<img src='<%# Eval("Image") != System.DBNull.Value ? string.Format("data:image/jpg;base64,{0}",Convert.ToBase64String((byte[])Eval("Image"))) : "Images/No_image.jpg" %>' alt="image" height="144" width="144" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
//**** In code behind *****
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillGrid();
}
}
public void FillGrid()
{
grvProduct.DataSource = GetProductWithImages();
grvProduct.DataBind();
}
ليست هناك تعليقات:
إرسال تعليق