2

I have silverlight 4 app. host ASP.NET web app. enabled WCF RIA services. From sever side apach2 php5 Mysql. With MySql connector, ADO.NET Entity Data Model, Domain Service class I have access to MySql database. Work fine... (for me slowly, but WORK).

table:

languages
        LANG_ID char(10)
        LANG_NAME char(40)
        LANG_NAME_SHORT char(10)
        LANG_IMAGE blob
        LANG_PATH char(100)
        DEFAULT char(1)
        STATUS_ID char(1)

xaml:

    <ComboBox x:Name="Language_Choice" Height="25" Width="110">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding LANG_PATH}" Width="16" Height="16"></Image>
                    <TextBlock Text="{Binding LANG_NAME_SHORT}" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

Code:

        DataDomainContext _context = new DataDomainContext();
        EntityQuery<languages> _query = from _l in _context.GetLanguagesQuery()
                                        where _l.STATUS_ID == "Y"
                                        orderby _l.LANG_NAME_SHORT
                                        select _l;
        LoadOperation<languages> _loadOp = _context.Load(_query);
        Language_Choice.ItemsSource = _loadOp.Entities;

Question: I want result query put into List:

public class LangChoice
{
    public string LangID { get; set; }
    public string LangName { get; set; }
    public byte LangImg { get; set; }
    public string LangPath { get; set; }
    public string LangDef { get; set; }
}     List<LangChoice> LangList = new List<LangChoice>();

Is there any way to put result to list and save list item index where languages.DEFAULT == "Y". Without list I don't know defined Language_Choice.SelectedIndex=0;(error=out of range) in xaml or code.

I'm new "developer". Is I write a code generic SELECT like: "SELECT fild1, fild2 FROM table WHERE field_900="HELP" ..." I don't wont result all field from table, only defined. Environment (c# Silverlight, Apach, PHP, MySql with WCF RIA Services)

Forwards Thanks

1 Answer 1

1

I find one way "answer"...

Add to ASP.NET Web Application Project: new "Silverlight-enabled WCF Service". Write to following code:

    [OperationContract]
    public List<Languages> LanguageList(string query)
    {
        string nwConn = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
        var langList = new List<Languages>();
        using (MySqlConnection conn = new MySqlConnection(nwConn))
        {
            conn.Open();
            using (MySqlCommand cmd = new MySqlCommand(query, conn))
            {
                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                if (dr != null)
                    while (dr.Read())
                    {
                        var lang = new Languages
                        {
                            LangID = dr.GetString(0),
                            LangName = " " + dr.GetString(1),
                            LangPath = dr.GetString(2),
                            LangDef = dr.GetString(3)
                        };
                        langList.Add(lang);
                    }
                return langList;
            }
        }
    }

[DataContract]
public class Languages
{
    [DataMember]
    public string LangID;
    [DataMember]
    public string LangName;
    [DataMember]
    public string LangPath;
    [DataMember]
    public string LangDef;
}

In web.config create conecction:
<connectionStrings> <add name="MyConnection" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;User Id=root;password=***;database=****;Persist Security Info=True"/> </connectionStrings>

In Silverlight client project: Add "Service Reference".

Now Code some think like:

        string query = @"SELECT `LANG_ID`, `LANG_NAME_SHORT`, `LANG_PATH`, 
                        `DEFAULT` FROM `languages` WHERE `STATUS_ID` = 'Y' ORDER BY `LANG_NAME_SHORT`";
        DataToListServiceClient proxy = new DataToListServiceClient();
        proxy.LanguageListCompleted += new EventHandler<LanguageListCompletedEventArgs>(proxy_LanguageListCompleted);
        proxy.LanguageListAsync(query);



    void proxy_LanguageListCompleted(object sender, LanguageListCompletedEventArgs e)
    {
        System.Collections.ObjectModel.ObservableCollection<Languages> _list = e.Result;
        Language_Choice.ItemsSource = _list;
        int i = 0; for (i = 0; i < _list.Count; i++) { if (_list[i].LangDef == "Y") break; }
        Language_Choice.SelectedIndex = i;
    }

Change XAML:

Source="{Binding LangPath}"  &  Text="{Binding LangName}"  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.