Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# shopping basket, trying to remove an item...

Options
1356789

Comments

  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Darragh29 wrote: »
    But how do I "call it"???

    LoadCart();


  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    John_Mc wrote: »
    Put the code that loads the cart into a function and call it from Page_Load and whenever the content of the cart is modified.

    private void LoadCart()
    {

    }

    Is it possible to do something like this:

    PageLoad()

    {
    LoadCart();
    DeleteItem();

    Panel1.Controls.Add(LoadCart(Product_Label));
    Panel1.Controls.Add(DeleteItem(Delete_LinkBtn));
    }


    If I wanted to bring together two separate controls from two different functions into my panel in the Page_Load function??? Assuming that I had one function called LoadCart to add items to my cart and another one called DeleteItem to delete an item??? I've tried it and am getting an error saying:

    The name 'LoadCart_Product_Label' does not exist in the current context


    What I mean is if I call up a function, are the controls that are within that function I'm calling up, not brought in within the scope of the function I'm calling this function from???


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Darragh29 wrote: »
    Is it possible to do something like this:

    PageLoad()

    {
    LoadCart();
    DeleteItem();

    Panel1.Controls.Add(LoadCart(Product_Label));
    Panel1.Controls.Add(DeleteItem(Delete_LinkBtn));
    }


    If I wanted to bring together two separate controls from two different functions into my panel in the Page_Load function??? Assuming that I had one function called LoadCart to add items to my cart and another one called DeleteItem to delete an item??? I've tried it and am getting an error saying:

    The name 'LoadCart_Product_Label' does not exist in the current context


    What I mean is if I call up a function, are the controls that are within that function I'm calling up, not brought in within the scope of the function I'm calling this function from???

    I assume LoadCart_Product_Label is a control created and used within LoadCart() or LoadDelete()?

    If that's the case then only LoadCart or LoadDelete will know what it is and only it can add it to the container.

    There's no reason why you should add the controls created by your functions outside of those functions.


  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    Well I've gone full circle with it now, I've managed to get the Delete_Btn Linkbutton working with the event handler, but now I have too many iterations of my LinkButton! ARGH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Darragh29 wrote: »
    Well I've gone full circle with it now, I've managed to get the Delete_Btn Linkbutton working with the event handler, but now I have too many iterations of my LinkButton! ARGH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    What do you mean? Is it being called twice or are there duplicate items in the cart?

    If it's the latter then clear the container of any controls before starting to add them.

    If its the former use the IsPostback property prevent calling the LoadCart method in Page load.

    E.g
    if(!IsPostback)
    {
    LoadCart();
    }
    


  • Advertisement
  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    John_Mc wrote: »
    What do you mean? Is it being called twice or are there duplicate items in the cart?

    If it's the latter then clear the container of any controls before starting to add them.

    If its the former use the IsPostback property prevent calling the LoadCart method in Page load.

    E.g
    if(!IsPostback)
    {
    LoadCart();
    }
    

    It's hard to explain what the problem is... I've tried clearing the container of controls but no matter what way I try to do it, I can't seem to get the solution I want...

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Web.Services;
    using System.Collections.Specialized;
    using AjaxControlToolkit;
    using System.Data.SqlClient;
    using System.Data.OleDb;
    using System.Web.SessionState;
    using System.Text;

    public partial class _Default : System.Web.UI.Page
    {
    private bool CreateControls
    {
    get
    {
    return ViewState["CreateControls"] != null ? (bool)ViewState["CreateControls"] : false;
    }
    set
    {
    ViewState["CreateControls"] = value;
    }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
    if (this.Session["dummy"] == null) { this.Session["dummy"] = 1; } lblSessionID.Text = this.Session.SessionID;

    if (CreateControls)
    {
    CreateTheControls();
    }
    }


    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
    // Get selected values
    string make = DropDownList1.SelectedItem.Text;
    string model = DropDownList2.SelectedItem.Text;
    string color = DropDownList3.SelectedItem.Text;

    // Output result string based on which values are specified
    if (string.IsNullOrEmpty(make))
    {
    Label1.Text = "Please Select an Engine Type.";
    }
    else if (string.IsNullOrEmpty(model))
    {
    Label1.Text = "Please select a model.";
    }
    else if (string.IsNullOrEmpty(color))
    {
    Label1.Text = "Please Select a Make.";
    }
    else
    {
    string dropdowna = DropDownList1.SelectedValue;
    string dropdownb = DropDownList2.SelectedValue;
    string dropdownc = DropDownList3.SelectedValue;


    DateTime myDateTime1 = new DateTime();
    myDateTime1 = new DateTime();
    myDateTime1 = DateTime.Now;

    string selectSQL;

    //Connection for SELECT FROM "Vehicle data"
    SqlConnection connz = new SqlConnection("Data Source....");
    connz.Open();

    selectSQL = "SELECT * FROM Vehicle_Data WHERE Vehicle_Make = '" + DropDownList1.SelectedItem.Value + "' AND Vehicle_Model = '" + DropDownList2.SelectedItem.Value + "' AND Engine_Type = '" + DropDownList3.SelectedItem.Value + "'";

    SqlCommand cmdy = new SqlCommand(selectSQL, connz);
    cmdy.CommandType = CommandType.Text;

    SqlDataReader myDataReader;
    myDataReader = cmdy.ExecuteReader();

    Label1.Text = string.Format("Vehicle Information: {0} {1}.", make, model);
    Label7.Text = string.Format("Engine Type: {0}.", color);

    while (myDataReader.Read())

    {
    Label2.Text = string.Format("Your Purchase ID is: {0} ", myDataReader["Stock_Code"]);
    Label12.Text = string.Format("Retail Price: €");
    Label14.Text = string.Format("Product Info: ");
    Label3.Text = string.Format("{0:F2}", myDataReader["Retail_Price"]);
    Label4.Text = string.Format("{0} ", myDataReader["Product_Description"]);
    Label5.Text = string.Format("{0} ", myDataReader["Component_Manufacturer"]);
    Label15.Text = string.Format("Manufacturer: ");
    Label6.Text = string.Format("Timestamp: {0} ", myDateTime1);

    }

    myDataReader.Close();
    connz.Close();
    }
    }

    [WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static CascadingDropDownNameValue[] GetDropDownContentsPageMethod(string knownCategoryValues, string category)
    {
    return new CarsService().GetDropDownContents(knownCategoryValues, category);
    }

    public void Button1_Click(object sender, EventArgs e)
    {
    SqlConnection conn = new SqlConnection("Data Source....");
    conn.Open();

    if (this.Session["dummy"] == null) { this.Session["dummy"] = 1; } lblSessionID.Text = this.Session.SessionID;


    string TransDateTime = DateTime.UtcNow.ToString("r");

    string Delivery = string.Format("7.50");
    string dropdown1 = DropDownList1.SelectedValue;
    string dropdown2 = DropDownList2.SelectedValue;
    string dropdown3 = DropDownList3.SelectedValue;
    string Manufacturer = Label5.Text;
    string Description = Label4.Text;
    string Retail = Label3.Text;
    string SessionID = lblSessionID.Text;


    string sql = "insert into used_equipment (eq_type, eq_make, eq_model, SessionID, Retail_Price, Component_Manufacturer, Product_Description, Date_Time, Delivery_Cost)\n";
    sql += "values(";

    sql += "'" + dropdown1 + "',";
    sql += "'" + dropdown2 + "',";
    sql += "'" + dropdown3 + "',";
    sql += "'" + SessionID + "',";
    sql += "'" + Retail + "',";
    sql += "'" + Manufacturer + "',";
    sql += "'" + Description + "',";
    sql += "'" + TransDateTime + "',";
    sql += "'" + Delivery + "')";

    SqlCommand cmd = new SqlCommand(sql, conn);
    cmd.CommandType = CommandType.Text;
    cmd.ExecuteNonQuery();
    conn.Close();


    ////////////////////////////////////////////////////////////////

    string selectSQLX;
    string selectSUM;
    string SumDelivery;

    SqlConnection connT = new SqlConnection("Data Source....");
    connT.Open();

    SqlConnection connP = new SqlConnection("Data Source....");
    connP.Open();

    SqlConnection connX = new SqlConnection("Data Source....");
    connX.Open();

    selectSQLX = "SELECT * FROM used_equipment WHERE SessionID = '" + this.Session.SessionID + "'";
    selectSUM = "SELECT SUM(Retail_Price) FROM used_equipment WHERE SessionID = '" + this.Session.SessionID + "'";
    SumDelivery = "SELECT SUM(Delivery_Cost) FROM used_equipment WHERE SessionID = '" + this.Session.SessionID + "'";

    SqlCommand cmdy = new SqlCommand(selectSQLX, connT);
    cmdy.CommandType = CommandType.Text;

    SqlCommand cmdu = new SqlCommand(selectSUM, connP);

    SqlCommand cmdL = new SqlCommand(SumDelivery, connX);


    SqlDataReader DataReader;
    DataReader = cmdy.ExecuteReader();

    Panel1.Controls.Clear();
    if (DataReader.HasRows)
    {
    //Label13.Text = "";

    while (DataReader.Read())
    {

    object obj1 = new object();
    obj1 = string.Format(DataReader["eq_type"].ToString());

    object obj2 = new object();
    obj2 = string.Format(DataReader["eq_make"].ToString());

    object obj3 = new object();
    obj3 = string.Format(DataReader["eq_model"].ToString());

    object obj4 = new object();
    obj4 = string.Format(DataReader["Component_Manufacturer"].ToString());

    object obj5 = new object();
    obj5 = string.Format(DataReader["Product_Description"].ToString());

    object obj6 = new object();
    obj6 = string.Format(DataReader["Retail_Price"].ToString());

    object obj7 = new object();
    obj7 = string.Format(DataReader["Project_id"].ToString());

    string newstring = string.Concat(obj1, " " + obj2, " " + obj3, "<br>" + obj4, " " + obj5, "<br>Retail Price: € " + obj6, "<BR>" + obj7, "<BR>");

    Label Product_Label = new Label();
    Product_Label.ID = string.Format(DataReader["Project_id"].ToString());
    Product_Label.Text = newstring.ToString();


    Panel1.Controls.Add(Product_Label);

    CreateControls = true;
    CreateTheControls();


    }



    decimal Cart = Convert.ToDecimal(cmdu.ExecuteScalar());
    decimal Carry = Convert.ToDecimal(Delivery);
    decimal Cart_Total = Cart + Carry;

    Label Shopping_Cart = new Label();
    Shopping_Cart.ID = "Cart";
    Shopping_Cart.Text = string.Format("Order Total: € {0} ", (Cart));

    Label Shipping_Label = new Label();
    Shipping_Label.ID = "Shipping";
    Shipping_Label.Text = string.Format("<BR>Shipping: € {0} ", (Carry));

    Label Order_Total = new Label();
    Order_Total.ID = "Order_Tot";
    Order_Total.Text = string.Format("<BR>Invoice Total: € {0} ", (Cart_Total));

    Panel1.Controls.Add(Shopping_Cart);
    Panel1.Controls.Add(Shipping_Label);
    Panel1.Controls.Add(Order_Total);

    }

    DataReader.Close();
    connT.Close();
    connP.Close();
    }


    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void Button2_Click(object sender, EventArgs e)
    {

    }

    protected void Delete_Btn_Click(object sender, EventArgs e)
    {
    Label Delete_Text = new Label();
    Delete_Text.ID = string.Format("12");
    Delete_Text.Text = string.Format("Item has been deleted!");
    Panel1.Controls.Clear();
    Panel1.Controls.Add(Delete_Text);

    }

    public void CreateTheControls()
    {

    SqlConnection connUS = new SqlConnection("Data Source....");
    connUS.Open();

    string selectSQLXUS;

    selectSQLXUS = "SELECT * FROM used_equipment WHERE SessionID = '" + this.Session.SessionID + "'";

    SqlCommand cmdUS = new SqlCommand(selectSQLXUS, connUS);
    cmdUS.CommandType = CommandType.Text;
    SqlDataReader MyDataReader;
    MyDataReader = cmdUS.ExecuteReader();

    if (MyDataReader.HasRows)
    {
    ////////////////////////////////////////////////////////////

    while (MyDataReader.Read())
    {

    LinkButton Delete_Btn = new LinkButton();
    Delete_Btn.ID = string.Format(MyDataReader["Project_id"].ToString());
    Delete_Btn.Text = "Remove Item<BR><BR>";
    Delete_Btn.CommandName = "DeleteMe";
    //Delete_Btn.PostBackUrl = "~/MainTestX.aspx";
    Delete_Btn.OnClientClick = "";
    Delete_Btn.CommandArgument = string.Format("13");
    Delete_Btn.Click += new EventHandler(Delete_Btn_Click);
    Panel1.Controls.Add(Delete_Btn);

    }
    }

    }

    }

    The code in bold seems to be the problem. If I add in a clear container where I've inserted the comment line in bold, I end up clearing out the whole container, including the grey text that is my Product_Label string.

    You can see the problem more clearly in the attached image... The linkButton ID's for both buttons are $479 and $480 when I hover the mouse over them...

    I had this problem before with a label and I resolved it by appending two pieces of into into the one label instead of using two labels and it seems to be the same thing going on here...

    I'm wondering is there any way I could do something like this that I think would resolve the issue...

    Panel1.Controls.Add(Product_Label);
    Panel1.Controls.Add(CreateTheControls);
    CreateControls = true;
    //CreateTheControls();



    I'm getting an error when I do that as I can't just use a function like that with a control but if I could do something like this, I think I'd basically be appending the container as opposed to what I'm doing at the moment which is a bit messy...


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    I think you've done things a bit strangely here and it's adding to your confusion. You've put your logic that populates the item description into your button event handlers, and the code that adds the delete button in the page load.

    The delete button is being added twice because a page loads twice when you click a button. The first is to recreate the page, and the second is to process the button click event. You handle this by checking against the IsPostback property to see if you should add the controls.

    If it isnt a postback, then you should add the controls. If it is, you don't.

    The quickest way of saying this is:
    if(!IsPostback)
    {
    if (CreateControls)
    {
    CreateTheControls();
    }
    }
    

    This will probably solve the Delete button being added twice, but I'd recommend you handle things more logically. The two are linked to each other, if there's an item then there's a delete button.

    If you had a function called PopulateCart, which adds the text & description, and then calls the function to add the delete button it would make a lot more sense. You could also pass the information needed to create the delete button to the function, and this would safe a trip to the server & DB.

    Then you could call the PopulateCart function within you IsPostback check given above in the page Load function.


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    I am sorely tempted to write the correct code for you so that you can see how its done...

    A silly question now is why is all this info not in a datagrid so that you can do CRUD ops on it?


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Ginger wrote: »
    I am sorely tempted to write the correct code for you so that you can see how its done...

    A silly question now is why is all this info not in a datagrid so that you can do CRUD ops on it?

    Yeah, or a Repeater as you suggested earlier. Would make things a lot easier


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    I will see what I can do over the weekend for you.. being honest, its not a bad exercise for me

    Tho I have done the whole adding custom controls at runtime, this is not something that requires it...

    All you need is a list of items and when you want to delete it, you need to just know the ID to delete, ideal job for a repeater or the OOB datagrid which you can put the insert, update and delete options into by using the DataBind() method.

    http://www.dotnetjunkies.ddj.com/Article/24D5D73F-F941-4A70-B064-3195B8C039E7.dcik for example shows the whole thing like you might want to do

    You can put all the shopping cart in as a generic list in session (or db) and then retrieve so that they can edit amounts, delete for example

    Possibly a bit late in the day to be suggesting it tho


  • Advertisement
  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    My approach with C# up until this experience has been woeful!!! I've never used functions, wherever I could, I just horsed everything into the Page_Load until I had to use a Button_Click event handler, and how I got through two years of C in 3rd level I'll never know!

    I'm going to try approaching this problem more logically over the weekend and using one function to populate the cart and another one to delete items from the cart. I can't thank the two of you enough for your help with this, I'll have to set up a little drinking fund for a night on the tiles for yiz! ;)


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Darragh29 wrote: »
    My approach with C# up until this experience has been woeful!!! I've never used functions, wherever I could, I just horsed everything into the Page_Load until I had to use a Button_Click event handler, and how I got through two years of C in 3rd level I'll never know!

    I'm going to try approaching this problem more logically over the weekend and using one function to populate the cart and another one to delete items from the cart. I can't thank the two of you enough for your help with this, I'll have to set up a little drinking fund for a night on the tiles for yiz! ;)

    Perhaps you should try using an ASP.net control for this? It might show you how handy the out of the box controls are... think you probably need to see the benefits of this framework before you lose hope!

    As suggested above, try using a Repeater control and bind it to the Datatable or DataSource returned by your SQL.
    <asp:Repeater id="rptCart" runat="server">
    <ItemTemplate>
    <asp:Label Text='<%#DataBinder.Eval(Container.DataItem, "Item_Text")%>' runat="server" CssClass="AStyle"/>
    <br/>
    <asp:Button Text="Delete" OnClick="DeleteFunction" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "ID")%>'/>
    </ItemTemplate>
    </asp:Repeater>
    

    There's no shortage of documentation on how to use this control so give it a shot. Obviously you do the binding in it's own function and call that within Page_Load and check against Postback like I pointed out earlier.

    Don't worry about the work you've already put into this, what you've learned wont go to waste!

    You could add a button


  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    Right, I've just copied a back up for my old solution and wiped the slate clean! Is the basic idea here to use a datalist???


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Darragh29 wrote: »
    Right, I've just copied a back up for my old solution and wiped the slate clean! Is the basic idea here to use a datalist???

    I usually work with the DataTable datatype. You'll need in import System.Data to use it though.

    Once you populate your datatable with the contents returned by your SQL, you can bind it to the repeater using:
    rptCart.DataSource=dt;
    rptCart.Databind();
    

    dt is your datatable.


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Right he is what you could do

    You can create a new project for example called common which contains your classes describing your objects

    for example

    A car class

    public class Car
    {
    public int Id {get;set}
    public string Make {get;set;}
    public string Model {get;set;}
    public string Price {get;set;}
    }

    You can then decide if you want a data layer, business layer or just hop everything together in your ASP.NET code (normally I do n-tier I just find it tidy)

    If you use a data layer you can for example create a function called GetAllCars(string Make)

    like so
    public static List<Car> GetAllCars(string make)
    {
    List<Car> cars = new List<Car>();

    //Get a dataset of all cars for a particular make

    foreach(Datarow row in DataSet.Tables[0].Rows)
    {
    List.Add(new Car{
    Make = row["Make"].ToString(),
    Model = row["Model"].ToString()
    //and so on

    });

    return cars;
    }


    This will create a List of cars you can show for example

    Now using John_Mc's repeater

    YourRepeater.DataSource = GetAllCars("Mazda");
    YourRepeater.DataBind();

    Now in your repeater you would have

    <%# ((Car)Container.DataItem).Make%> would show the make

    You could arrange said info in a table and it would show the cars details and you can also add other controls to do deletes updates etc.

    Now if you use a datagrid, its the exact same thing

    DataGrid.DataSource = GetAllCars("Mazda");
    DataGrid.DataBind();

    Now with the DataGrid you can add the options for updates and deletes using the commands, enableupdates, enabledeletes in the designer.

    You then just need to add the event handlers that will do that and how to do that is posted a couple of posts ago..

    HTH


  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    I've did a bit of Googling and I found this which looks like it sets me up with a DataTable and it's compiling for me no bother...

    private DataTable CreateDataTable()
    {
    DataTable myDataTable = new DataTable();

    DataColumn myDataColumn;

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "id";
    myDataTable.Columns.Add(myDataColumn);

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "username";
    myDataTable.Columns.Add(myDataColumn);

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "firstname";
    myDataTable.Columns.Add(myDataColumn);

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "lastname";
    myDataTable.Columns.Add(myDataColumn);

    return myDataTable;
    }

    So I just adapt the columns to match up with my SQLDB? Then I just have to populate my DataTable, do I use a datareader to do this???


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    I have a sample project that does something similar and I will upload it here for you.

    Its a pure skeleton but shows a repeater being databound for example and also shows it with Common objects, DataLayer, Business and a handy little trick of filling objects if you have named the properties the same in the database :)
    (solution removed)


  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    Ginger wrote: »
    I have a sample project that does something similar and I will upload it here for you.

    Its a pure skeleton but shows a repeater being databound for example and also shows it with Common objects, DataLayer, Business and a handy little trick of filling objects if you have named the properties the same in the database :)

    Pm for password

    Jasus that looks seriously complicated!


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Darragh29 wrote: »
    I've did a bit of Googling and I found this which looks like it sets me up with a DataTable and it's compiling for me no bother...

    private DataTable CreateDataTable()
    {
    DataTable myDataTable = new DataTable();

    DataColumn myDataColumn;

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "id";
    myDataTable.Columns.Add(myDataColumn);

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "username";
    myDataTable.Columns.Add(myDataColumn);

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "firstname";
    myDataTable.Columns.Add(myDataColumn);

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "lastname";
    myDataTable.Columns.Add(myDataColumn);

    return myDataTable;
    }

    So I just adapt the columns to match up with my SQLDB? Then I just have to populate my DataTable, do I use a datareader to do this???

    Yeah exactly, within your While loop that iterates through the DBReader, you'll need to create a new row in the datatable. I forget exactly how to do this, but you're not just creating a datarow, you're creating a datarow for that datatable so that it has your column scheme.

    Google should give you a result on this. I had to do it recently myself.

    Once you have a new row, simply add your SQL column value to the corresponding column in myDataTable.

    I'd suggest using Gingers project as a template, it's definitely a good idea and best practice to seperate your logic into Data access, business rules and then you're logic in the code behind files. So instead of putting the code above in your code behind file, put it in a Dataclass and make it publicly accessible.


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Darragh29 wrote: »
    Jasus that looks seriously complicated!

    Take it one step at a time, but this is the structure of your web application that you should be aiming for!


  • Advertisement
  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    code to create a new row:

    myRow = myDataTable.NewRow();


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Ok

    to explain

    You have a project called common, which contains my class representation of the data eg Player

    This project is referenced by all the other projects so that any changes to my definition of a class is automatically recieved.

    So the way that this works is so

    ASP.NET references Business which references DataAccess

    The only way for my ASP.NET to get its information is via the Business layer, meaning I can change the whole way the database works and just change only 1 section.

    As well as that, it means that the whole database thing is abstracted away, which is a very good thing. To get a list of Players, I just say GetAllPlayers() and thats it. It works.. no checking for open connections, no messing with configs. I just tell it what I want :)

    Now the database helper class takes care of access the database, opening and closing connections and returning information to my functions in the dataaccess project. That in turn, turns them into objects such as Player or Lists and sends them back to the Business project which sends them to the ASP.NET app so that it can run happy.

    In the DataAccess project there is a class called Common, which has 2 functions. These fill my objects using Generics..

    What I do, is I pass in an object eg Player and tell it to find out what public properties it has and to take the data from the row in the dataset and match it to the property names.

    What makes this a very handy function is that if I name my Fields in the database the same as the properties in my class, i can just populate the info very quickly and just use 1 function without having to write a function for each object type..

    Its complex if you dont see how it all hangs together....

    But step through the code from the ASPX page it will show how it gets to the database and back and how you end up with lists of objects rather than datasets


  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    When I try to run that demo site, I'm getting this error...

    It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Right ok, you are probably running on IIS, I run mine on Cassini (the dev web server)

    Right first question running on IIS? You need to configure the virtual folder as an application

    If its not that

    Second question, are you running in a folder and and is there a web.config above that folder... IE is there 2 web.configs in the same path


  • Registered Users Posts: 2,931 ✭✭✭Ginger




  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    Ginger wrote: »
    Right ok, you are probably running on IIS, I run mine on Cassini (the dev web server)

    Right first question running on IIS? You need to configure the virtual folder as an application

    If its not that

    Second question, are you running in a folder and and is there a web.config above that folder... IE is there 2 web.configs in the same path

    I just created a new website and copied your files into it and that seems to have sorted out the problem there. Even just seeing this working is enough to make me want to go out for a few pints to celebrate!!!

    I have to pop out for an hour or two for a meeting but I'll be back later pulling that solution you gave me apart! Thanks again so much to the both of you... Beers on me in Dublin!


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Just realised I didnt supply a database if you are using SQL Server 2008 I can send you the files, otherwise I will create an SQL script for you with all the procs and logins etc

    N


  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    Ginger wrote: »
    Just realised I didnt supply a database if you are using SQL Server 2008 I can send you the files, otherwise I will create an SQL script for you with all the procs and logins etc

    N

    I'm using MS SQL Server 2005. I think I can knock that bit of it together though....


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    PM sent with script, just create your own user and database


  • Advertisement
  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    What I'm trying to do now is pull asunder the code I have that adds stuff to my cart (DB up until now), and get it to add items into this new datatable that you gave me...


Advertisement