2

I have inserted the data into the SqlCe Database with the below code:

using (DBContacts context = new DBContacts(ConnectionString))
            {
                TblContacts tblCtc = new TblContacts();

                tblCtc.FirstName = txtBoxFirstNm.Text;
                tblCtc.LastName = txtBoxLastNm.Text;
                tblCtc.Mobile1 = txtMobile1.Text;
                tblCtc.Email1 = txtEmail1.Text;

                context.TblContacts.InsertOnSubmit(tblCtc);
                context.SubmitChanges();

                MessageBox.Show("Inserted Ok.");


            }

Questions:

1) How do I use select from Linq To SQL and display the retrieved data ?

 using (DBContacts context = new DBContacts(ConnectionString))
            {
              IEnumerable  ctc = from c in context.TblContacts select c;   


        // what to get the result and display ??

               // txtBlkFirstname.Text =   ctc.FirstName  ??
               // txtBlkLastname.Text = ctc.LastName    ??
               .....     

            }

1a) Which to use ? IQueryable or IEnumerable?

1b) What does Select-statement return? How to handle the data-return?

1c) How to get the data out from Iqueryable or IEnumerable and assign values to the TextBlock controls?

------ How to bind the data to the ListBox

2
  • HI there, can i get a bit of more of code so that i can help you Commented Jul 22, 2011 at 11:33
  • Woukd appreciate if you can tell me which part I missed out. I got stuck on the second part : retrieving data. Commented Jul 22, 2011 at 11:58

1 Answer 1

2
public List<TblContacts> GetAllContact()
    {
        using (DBContacts context = new DBContacts(ConnectionString))
        var contacts = from m in context.TblContacts select m;
        return contacts.Take(100).ToList();
    }

Something similar I use in my WCF Service, should work in your app too I hope,

Try to bind a listbox source to the GetAllContacts() and then inside that listbox bind their data to the individual fields

       <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel Name="contentP" Opacity="1">
            <ListBox Height="623" Margin="-20,0,0,0" Name="listBox1" Width="473">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="132">
                            <Image Source="{Binding ImageSource}" Height="75" Width="75" VerticalAlignment="Top" Margin="15,10,10,0"/>
                            <StackPanel Width="380">
                                <ContentControl Margin="0,10,0,0" HorizontalAlignment="Left">
                                    <HyperlinkButton HorizontalAlignment="Left" Style="{StaticResource HyperlinkButtonStyle}" Content="{Binding Title}" Foreground="#FFC8AB14" NavigateUri="{Binding Link}" TargetName="_blank"/>
                             </ContentControl>
                             <TextBlock Margin="10,10,30,0" Text="{Binding Message}" Foreground="{StaticResource PhoneAccentBrush}" TextWrapping="Wrap" FontSize="20" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>           
    </Grid> 

in your code you would set the "listbox1" ItemsSource to the source of your DBQuery, not sure how you would do it in your case as I do it using my WCF Service but its something liek this

listbox1.ItemsSource = (result of your query ("e.Result;");
Sign up to request clarification or add additional context in comments.

1 Comment

can you show me how to bind the Data to the ListBox with a TextBlock inside? Like <listBox> <dataTemplate> <textBlock>....</Listbox> what to include for binding in textBlock?Thanks

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.