Complete Item Listing

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
vashts1980
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 98
Joined: Fri Apr 29, 2011 2:56 pm

Complete Item Listing

Post by vashts1980 » Wed Feb 29, 2012 11:17 am

During my integration process, I ran into a situation where there was a problem finding a match between AbleCommerce and the other system. Using C#, I'd like to be able to pull the entire list of items and their variants so I can do a comparison and make sure there's only a one-to-one ratio of matches. I know the code necessary for finding the matches in the other system, but I don't know how I would pull up the entire product listing and variants from within AbleCommerce. My thoughts were to have this run in a code-behind then display the results to a field on the related page.

Along these same lines, I'm also unsure as to how to add a page to the site and have it recognized. Part of my work is going to rely upon a special custom page that will not be available to visitors, and what I need to do here for this particular concern will apply to that work, as well.

vashts1980
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 98
Joined: Fri Apr 29, 2011 2:56 pm

Re: Complete Item Listing

Post by vashts1980 » Wed Feb 29, 2012 6:10 pm

Ok, I've actually figured out how to get an extra page set up in the system, and I've found the database schema for the version I'm working with. Now... Can someone instead point me to something that describes now to properly set up the database connection so that I can submit my SQL query?

I keep getting the following error message in the logs..
Exception of type 'System.Web.HttpUnhandledException' was thrown.; ExecuteReader: Connection property has not been initialized.

User avatar
jmestep
AbleCommerce Angel
Posts: 8164
Joined: Sun Feb 29, 2004 8:04 pm
Location: Dayton, OH
Contact:

Re: Complete Item Listing

Post by jmestep » Thu Mar 01, 2012 6:49 am

If you add using for CommerceBuilder.Data, you can get the database by Token.Instance.Database. You don't need to do any connection opening, closing, etc. if you do it that way. You might need to add a using to CommerceBuilder.Common also.
Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx

vashts1980
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 98
Joined: Fri Apr 29, 2011 2:56 pm

Re: Complete Item Listing

Post by vashts1980 » Thu Mar 01, 2012 11:18 am

Will that work without a codebehind page? I had to switch to having everything operate in an ASPX page without a codebehind. For some reason I got nothing but problems when trying to use one.

vashts1980
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 98
Joined: Fri Apr 29, 2011 2:56 pm

Re: Complete Item Listing

Post by vashts1980 » Thu Mar 01, 2012 12:34 pm

FYI, I was going with some sample code provided in the wiki that looks like this...

Code: Select all

CommerceBuilder.Data.Database database = Token.Instance.Database;
String sql = ("SQLQUERY");
using (System.Data.Common.DbCommand selectCommand = database.GetSqlStringCommand(sql))
{
    ...SQL results handling
}
The best thing I can think of for handling this is using this...

Code: Select all

System.Data.Common.DbDataReader dbReader = selectCommand.ExecuteReader()
...but that's what causes the error mentioned above.

User avatar
jmestep
AbleCommerce Angel
Posts: 8164
Joined: Sun Feb 29, 2004 8:04 pm
Location: Dayton, OH
Contact:

Re: Complete Item Listing

Post by jmestep » Fri Mar 02, 2012 7:18 am

You could try putting the code on the aspx page within script tags-kind of like Able does in the checkout payment page. They are .ascx files there, but with no code behind.
<%@ Control Language="C#" ClassName="CheckPaymentForm" EnableViewState="false" %>
<script runat="server">
C# stuff
</script>
Html stuff
Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx

JaminLeather
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 27
Joined: Fri Feb 05, 2010 9:37 am
Location: Myrtle Beach, South Carolina
Contact:

Re: Complete Item Listing

Post by JaminLeather » Fri Mar 02, 2012 11:25 am

You could create your own query, but for pulling a list of all products along with any variants, you could use the existing CommerceBuilder methods. Try something like this...

Code: Select all

...
using CommerceBuilder.Products;
...
        ProductCollection allProducts = CommerceBuilder.Products.ProductDataSource.LoadForStore("Sku");
        foreach (Product selectedProduct in allProducts)  // Loop through all products
        {
            OptionCollection _OptionCollection = selectedProduct.GetOptions();
            foreach (Option _Option in _OptionCollection) // Loop through all product choices
            {
                OptionChoiceCollection _OptionChoice = OptionDataSource.Load(_Option.OptionId).Choices;
                foreach (OptionChoice opt in _OptionChoice) // Loop through each choice
                {
                  // Do whatever you want with each product variant
                }
            }
        }
...
Jamin' Leather Catalog Outlet
http://www.jaminleather.com

vashts1980
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 98
Joined: Fri Apr 29, 2011 2:56 pm

Re: Complete Item Listing

Post by vashts1980 » Mon Mar 05, 2012 11:54 am

Thanks everyone!

Actually doing an SQL query was truly the best way I wanted to go as it made it easy to get exactly what I wanted and how. But I'll keep those AC methods in mind for next time.

I was able to do an item comparison between the two different systems, and it looks like the real problems I had were either items that had typos (or not written to match in both systems) and not properly typing the items for the target system.

This is what ended up working for me, in case anyone is interested...

Code: Select all

CommerceBuilder.Data.Database database = Token.Instance.Database;
StringBuilder sql = new StringBuilder();
sql.Append("SELECT ac_Products.Name,ac_Products.Sku,ac_ProductVariants.VariantName,ac_ProductVariants.Sku FROM ac_Products _
     LEFT JOIN ac_ProductVariants ON ac_Products.ProductId=ac_ProductVariants.ProductId");
System.Data.Common.DbCommand selectCommand = database.GetSqlStringCommand(sql.ToString());
database.AddInParameter(selectCommand, "@storeId", System.Data.DbType.Int32, Token.Instance.StoreId);
System.Data.IDataReader dbResults = database.ExecuteReader(selectCommand);
while (dbResults.Read())
{
       ... do stuff here
}
dbResults.Close();
dbResults.Dispose();

Post Reply