0

I previously asked a question about back end development, I've been working using the solutions provided but I've hit a snag. I understand that I can create tables in an Azure database, and I can add entries to these tables using Microsoft Database studio.

My problem now is, I'm making a cross platform application that supposed to access these data and display them on the screen of the app.

The table has two columns, "Key" and "Text"

The app starts by loading an array of buttons, each button is assigned a key from the table in order and shows an snippet of the associated "Text". When clicked they open a new page showing the full text on the page.

It sounded easy enough but I don't know how to implement it. What I had in mind was something that looks like, forgive, this is just a rough illustration:

//each button is some sort of class
for (int i=1; i< Table.length; i++){
    create button
    button.key=i;
    button.text=table[i].text;
}

//each button has an onclick method
void onClick(){
    open page with text box
    textbox.text=button.text
}

if this was all arrays, I could probably do this but with tables, I have very little idea what to do. I'd really appreciate some help with this.

Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47
Drew U
  • 443
  • 2
  • 8
  • 17

1 Answers1

0

if this was all arrays, I could probably do this but with tables, I have very little idea what to do.

I will provide a sample code which will help you query data from table and save it to a collection.

If you use C# as your programming language, you could query data from Azure SQL database using ADO.NET. If you use .NET Core, you need to install 'System.Data.SqlClient' package before using ADO.NET.

C# code, define a class which has 2 properties.

public class KeyAndText
{
    public string Key { get; set; }

    public string Text { get; set; }
}

C# code:Get data from table and save to a collection

List<KeyAndText> keyAndTextCollection = new List<KeyAndText>();

string connectionString = "";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("select * from tablename", connection);
connection.Open();

SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
    KeyAndText item = new KeyAndText();
    item.Key = reader.GetString(reader.GetOrdinal("Key"));
    item.Text = reader.GetString(reader.GetOrdinal("Text"));

    keyAndTextCollection .Add(item);
}

connection.Close();

You could get the connection string from Azure portal.

enter image description here

By default, Azure SQL Firewall will block the client to access Azure SQL data if we haven't configure the IP address in Firewall settings. Please configure it before running your application.

enter image description here

After upper step, you will get a collection named keyAndTextCollection which contains list of key and text. You could use it to generate buttons.

for (int i = 1; i < keyAndTextCollection.Count; i++)
{
    create button
    button.key = keyAndTextCollection[i].Key;
    button.text = keyAndTextCollection[i].Text;
}
Amor
  • 8,325
  • 2
  • 19
  • 21
  • O my Gosh, THANK YOU! I've been trying to figure this out for days, Thank you so much! – Drew U May 03 '17 at 19:53
  • hello, I ran into a problem trying to use your method. The question is posted here: [Issues installing System.Data.SqlClient](http://stackoverflow.com/questions/43770293/cannot-instell-system-data-sqlclient-in-visual-studio-xamarin-project) – Drew U May 03 '17 at 21:28