ASP.NET Database Error

Mark01
Mark01 used Ask the Experts™
on
Background: I’m working with a C# module in DotNetNuke. I am currently:
a.) Adding a new DropDownList control

I have performed the following steps:
1. Studied the DNN Events module.
2. Created a new Categories table (Based on the identical table in the DNN Events module).
3. Created the following stored procedures:
DeleteCategory, CategoryList, GetCategory and CategorySave.
4. Converted VB to C# and copied the appropriate code into the EditMarkTest1.aspx.cs, VendorsController.cs, SqlDataProvider.cs, DataProvider.cs and VendorsInfo.cs classes.

Question: Why does the Category primary key not exist in the current context?

Error Message:
Error      1      The name ‘Category’ does not exist in the current context      C:\junk\dnn\1\DesktopModules\MarkTest1\EditMarkTest1.ascx.cs      86      95      C:\...\1\


Code Causing Error:
          List<VendorsInfo> categories = objGCShippingAdminController.GetCategory(PortalId, Category);

Screen Shots:
The attached screen shots show the table with the new DropDownList control and the database diagram for the Categories table.
Code:
Here is some code from various classes in the module.

EditMarkTest1.ascx.cs

protected void Page_Load(System.Object sender, System.EventArgs e)
        {
            //new combo box (Jan. 2010)

            cmbCategory.DataTextField = “CategoryName”;
            cmbCategory.DataValueField = “Category”;
            cmbCategory.DataBind();
            VendorsController objGCShippingAdminController = new VendorsController();

            List<VendorsInfo> categories = objGCShippingAdminController.GetCategory(PortalId, Category);
--------------------------------------------------------------------------------------------------------------------------------------
The error shown below occurs when the following line is executed:

           List<VendorsInfo> categories = objGCShippingAdminController.GetCategory(PortalId, Category);


Error Message:

Error	1	The name ‘Category’ does not exist in the current context	C:\junk\dnn\1\DesktopModules\MarkTest1\EditMarkTest1.ascx.cs	86	95	C:\...\1\

*******************************************************************************************************************
*******************************************************************************************************************

DataProvider.cs:

       //New Categories table (combo box) code. Names of the stored procedures.
        //
        public abstract void DeleteCategory(int PortalID, int Category);
        public abstract IDataReader GetCategory(int PortalId, int Category);
        public abstract void CategorySave(int PortalId, int Category, string CategoryName, string Color);
        public abstract void CategoryList(int PortalId, int Category);

       #region Abstract methods

        //These are the names of the stored procedures.
        public abstract void AddMarkTest1(int PortalId, string ShippingName, string BasePrice, string PricePerUnit, string Comments1);
        public abstract IDataReader GetMarkTest1(int PortalId);
        public abstract IDataReader GetMarkTest1s(int ModuleId);
        public abstract void UpdateMarkTest1(int ModuleId, string ShippingName, string BasePrice, string PricePerUnit, string Comments1);
        public abstract void DeleteMarkTest1(int ItemId);

    #endregion

*******************************************************************************************************************
*******************************************************************************************************************

SqlDataProvider.cs:

   #region Public Methods

        public override void AddMarkTest1(int PortalId, string ShippingName, string BasePrice, string PricePerUnit, string Comments1)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName(“AddMarkTest1”), PortalId, ShippingName, BasePrice, PricePerUnit, Comments1);
        }

        public override void DeleteMarkTest1(int ItemId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName(“DeleteMarkTest1”), ItemId);
        }

        public override IDataReader GetMarkTest1(int PortalId)
        {
             return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName(“GetMarkTest1”), PortalId);
        }

        public override IDataReader GetMarkTest1s(int ModuleId)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName(“GetMarkTest1s”), ModuleId);
        }

        public override void UpdateMarkTest1(int ItemId, string ShippingName, string BasePrice, string PricePerUnit, string Comments1)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName(“UpdateMarkTest1”), ItemId, ShippingName, BasePrice, PricePerUnit, Comments1);
        }

        //new for combo box
        //*
        public override void DeleteCategory(int PortalId, int Category)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName(“DeleteCategory”), PortalId, Category);
        }

        //*2nd try
        public override IDataReader GetCategory(int PortalId, int Category)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName(“GetCategory”), PortalId, Category);
        }


        public override void CategorySave(int PortalId, int Category, string CategoryName, string Color)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName(“CategorySave”), PortalId, Category);
        }

        public override void CategoryList(int PortalId, int Category)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName(“CategoryList”), PortalId, Category);
        }
        //
    #endregion

*******************************************************************************************************************
*******************************************************************************************************************

VendorsController.cs:

       //new
        public List<VendorsInfo> GetMarkTest1(int portalId)
        {
            return CBO.FillCollection<VendorsInfo>(DataProvider.Instance().GetMarkTest1(portalId));
        }


        //new for combo box
       public List<VendorsInfo> GetCategory (int PortalId, int Category)
        {
            return CBO.FillCollection<VendorsInfo>(DataProvider.Instance().GetCategory(PortalId, Category));
    }
        /// <summary>
        /// Updates the information for an existing shipping option
        /// </summary>
        /// <param name=”objGCShippingAdmin”>VendorsInfo object initialized w/ the new shipping option information</param>

        //new
        public void UpdateMarkTest1(VendorsInfo objGCShippingAdmin)
        {
            if ((objGCShippingAdmin != null) && (objGCShippingAdmin.ItemId >= 0) && (objGCShippingAdmin.ShippingName != null) && (objGCShippingAdmin.ShippingName.Length != 0))
            {
                DataProvider.Instance().UpdateMarkTest1(objGCShippingAdmin.ItemId, objGCShippingAdmin.ShippingName, objGCShippingAdmin.BasePrice, objGCShippingAdmin.PricePerUnit, objGCShippingAdmin.Comments1);
            }
        }
        
        /** orig    ***
        public void UpdateVendorsInfo(VendorsInfo objGCShippingAdmin)
        {
            if ((objGCShippingAdmin != null) && (objGCShippingAdmin.ItemId >= 0) && (objGCShippingAdmin.ShippingName != null) && (objGCShippingAdmin.ShippingName.Length != 0))
            {
                DataProvider.Instance().UpdateVendorsInfo(objGCShippingAdmin.ItemId, objGCShippingAdmin.ShippingName, objGCShippingAdmin.BasePrice, objGCShippingAdmin.PricePerUnit);
            }
        }
        */

        //NEW FOR Categories table (combo box
        public void DeleteCategory(int PortalId, int Category)
        {
           
            //Mark
            DataProvider.Instance().DeleteCategory(PortalId, Category);
            
            //Experts Exchange
            //DotNetNuke.Data.DataProvider.Instance().DeleteCategory(PortalId, Category);

        }
         public void CategorySave(int PortalId, int Category, string CategoryName, string Color)
        {
            DataProvider.Instance().CategorySave(PortalId, Category, CategoryName, Color);
        }
*******************************************************************************************************************
*******************************************************************************************************************

VendorsInfo.cs:

       #region Private Members

        private int _portalId;
        private int _itemId;
        private string _shippingName;
        //(1-27-10) New for combo box
        private int _category;



        //orig
//        private decimal _basePrice;

        //new for Vendors
        private string _basePrice;

                //orig
//        private decimal _pricePerUnit;

        //new for Vendors
        private string _pricePerUnit;

        //new for Vendors
        private string _comments1;

        //new for Categories (combo box)
        private string _categoryName;

        #endregion

        #region Constructors
        public VendorsInfo()
        {
        }
        #endregion

        #region Public Properties

        /// <summary>
        /// Gets and sets the Portal Id
        /// </summary>
        public int PortalId
        {
            get
            {
                return _portalId;
            }
            set
            {
                _portalId = value;
            }
        }

        /// <summary>
        /// Gets and sets the shipping option Item Id
        /// </summary>
        public int ItemId
        {
            get
            {
                return _itemId;
            }
            set
            {
                _itemId = value;
            }
        }

        /// <summary>
        /// Gets and sets the name of the shipping option
        /// </summary>
        public string ShippingName
        {
            get
            {
                return _shippingName;
            }
            set
            {
                _shippingName = value;
            }
        }
        /// <summary>
        /// Gets and sets the shipping option Category
        /// </summary>
        public int Category
        {
            get
            {
                return _category;
            }
            set
            {
                _category = value;
            }
        }
        /// <summary>
        /// Gets and sets the base price of the shipping option
        /// </summary>

        /** orig    ****
        public decimal BasePrice
        {
            get
            {
                return _basePrice;
            }
            set
            {
                _basePrice = value;
            }
        }

         ***    end orig    *****/

        //new for Vendors
        public string BasePrice
        {
            get
            {
                return _basePrice;
            }
            set
            {
                _basePrice = value;
            }
        }

        /// <summary>
        /// Gets and sets the price per shipping unit for the shipping option
        /// </summary>

        /** orig    ****
        public decimal PricePerUnit
        {
            get
            {
                return _pricePerUnit;
            }
            set
            {
                _pricePerUnit = value;
            }
        }
         ***    end orig    ***/

        //new for Vendors
        public string PricePerUnit
        {
            get
            {
                return _pricePerUnit;
            }
            set
            {
                _pricePerUnit = value;
            }
        }

        //new Comments field
        public string Comments1
        {
            get
            {
                return _comments1;
            }
            set
            {
                _comments1 = value;
            }
        }

        //new for categories combo box
        //*
        public string CategoryName
        {
            get
            {
                return _categoryName;
            }
            set
            {
                _categoryName = value;
            }
        }

*******************************************************************************************************************
*******************************************************************************************************************

The following code executes when the data in the table is edited:

EditMarkTest1.ascx.cs

private void BindShippingList()
        {
            VendorsController objGCShippingAdminController = new VendorsController();
           //orig
            //List<VendorsInfo> shippingRates = objGCShippingAdminController.GetVendorsInfo(PortalId);

            //mod
            List<VendorsInfo> shippingRates = objGCShippingAdminController.GetMarkTest1(PortalId);

            addNewShippingTypeButton.Visible = true;
            finishShippingListButton.Visible = true;

            //new Branches links
            addNewBranchesTypeButton.Visible = true;
            //branchesListPanel.Visible = true;
            finishBranchesListButton.Visible = true;

            shippingDataGrid.DataSource = shippingRates;
            shippingDataGrid.DataBind();

            //new Branches code
            branchesDataGrid.DataSource = shippingRates;
            branchesDataGrid.DataBind();
        }

*******************************************************************************************************************
*******************************************************************************************************************


The Panel and table (HTML)

<asp:Panel ID=”branchesListPanel” runat=”server” Visible=”False” meta:resourcekey=”branchesListPanelResource1” >
                        <asp:DropDownList ID=”DropDownList1” runat=”server” CssClass=”ErrorMessageLabel” Width=”40%”
                        AutoPostBack=”true”>
                        </asp:DropDownList>
                        </td>
                          <tr valign=”top”>
    <asp:DataGrid ID=”branchesDataGrid” runat=”server” AutoGenerateColumns=”False” CssClass=”DataGrid_Container” resourcekey=”branchesDataGridResource1”>
        <Columns>
            <asp:BoundColumn DataField=”ItemId” ReadOnly=”true”>
                <ItemStyle CssClass=”invisible” />
                <HeaderStyle CssClass=”invisible” />
            </asp:BoundColumn>
            <asp:BoundColumn DataField=”ShippingName” HeaderText=”Vendor” >
                <ItemStyle CssClass=”DataGrid_Item” />
                <HeaderStyle CssClass=”DataGrid_Header” />
            </asp:BoundColumn>
            <asp:BoundColumn DataField=”BasePrice” HeaderText=”Nox Base Shipping Cost” DataFormatString=”{0:C}” >
                <ItemStyle CssClass=”DataGrid_Item” />
                <HeaderStyle CssClass=”DataGrid_Header” />
            </asp:BoundColumn>
            <asp:BoundColumn DataField=”PricePerUnit” HeaderText=”Cost Per Shipping Unit” DataFormatString=”{0:C}” >
                <ItemStyle CssClass=”DataGrid_Item” />
                <HeaderStyle CssClass=”DataGrid_Header” />
            </asp:BoundColumn>
            
            <asp:BoundColumn DataField=”Comments1” HeaderText=”Comments” DataFormatString=”{0:C}” >
                <ItemStyle CssClass=”DataGrid_Item” />
                <HeaderStyle CssClass=”DataGrid_Header” />
            </asp:BoundColumn>
            
            <asp:EditCommandColumn EditText=”Edit” CancelText=”Cancel” HeaderText=”Edit” UpdateText=”Update” meta:resourcekey=”EditCommandColumnResource2”>
                <ItemStyle CssClass=”CommandButton” />
                <HeaderStyle CssClass=”DataGrid_Header” />
            </asp:EditCommandColumn>
            <asp:ButtonColumn Text=”Delete” CommandName=”Delete” HeaderText=”Delete” meta:resourcekey=”ButtonColumnResource2” >
                <ItemStyle CssClass=”CommandButton” />
                <HeaderStyle CssClass=”DataGrid_Header” />
            </asp:ButtonColumn>
        </Columns>
    </asp:DataGrid>

Open in new window

Combo-2.jpg
Db1.jpg
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Simple question...where are you actually setting the data to the dropdownlist?

CPG

Author

Commented:
Hi copyPasteGhost,

I believe you are referring to the DropDownList data source property. The following line is in the Page_Load event of EditMarkTest1.ascx.cs. It is currently rem'd out until I resolve the Category primary key error.

cmbCategory.DataSource = categories;

Mark
the current error is saying that the value is not in the dropdown list.

cmbCategory.DataSource = categories; should solve that.

What primary key error are you getting...

Sorry if I'm not understanding.... I'll try to help I promise.

CPG
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Author

Commented:
copyPasteGhost,

When the following line is not rem'd out, I still get the same error.

cmbCategory.DataSource = categories;


Here is the error message:
Error      1      The name ‘Category’ does not exist in the current context      C:\junk\dnn\1\DesktopModules\MarkTest1\EditMarkTest1.ascx.cs      86      95      C:\...\1\



The error is caused by the following:
          List<VendorsInfo> categories = objGCShippingAdminController.GetCategory(PortalId, Category);

Mark
what is populating: objGCShippingAdminController

Author

Commented:
copyPasteGhost,

The GetCategory Stored Procedure is populating it. Here it is:

ALTER PROCEDURE YourCompany_GetCategory
(
      @Category int,
      @PortalID int
)
AS
SELECT Category, PortalID, CategoryName
FROM YourCompany_Categories
WHERE Category = @Category AND PortalID = @PortalID

Mark
are you sure the stored proc is getting called before the query is executing?

why don't you try to step through the code to see if the objGCShippingAdminController is populated before you call the GetCategory method.
Top Expert 2013

Commented:
Looks like it is looking for a variable or property - Category in the context of which it is used.
You need to declare int Category somewhere and assign it appropriate value before passing it to that function.
Is that the entire code or the part of the code?
that's actually a good catch thanks sami. can't believe I missed that.

Author

Commented:
Hi guru_sami,

The code for the DropDownList control is not complete, but that is I all of the code I have written so far. I have translated VB into C# and used the Events DNN module as an example. Note that the Events module is written in VB.

I added the following to the page_Load event of EditMarkTest1.ascx.cs, but I still get the same error with category.
           VendorsInfo shippingInfo = new VendorsInfo();
            shippingInfo.Category = category;

Here’s the code from the properly functioning vendors editing table:
           VendorsInfo shippingInfo = new VendorsInfo();
            shippingInfo.PortalId = PortalId;
List<VendorsInfo> shippingRates = objGCShippingAdminController.GetMarkTest1(PortalId);

Author

Commented:
copyPasteGhost,

I’m having difficulty finding the right variable or property to observe while stepping through the code. I’m trying to observe the values of objGCShippingAdminController when the vendors data is edited. That code executes without any errors. I’ve rem’d out the new DropDownList code so I can step through the module.

Here are my attempts so far:

In EditMarkTest1.ascx.cs
Watch window:

Variable = objGCShippingAdminController      

Value = {YourCompany.Modules.MarkTest1.VendorsController}      YourCompany.Modules.MarkTest1.VendorsController

---------------------------
In VendorsController.cs

Added to watch: VendorsInfo

Results:
(a) YourCompany.Modules.MarkTest1.VendorsInfo      YourCompany.Modules.MarkTest1.VendorsInfo

(b) base - object
Top Expert 2013
Commented:
1: You can post the vb code file you are trying to convert
2: Let me explain what the error says.

Take sample code below:

protected void Page_Load(object sender, EventArgs e)
    {
        int x =5;
        int z = x+y;
    }
Now if you try to compile the code above you will get error:
--->      The name 'y' does not exist in the current context      ..blablabla

So say for your code:
           VendorsInfo shippingInfo = new VendorsInfo();
            shippingInfo.Category = category;

You declared and instantiated "shippingInfo" but if you haven't done that for "category" you will get the error you are seeing. Again its not necessary to define in the function but that could be defined outside of function as an property or a member of class.

Author

Commented:
guru_sami,

Thank you for explaining the theory to me; it was very helpful. At this point I don’t have any VB code to post that needs to be converted.

Is the best approach to step through the existing code for the vendors data? That code executes without any errors. That part of the module functions properly.

Here is the properly functioning code for the vendor editing:

          VendorsInfo shippingInfo = new VendorsInfo();
          List<VendorsInfo> shippingRates = objGCShippingAdminController.GetMarkTest1(PortalId);

I’m trying to find the line where PortalId is declared.

I’ve only found the following line in EditMarkTest1.ascx.cs, but it does not hit when I edit the vendor data:
shippingInfo.PortalId = PortalId;

Author

Commented:
guru_sami,

I’m trying to find the line of code where PortalID is declared and instantiated. The results listed below are what I found when searching the entire DotNetNuke solution.
(1) Class: VendorSettingsInfo.cs
(a) Line 25: private int _portalId;
(b) Line 49: public int PortalId

Here is the code surrounding line 49:

       #region Public Properties

        /// <summary>
        /// Gets and sets the Portal ID
        /// </summary>
        public int PortalId
        {
            get
            {
                return _portalId;
            }
            set
            {
                _portalId = value;
            }
        }

        /// <summary>
        /// Gets and sets the Merchant ID
        /// </summary>
        public string MerchantId
        {
            get
            {
                string merchantId = _merchantId;
                if (_merchantId != null)
                {
                    merchantId = _merchantId.Trim();
                }
                return merchantId;
            }
            set
            {
                _merchantId = value;
            }
        }

(2) Class: VendorsInfo.cs
Line 14: private int _portalId;
Line 53: public int PortalId

Here is the code surrounding line 53:

       #region Public Properties
        /// <summary>
        /// Gets and sets the Portal Id
        /// </summary>
        public int PortalId
        {
            get
            {
                return _portalId;
            }
            set
            {
                _portalId = value;
            }
        }

Further down in the same class beginning on line 94 is the new code for the Categories DropDownList.

       /// <summary>
        /// Gets and sets the shipping option Category
        /// </summary>
        public int Category
        {
            get
            {
                return _category;
            }
            set
            {
                _category = value;
            }
        }

Open in new window

Author

Commented:
Thank you, copyPasteGhost and guru_sami.

Author

Commented:
Your answers were very helpful. I submitted a new question based on the answers to this question.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial