Solved

Newbie needs help passing variables and clean-up.

Posted on 2003-11-14
3
235 Views
Last Modified: 2010-04-16
Newbie needs help passing variables from inside a class.  It's a web service and I can't get "Model" back from the class to update a table.  Any help is appreciated and I know this code is terrible so any clean-up suggestions are appreciated.  The purpose is to read the first table to see if this order has already been updated to the table.  If it has not, then it gets the header record and then the detail records from the "from" table.  It then updates the detail records in the "to" table from the array (in the class) and gathers the Model ordered from one of the detail records, saving it in sModel.   Then it's time to update the header record in the "to" table with the model, but since it's outside of the class, I can't retrieve it from the class, even though I can see it in debug in ordItems _items.
I'll give 250 points for passing the variable and 250 for clean-up suggestions (in code).

            [WebMethod]
            public string UpdtConfWG(string DNID)
            {

                  //  Check to see if already updated
                  SqlConnection conWG = dbAccess.getSqlConnection("WebOrder");
                  string sqlWG = "select * from db3l2p1 where Var2="+DNID;
                  bool WGHdr = dbAccess.getFirstRecord(conWG, sqlWG);
           

                  // if not found in WG database, continue
                  if (WGHdr)
                  {
                        // Need configurator table record (header)
                        SqlConnection con = dbAccess.getSqlConnection("Config");
                        string sqlHdr = "select * from configuration where CONF_ID = (" + DNID + ")";
                        object[] confHdr = dbAccess.readFirstRecord(con, sqlHdr);
                        string USERID = confHdr[1].ToString();
                        double BASEPRICE = Convert.ToDouble(confHdr[2]);
                        string CONF_DATE = confHdr[3].ToString();
                        string CONF_DESC = confHdr[4].ToString();
                        string CUSTOMER_NAME = confHdr[5].ToString();
                        string CONF_STATUS = confHdr[7].ToString();
                        if (CONF_STATUS == "3")
                        {
                              double CONF_TOTAL = Convert.ToDouble(confHdr[8]);
                              int MAPICS_ORDER = Convert.ToInt16(confHdr[9]);
                              string SHIPPING_INSTRUCTION = confHdr[12].ToString();
                              string TERM = confHdr[13].ToString();
                              string DEALER_PO_NUM = confHdr[14].ToString();                        

                              // configuration_tree table records (detail)
                              //SqlConnection conDtl = dbAccess.getSqlConnection("Config");
                              string sqlDtl = "select * from cf_get_conf_Info(" + DNID + ")";
                              string confTable2 = "Configuration";
                              DataTable dtConfDtl = dbAccess.getDataTable(con, confTable2, sqlDtl);            
                  

                              ArrayList ordItems = new ArrayList();
                    string sModel;
                              foreach (DataRow dr in dtConfDtl.Rows)
                              {
                                    try
                                    {  
                                          ordItems.Add(new ordItem(ordItems.Count, dr, DNID));
                                          //Here is where my problem is - I need the model from the class
                                          //  this won't compile, but I can see it in debug in ordItems _item
                                          //Error for next line: 'System.Collections.ArrayList' does not contain a definition for 'sModel'
                                          if (ordItems.sModel != null)
                            //Error for next line: An object reference is required for the nonstatic field, method, or property 'WebOrderUpdates.CSIWebOrderUpdts.ordItem.sModel"
                                        if (ordItem.sModel != null)
                                                this.Model = ordItem.sModel;
                                    }
                                    catch (System.UnauthorizedAccessException)
                                    {
                                    }
                              }

                              //WebOrderUpdates.ordItems oi = ordItems;
                              //string sModel = ((ordItems)[4].ToString());
                              //string sModel = "";//ordItems[4].sModel;
                              string sModelDesc = "";//ordItem[4];
                              sqlHdr = "INSERT INTO db3l2p1 (Division,Location,DNID,DNMacID,DealerNumber,DealerPO,Terms,ModelDesc,OrderDate,Status)"
                                    + " VALUES ('BlountFied','ZEB',"+DNID+",'"+Model+"','"+USERID+"','"+DEALER_PO_NUM+"','"+TERM+"','"+sModelDesc+"','"+CONF_DATE+"','10'" ;
                              SqlConnection conHdr = dbAccess.getSqlConnection("WebOrder");
                              SqlCommand oInsertCommand = new SqlCommand(sqlHdr);
                              oInsertCommand.Connection = conHdr;
                              oInsertCommand.ExecuteScalar();
                              conHdr.Close();

                              return "Update Completed";
                        }

                  return "Update Completed";

                  }

      return "Update Completed";

}      


            public class ordItem
            {
                  private int index;
                  private string Equipment_Name;
                  private string Equipment_Description;
                  private string Order_Code;
                  private string Mapics_Num;
                  private string Equipment_Code;
                  private int Quantity_Item;
                  private double Price;
                  private string Part_Name;
                  private string Part_Description;
                  private string WGPN;
                  private string WGPNT;
                  private double Part_Quantity;
                  public string sModel;
                  public string sModelDesc;
                  

                  public ordItem(int index, DataRow dr, string DNID)
                  {
                        this.index = index;
                        Equipment_Name = dr[0].ToString();
                        Equipment_Description = dr[1].ToString();
                        Order_Code = dr[2].ToString();
                        Mapics_Num = Convert.ToString(dr[3]);
                        Equipment_Code = dr[4].ToString();
                        Quantity_Item = Convert.ToInt16(dr[5]);
                        Price = Convert.ToDouble(dr[6]);
                        Part_Name = dr[7].ToString();
                        Part_Description = dr[8].ToString();
                        WGPN = dr[9].ToString();
                        WGPNT = dr[10].ToString();
                        Part_Quantity = Convert.ToDouble(dr[11]);
                        string sModel;
                        bool isStatic = false;
                        string sqlDtl;
                        
                        if (WGPNT == "HL")
                        {
                              sModel = Equipment_Name;
                              //Model = Equipment_Name;
                              sModelDesc = Equipment_Description;
                              sqlDtl = "INSERT INTO db3l2p2 (Var0,Var1,Var2,Var3,Var4,Var5,Var6,Var9)"
                                    + " VALUES ("+DNID+","+this.index+",'"+Order_Code+"','"+Equipment_Description+"',"+Quantity_Item+",'"+Equipment_Name+"','"+WGPN+"',"+Price+")" ;
                        }
                        else
                        {
                              sqlDtl = "INSERT INTO db3l2p2 (Var0,Var1,Var2,Var3,Var4,Var5,Var6,Var9)"
                                    + " VALUES ("+DNID+","+this.index+",'"+Order_Code+"','"+Equipment_Name+"',"+Quantity_Item+",'"+Equipment_Code+"','"+WGPN+"',"+Price+")" ;
                        }

                //public string dSQL { get {return sql;} }
                        SqlConnection conDtl = dbAccess.getSqlConnection("WebOrder");
                        SqlCommand oInsertCommand = new SqlCommand(sqlDtl);
                        oInsertCommand.Connection = conDtl;
                        oInsertCommand.ExecuteScalar();
                        conDtl.Close();
                        
               
                  }


            }
      }
}
0
Comment
Question by:dcass
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
ptmcomp earned 500 total points
ID: 9753941
The problem is that ArrayList has no type information about orditem. You have to cast the items back to orditem to access members of the items.

                         ArrayList ordItems = new ArrayList();
                         string sModel;
                         foreach (DataRow dr in dtConfDtl.Rows)
                         {
                              try
                              {  
                                   orditem myOrdItem;

                                   myOrdItem = new ordItem(ordItems.Count, dr, DNID);
                                   ordItems.Add(myOrdItem);
                                   if (myOrdItem.sModel != null)
                                        this.Model = myOrdItem.sModel;
                              }
                              catch (System.UnauthorizedAccessException)
                              {
                              }
                         }

Note: Please read about naming conventions in C# and use properties to access private members. Public member variables are seen as dirty programming. This is not necessary to make it work but it helps others to read the code.

Change this:

          public class ordItem
          {
               private int index;
               ...
               private double Part_Quantity;
               public string sModel;
               public string sModelDesc;

to this:

          public class OrdItem
          {
               private int index;
               ...
               private double part_Quantity;
               private string model;
               private string modelDesc;

               // use properties to access private stuff
               public string ModelDesc
               {
                   get
                   {
                       return modelDesc;
                   }
               }
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9753946
This line:
  if (ordItems.sModel != null)
cannot work since you cannot access a property of a single item on an array. How should the system know which of the items you want to access.

This would work:
  if (((orditem)ordItems[0]).sModel != null)
0
 

Author Comment

by:dcass
ID: 9765646
The first anwer worked just fine.
However, I would like to make put both update into a table and update with a Fill statement instead of the update I have here.
Wouldn't that be cleaner?  That would fulfill my other question for the points.
Thanks in advance.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Get Current Exchange Rate from XE 9 40
asp.net, radiobuttonlist, c# 3 66
How to set focus on a dynamic control 18 47
C# LINQ query question 11 29
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question