Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi, my frustration levels are so high at the moment I could Yell, so please ignore any errors.

We have a legacy Access system that has forms and database initialization. The Access system is live and we are trying to port it across form by web form to C#. net.

We have not been able to migrate to SQL due to the large number of errors that occur during the MS migration.

The one great thing about Access is how easy it is to build forms, doing things in C# I have found illogical and frustrating.

At the moment this is what I am stuck on. We have a table that is very badly normallized. It is a table of orders to be delivered. If a client has a more than one order for a day the orderdate, preparation (or roast) date, required date and other fields are duplicated.

So I am keen to create a master detail form. I have created an object with header and details:

C#
<blockquote class="FQ"><div class="FQA">Quote:</div>namespace TrackerDotNet.classes
{
  /// <summary>
  /// based on the order Query the classes stores the data
  /// SQL: SELECT OrdersTbl.CustomerID, CustomersTbl.CompanyName,  OrdersTbl.OrderDate, " +
  //           " OrdersTbl.RoastDate, OrdersTbl.RequiredByDate, OrdersTbl.Confirmed, OrdersTbl.Notes, " + 
  //           "PersonsTbl.Abreviation
  /// </summary>
  public class OrderHeaderDetails
  {

    //  CustomersTbl.CompanyName, OrdersTbl.CustomerId, OrdersTbl.OrderDate, OrdersTbl.RoastDate, 
    //  OrdersTbl.RequiredByDate, PersonsTbl.Abreviation, OrdersTbl.Confirmed, OrdersTbl.Done, OrdersTblNotes
    public OrderHeaderDetails()
    {
      _otCustomerID = 0;
      _ctCompanyName = _ptAbreviation = _otNotes = "";
      _otOrderDate = _otRoastDate = _otRequiredByDate  = DateTime.Now;
      _otConfirmed = true;
      _otDone = false;
    }

    private long _otCustomerID;
    private string _ctCompanyName;
    private DateTime _otOrderDate;
    private DateTime _otRoastDate;
    private DateTime _otRequiredByDate;
    private bool _otConfirmed;
    private bool _otDone;
    private string _otNotes;
    private string _ptAbreviation;

    public long CustomerID
    {
       get { return _otCustomerID; }
       set { _otCustomerID = value; }
    }
    public string CompanyName
    {
       get { return _ctCompanyName; }
       set { _ctCompanyName = value; }
    }
    public DateTime OrderDate
    {
       get { return _otOrderDate; }
       set { _otOrderDate = value; }
    }
    public DateTime RoastDate
    {
       get { return _otRoastDate; }
       set { _otRoastDate = value; }
    }
    public DateTime RequiredByDate
    {
       get { return _otRequiredByDate; }
       set { _otRequiredByDate = value; }
    }
    public bool Confirmed
    {
      get { return _otConfirmed; }
      set { _otConfirmed = value; }
    }
    public bool Done
    {
      get { return _otDone; }
      set { _otDone = value; }
    }
    public string Notes
    {
       get { return _otNotes; }
       set { _otNotes = value; }
    }
    public string Abreviation
    {
       get { return _ptAbreviation; }
       set { _ptAbreviation = value; }
    }
  }

  public class OrderLinesDetails
  {
    // ItemTypeID, QuantityOrdered, PrepTypeID 
    public OrderLinesDetails()
    {
      _otItemTypeID = _otPrepTypeID = 0;
      _otQuantityOrdered = 0.00;
    }

    private long _otItemTypeID, _otPrepTypeID;
    private double _otQuantityOrdered;

    public long otItemTypeID { get { return _otItemTypeID; } set { _otItemTypeID = value; } }
    public long otPrepTypeID { get { return _otPrepTypeID; } set { _otPrepTypeID = value; } }
    public double otQuantityOrdered { get { return _otQuantityOrdered; } set { _otQuantityOrdered = value; } }
  }
  
}
</blockquote>


I have then also created an object which I was hoping to link as an object datasource to the detailview (header / master) and gridview (lines / details)

C#
<blockquote class="FQ"><div class="FQA">Quote:</div>namespace TrackerDotNet.App_Code
{
  public class OrdersData
  {

    const string CONST_CONSTRING = "Tracker08ConnectionString";
    const string CONST_ORDERSHEADER_SELECT = "SELECT CustomersTbl.CompanyName, OrdersTbl.CustomerId As CustomerId, OrdersTbl.OrderDate, OrdersTbl.RoastDate, " +
                                             " OrdersTbl.RequiredByDate, PersonsTbl.Abreviation, OrdersTbl.Confirmed, OrdersTbl.Done, OrdersTbl.Notes " +
                                             " FROM ((OrdersTbl LEFT OUTER JOIN PersonsTbl ON OrdersTbl.ToBeDeliveredBy = PersonsTbl.PersonID)" +
                                             " LEFT OUTER JOIN CustomersTbl ON OrdersTbl.CustomerId = CustomersTbl.CustomerID)" +
                                             " WHERE ([CustomerId] = ?) AND ([RoastDate] = ?)";
    const string CONST_ORDERSLINES_SELECT = "SELECT ItemTypeID, QuantityOrdered, PrepTypeID FROM OrdersTbl WHERE ([CustomerId] = ?) AND ([RoastDate] = ?)";

    // connection Strinbg
    private string _connectionString;

    public OrdersData()
    {
      Initialize();
    }
    public void Initialize()
    {
      // Initialize data source. Use "Tracker08" connection string from configuration.

      if (ConfigurationManager.ConnectionStrings[CONST_CONSTRING] == null ||
          ConfigurationManager.ConnectionStrings[CONST_CONSTRING].ConnectionString.Trim() == "")
      {
        throw new Exception("A connection string named " + CONST_CONSTRING + " with a valid connection string " +
                            "must exist in the <connectionStrings> configuration section for the application.");
      }
      _connectionString =
        ConfigurationManager.ConnectionStrings[CONST_CONSTRING].ConnectionString;
    }

    public List<OrderHeaderDetails> LoadOrderHeader(Int32 pCustomerId, DateTime pPrepDate)
    {
      List<OrderHeaderDetails> ohDetails = new List<OrderHeaderDetails>;

      string _sqlCmd = CONST_ORDERSHEADER_SELECT;
      OleDbConnection _conn = new OleDbConnection(_connectionString);

      OleDbCommand _cmd = new OleDbCommand(_sqlCmd, _conn);
      _cmd.Parameters.Add(new OleDbParameter { Value = pCustomerDone } );
      _cmd.Parameters.Add(new OleDbParameter { Value = pPrepDate } );

      _conn.Open();
      objReader = objCmd.ExecuteReader();
      
      while (objReader.Read()) 
      {
        OrderHeaderDetails ohDetail = new OrderHeaderDetails();
 
        ohDetail.CustomerID = (Int32)objReader["CustomerID"];
        ohDetail.CompanyName = (string)objReader["CompanyName"];
        ohDetail.Abreviation = (string)objReader["Abreviation "];
        ohDetail.Notes = (string)objReader["Notes"];
        ohDetail.OrderDate = (string)objReader["OrderDate"];
        ohDetail.RoastDate = (string)objReader["RoastDate"];
        ohDetail.RequiredByDate = (string)objReader["RequiredByDate"];
        ohDetail.Confirmed = (bool)objReader["Confirmed"];
        ohDetail.Done = (bool)objReader["Done"];
        
        ohDetails.Add(ohDetail);
      }
      objReader.Close();  
      objConn.Close();

      return ohDetails;
    }


  }
}</blockquote>


I had previously used
C#
da.Fill(ds, "Orders");
but this seems to only return the data from the OrdersTbl not the customers table.

All I want to do is be able to create my own SELECT, UPDATE, INSERT and DELETE or CRED commands, and attach them to the grid / detail view. Is this really such a mission!


I have used many articles on CodeProject and ASP.NET to get this far and now I too frustrated. Is there not an easier way to do this?

I had tried to attach the class to the Object data source as below, but this is also problematic, since placing the class in the app_code folder seems to make to compiling not work and the auto fillful programming too.
ASP.NET
<asp:ObjectDataSource ID="odsOrderHeader" runat="server" TypeName="TrackerDotNet.App_Code.OrdersData" 
    EnablePaging="true" SelectMethod="LoadOrderHeader" >
        <SelectParameters>
          <asp:QueryStringParameter DefaultValue="1" Name="CustomerId" QueryStringField="CustomerID" Type="Int32" />
          <asp:QueryStringParameter Name="RoastDate" QueryStringField="PrepDate" Type="DateTime" />
        </SelectParameters>

      </asp:ObjectDataSource>
Posted
Updated 25-Feb-13 9:18am
v2
Comments
chaau 25-Feb-13 19:04pm    
usually this works fine. I used Access very often. It has some limitations. Have you tried to run this query in access Query Wizard (when you switch to SQL Design View)? Does it show any errors?
Warren Machanik 26-Feb-13 9:54am    
Thanks for the reply.

Query works fine, I normally "design" them in the SQL design view, that is a great feature wish it could create the data object.

I am getting errors attaching the object data source. Also in the class the CTRL+Space does not work properly so I have to check each command manually.

1 solution

After much renaming and recreating this is what I can tell those that have the same frustrations.

1. Firstly the public class that you are accessing for both the data class (in my case
public class OrderHeaderDetails
must be in the App_code folder, I have no idea why.
2. Also in the same folder must be the class that the object data source binds too, I am not sure why either, but when I put it in another folder I got name space errors
3. code in the app_code folder does not seem to have normal features if you compile / build it does not pick up compile / build errors like the rest of the code, instead only at run time do you get run time errors that you then need to fix (gee this reminds me of a language that I hate starts with "p" and ends in "hp"). I am not sure if there is a way around this.
4. Code auto complete (the thing that lets you easily access library routines and variable names) does not work in classes created under the strange an unexplained bermund triangle ish folder app_code

Once you follow these guide lines, and have the patience to fix error after error that may be cause by something as stupid as a misspelt function call to
OldDbDataReader 
for example. This code does work.

the best references for this type of coding I have found here: http://msdn.microsoft.com/en-us/library/ms178538(v=vs.100).aspx[^]

And

http://www.manuelabadia.com/blog/PermaLink,guid,c72852ae-1fdd-4934-a715-f565ceaf21cc.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900