6 Replies - 4264 Views - Last Post: 28 June 2012 - 10:46 AM Rate Topic: -----

#1 hueikar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-June 12

How to update multiple data into sql server

Posted 21 June 2012 - 10:22 PM

I am using below coding to get shopping cart list at listbox.
public List<Order> FindOrder(string custemail, string orderstatus)
{
   var findorder = from r in order.Orders 
                   where (r.CustEmail  == custemail && r.OrderStatus == orderstatus ) 
                   select r;
   return findorder.ToList();
}

public pgShopCart()
{    
    var obj = App.Current as App;
    custemail = obj.custEMAIL;
    string orderstatus = "Pending";
    proxy.FindOrderCompleted += new EventHandler<FindOrderCompletedEventArgs>(proxy_FindOrderCompleted);
    proxy.FindOrderAsync(custemail, orderstatus);
}

void proxy_FindOrderCompleted(object sender, FindOrderCompletedEventArgs e)
{
    ListBox1.ItemsSource = e.Result;
}


After that I want to update the orderId status become "success" but I am only able to update 1 row only by using assign listbox1.itemsource into observable collection.

void proxy_FindOrderCompleted(object sender, FindOrderCompletedEventArgs e)
{
    ListBox1.ItemsSource = e.Result;
    ObservableCollection<Order> Orders = this.ListBox1.ItemsSource as ObservableCollection<Order>;

    foreach (Order order in Orders)
    {
        orderid = order.OrderID;
    } 
}


and this to update row:

public void UpdateOrderStatus(int orderid, string orderstatus, string paymentdate)
{
    Order data = order.Orders.Single(p => p.OrderID == orderid);
    data.OrderStatus = orderstatus;
    order.SubmitChanges();
}

private void btnPay_Click(object sender, RoutedEventArgs e)
{
    proxy.UpdateOrderStatusCompleted += new EventHandler<UpdateOrderStatusCompletedEventArgs>(proxy_UpdateOrderStatusCompleted);
    proxy.UpdateOrderStatusAsync(orderid, orderstatus);
}


Any idea to edit my coding for update multiple rows into database?

Is This A Good Question/Topic? 0
  • +

Replies To: How to update multiple data into sql server

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to update multiple data into sql server

Posted 22 June 2012 - 06:42 AM

Couldn't decide whether this was WPF or Silverlight, so moved to WPF.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to update multiple data into sql server

Posted 22 June 2012 - 06:54 AM

Add a new method that accept a collection of Orders. Something like this...

public void UpdateOrderStatuses(IEnumerable<Order> orders, string orderStatus, string paymentDate)
{
     foreach (var order in orders)
     {
          // Calls your already existing method for one order
          UpdateOrderStatus(order.OrderID, orderStatus, paymentDate);
     }
}


Was This Post Helpful? 1
  • +
  • -

#4 hueikar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-June 12

Re: How to update multiple data into sql server

Posted 24 June 2012 - 10:47 AM

View Posteclipsed4utoo, on 22 June 2012 - 06:54 AM, said:

Add a new method that accept a collection of Orders. Something like this...

public void UpdateOrderStatuses(Enumerable<Order> orders, string orderStatus, string paymentDate)
{
     foreach (var order in orders)
     {
          // Calls your already existing method for one order
          UpdateOrderStatus(order.OrderID, orderStatus, paymentDate);
     }
}



Thnx. But it show me error for at Enumerable..

and how can i call this function under button1_click?
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to update multiple data into sql server

Posted 25 June 2012 - 05:08 AM

It should be IEnumerable instead. Enumerable is from the System.Linq namespace, which isn't generic. IEnumerable is in the System.Collections.Generic namespace, and it is generic.

Typo on my part.
Was This Post Helpful? 0
  • +
  • -

#6 hueikar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-June 12

Re: How to update multiple data into sql server

Posted 28 June 2012 - 10:24 AM

Hi, i had try with ur coding with button1_click too
 private void button1_Click(object sender, RoutedEventArgs e)
         {
             var orders = this.ListBox1.ItemsSource as ObservableCollection<Order>;
             string orderstatus = "Success";
             string paymentdate = DateTime.Now.ToString("MM\\/dd\\/yyyy h\\:mm tt");
             UpdateOrderStatuses(orders, orderstatus, paymentdate);
}


but it still same as just update 1 of my record only..
I try to make counting my orderid inside observable collection to make a for loop statement.but it didnt work too.

 foreach (Order o in Orders)
            {
                ObservableCollection<Order> Orders = this.ListBox1.ItemsSource as ObservableCollection<Order>;
		proxy.UpdateOrderStatusAsync(o.OrderID, o.OrderStatus, o.PaymentDate);
                int numOrder = Orders.Count(x => x.OrderID == o.OrderID);
            }


the result show me value 1 but the real is i had 3 record for my orderid.
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to update multiple data into sql server

Posted 28 June 2012 - 10:46 AM

int numOrder = Orders.Count(x => x.OrderID == o.OrderID);



This code should always return one, as the OrderID should be unique between all orders.

Also, this code..

ObservableCollection<Order> Orders = this.ListBox1.ItemsSource as ObservableCollection<Order>;



is not needed in the foreach loop.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1