#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Database Search If No Result


    Hi there,

    I am working on a project.
    upon clicking a button the users data entry (order number) is used to search the database. I have the code to search the database below but I dont know what code to use to perform an error if the order number doesnt exist. Could anybody help. Im new to ASP and dont fully understand all the keywords available or the structures of if statements within it. Any help would be greatly appreciated.


    Code:
    Int64 ID = new Int64();
                    ID = Convert.ToInt64(txtID.Text);
    
     
         using (cmpkwoodEntities context = new
          cmpkwoodEntities())
    {
    
        Order order = new Order();
    
        order = context.Orders.SingleOrDefault(c => c.OrderID == ID);
    
            lblDate.Text = order.Date.ToShortDateString();
            lblCustomerForename.Text = order.Customer.Forename;
            lblCustomerSurname.Text = order.Customer.Surname;
    
            lblCity.Text = order.Customer.City;
            lblPostcode.Text = order.Customer.Postcode;
    
            OrderItem orderitem = new OrderItem();
            orderitem = context.OrderItems.SingleOrDefault(c => c.OrderID == ID);
    
            lblOrderID.Text = orderitem.OrderID.ToString();
    
            lblProductID.Text = orderitem.ProductID.ToString();
    
            Product product = new Product();
            product = context.Products.SingleOrDefault(c => c.ProductID == orderitem.ProductID);
    
            lblProductName.Text = product.Name.ToString();
  2. #2
  3. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,436
    Rep Power
    4539
    I don't see any reference to any database in the code you posted.
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0
    Hi there

    Thank you for your response.

    The website is connected to a database through a data model. That is how we have been told how to do it.


    cmpkwoodEntities is the model.

    Order is the database table.

    Code:
    using (cmpkwoodEntities context = new
          cmpkwoodEntities())
    {
    
        Order order = new Order();
    
        order = context.Orders.SingleOrDefault(c => c.OrderID == ID);
    Im not overly sure but I would hope this helps you help me
  6. #4
  7. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,436
    Rep Power
    4539
    Is this asp.NET? If so maybe try our NET forum, this forum is mostly classic asp with vbscript. I don't have any idea about your issue, sorry.
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester

IMN logo majestic logo threadwatch logo seochat tools logo