none
[UWP] Service database Crash RRS feed

  • Question

  • When I call the service in my application that connect with Azure to get data from SQL database the app sometimes crash with exception. The app sometimes work fine but sometimes crash. This is the my code 

    - Page1.xaml.cs -

    public GetTag.View View { get; set; }
    public new ObservableCollection<GetTag.UserTag> UserTag { get; set; }
    public ObservableCollection<GetTag.DataPromo> PromoListDB { get; set; }
    public ObservableCollection<GetTag.SelectTable> Selected { get; set; }
    public ObservableCollection<GetTag.PromoTimer> Timer { get; set; }
    ...
    // Other Declare variable
    ...
            public Promo()
            {
                View = new GetTag.View();
                UserTag = new ObservableCollection<GetTag.UserTag>();
                Timer = new ObservableCollection<GetTag.PromoTimer>();
                PromoListDB = new ObservableCollection<GetTag.DataPromo>();
                Selected = new ObservableCollection<GetTag.SelectTable>();
                ...
                // Code before call service
                ...
                GetTag.Service1Client client5 = new GetTag.Service1Client();
                GetView = client5.GetUserViewPromoAsync(Int32.Parse(HashUser.UserID)).Result;
                Debug.WriteLine(GetView.Count);
                if (GetView.Count != 0)
                {
                    GetTag.Service1Client client2 = new GetTag.Service1Client();
                    GetTag = client2.GetTagofPromotionAsync(GetView[GetView.Count - 1]).Result;
                    for (int i = 0; i < GetTag.Count; i++)
                    {
                        Debug.WriteLine(GetTag[i]);
                        if (i < 4)
                        {
                            sender2 = sender2 + GetTag[i] + " ";
                            if (i >= 1)
                            {
                                f = 1;
                            }
                        }
                    }
                    sender2 = sender2.Remove(sender2.Length - 1);
                    GetTag.Service1Client client3 = new GetTag.Service1Client();
    
                    Selected = client3.GetRankAsync(sender2, f).Result;
                    for (int i = 0; i < Selected.Count; i++)
                    {
                        Debug.WriteLine(Selected[i].TagID + " " + Selected[i].Ranking);
                    }
                    Debug.WriteLine("===Without Duplicate===");
                    for (int i = 0; i < Selected.Count; i++)
                    {
                        for (int j = 0; j < Selected.Count; j++)
                        {
                            if ((Selected[i].TagID == Selected[j].TagID) && (i != j))
                            {
                                Selected.Remove(Selected[j]);
                            }
    
                        }
                    }
                    GetTag.Service1Client client6 = new GetTag.Service1Client();
                    Timer = client6.GetPromoTimerAsync(Selected).Result;
                    GetTag.Service1Client client4 = new GetTag.Service1Client();
                    UserTag = client4.GetUserTagDBAsync(Int32.Parse(HashUser.UserID)).Result;
                    for (int i = 0; i < UserTag.Count; i++)
                           Interest.Add(UserTag[i].TagName);
                    GetTag.Service1Client client = new GetTag.Service1Client();
    
                ...
    
            }
               

    - AzureCloudeService (Example of some part)-

     public List<int> GetUserViewPromo(int UserID)
            {
                SqlConnection conn = new SqlConnection();
                List<int> Promo = new List<int>();
                try
                {
                    conn.ConnectionString = "[ConnectionString]";
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    //Query Here
                    String cmdText = "Select PromoID from PromoViewLog Where UserID=@input ";
                    cmd.Parameters.AddWithValue("@input", UserID);
                    cmd.CommandText = cmdText;
                    cmd.CommandType = CommandType.Text;
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        Promo.Add(dr.GetInt32(0));
                    }
                    dr.Close();
                    return Promo;
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }
            }
    ...       
     public ObservableCollection<PromoTimer> GetPromoTimer(ObservableCollection<SelectTable> x)
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = "[Connection String]";
                ObservableCollection<PromoTimer> Timer = new ObservableCollection<PromoTimer>();
                int count = 0;
                SqlCommand cmd;
                for (int i = 0; i < x.Count; i++)
                {
                    count = 0;
                    for (int j = 0; j < x.Count; j++)
                    {
                        if (x[i].Ranking > x[j].Ranking) count++;
                    }
                    conn.Open();
                    cmd = new SqlCommand();
                    cmd.Connection = conn;
                    String cmdText = "Select PromoID From PromoTag Where TagID=@ID";
                    cmd.Parameters.AddWithValue("@ID", x[i].TagID);
                    cmd.CommandText = cmdText;
                    cmd.CommandType = CommandType.Text;
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        Timer.Add(new PromoTimer { PromoID = dr.GetInt32(0), Multipy = (count + 1.00) / x.Count });
                    }
    
                    conn.Close();
                }
                return Timer;
                throw new NotImplementedException();
            }


    Thursday, October 27, 2016 2:42 PM

Answers

  • Hello Setnipat kriangsakdachai,

    Welcome to the UWP forum and your detailed code.

    The problem is that we can see your code but we have no idea about why it crashes. Since I'm not so familiar with AzureCloudeService I think we need to take the followings things in order to troubleshoot this issue:

    1. From code level we need to get the exception info in details. I've checked some articles and saw this:

    https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-dotnet-how-to-use-client-library/

    Where it implement some exceptions that you can catch, like MobileServicePreconditionFailedException,MobileServiceInvalidOperationException and so on. However from your code I can see that you are trying to directly connect to the database via a connectionstring. So your database is a SQL Server database? And you are using WCF to connect to your SQL Server?  We need to track the exception info in order to know what happens.

    2. From Azure part I'm thinking that maybe you can ask on Azure Community forum to see if they have a dashboard which can help you check the crashes for their apps.

    Best regards,

    Barry 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 31, 2016 7:38 AM
    Moderator