none
UWP with azure SQL database RRS feed

  • Question

  • Due to my information I have known that UWP need to access SQL database by using service so I use the service on cloud service I'm done an get the data from DB in all table except user which I cannot call and it's without reason due to other one can call with the same method but change only the table name


    And I call with function in the service

     public ObservableCollection<User> GetTagDB()
            {
                ObservableCollection<User> result = new ObservableCollection<Tag>();
                SqlConnection conn = new SqlConnection();
                try
                {
                    conn.ConnectionString = //Connection String
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    //Query Here
                    String cmdText = "Select * from User";
                    cmd.CommandText = cmdText;
                    cmd.CommandType = CommandType.Text;
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        result.Add(new Tag() { UserID = dr.GetDecimal(0), UserEmail = dr.GetString(1) });
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
    
                }
                return result;
            }

    I've try to add the code line by line and see where is the problem occur and found out that it crash on SqlDataReader dr = cmd.ExecuteReader(); and I don't know why.Is there should be a solution for these problem?

    Thank you for any suggestion

    Tuesday, March 29, 2016 7:02 PM

Answers

  • Hi Wichaya,

    Based on your code snippet, I create a similar demo like below, I don’t encounter the issue that you occurring. Could you please provide a bit more information about the issue (such as detail error message) to us?

    List<Tag> result = new List<Tag>();
                string cs = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Demo;Integrated Security=True";
                SqlConnection conn = new SqlConnection();
                try
                {
                    conn.ConnectionString = cs; //Connection String
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    //Query Here
                    String cmdText = "Select * from UserWithoutPK";
                    cmd.CommandText = cmdText;
                    cmd.CommandType = CommandType.Text;
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        result.Add(new Tag() { UserID = dr.GetDecimal(0), UserEmail = dr.GetString(1) });
                    }
      dr.Close();
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }
                foreach (var tag in result)
                {
                    Console.WriteLine("{0}--{1}", tag.UserID, tag.UserEmail);
                }
                Console.ReadKey();
    

    Best regards,

    Cole Wu

    Thursday, March 31, 2016 5:14 AM
    Moderator

All replies

  • Hi Wichaya,

    Move to "ADO.NET Managed Providers" forum for a better support, thanks for your understanding :)

    Best Regards,

    Xavier Eoro


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 30, 2016 9:33 AM
  • Hi Wichaya,

    Based on your code snippet, I create a similar demo like below, I don’t encounter the issue that you occurring. Could you please provide a bit more information about the issue (such as detail error message) to us?

    List<Tag> result = new List<Tag>();
                string cs = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Demo;Integrated Security=True";
                SqlConnection conn = new SqlConnection();
                try
                {
                    conn.ConnectionString = cs; //Connection String
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    //Query Here
                    String cmdText = "Select * from UserWithoutPK";
                    cmd.CommandText = cmdText;
                    cmd.CommandType = CommandType.Text;
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        result.Add(new Tag() { UserID = dr.GetDecimal(0), UserEmail = dr.GetString(1) });
                    }
      dr.Close();
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }
                foreach (var tag in result)
                {
                    Console.WriteLine("{0}--{1}", tag.UserID, tag.UserEmail);
                }
                Console.ReadKey();
    

    Best regards,

    Cole Wu

    Thursday, March 31, 2016 5:14 AM
    Moderator
  • Error Count = error CS0103: The name 'InnerExceptionCount' does not exist in the current context I use this code with other table is work fine except this User table that occur the problem
    Thursday, March 31, 2016 1:37 PM
  • OK I solve it now by using the dummy database with other name that is not user. Anyway thank you very much for a suggestion.
    Saturday, April 2, 2016 9:15 AM