Click here to Skip to main content
15,919,178 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

This application is related to sending group sms and at present Im saving it in the database. When I select the item in the combo box I will get the contacts related to it. Im selecting the contacts using checkbox, I should be able to send sms to the contacts that I check. Just go through the below code



XML
<ListBox Height="326" HorizontalAlignment="Left"
                    Margin="6,101,0,0" Visibility="Visible"
                    Name="lbContacts" VerticalAlignment="Top" Width="300" ItemsSource="{Binding}" >
               <ListBox.ItemTemplate>
                   <DataTemplate>
                       <ListBoxItem>
                           <StackPanel Orientation="Vertical">
                               <CheckBox Name="chkContactName" Content="{Binding ContactName}" />
                               <TextBlock Name="tbMobileNo" Text="{Binding MobileNo}"/>

                           </StackPanel>
                       </ListBoxItem>
                   </DataTemplate>
               </ListBox.ItemTemplate>
           </ListBox>
           <TextBox Height="132" HorizontalAlignment="Left" Margin="-13,433,0,0" Name="txtMessage" VerticalAlignment="Top" Width="460" />
           <Button Content="Send" Height="72" HorizontalAlignment="Left" Margin="287,566,0,0" Name="btnSend" VerticalAlignment="Top" Width="160" Click="btnSend_Click" />







and the below code is to iterate through all the items in the listbox.
//Im getting the exception as invalid cast exception in the below foreach loop

MSIL
private void btnSend_Click(object sender, RoutedEventArgs e)
       {
           List<string> lst = new List<string>();

           foreach(ListBoxItem li in lbContacts.Items)
           {
               CheckBox chk = (CheckBox)li.FindName("chkContactName");
               if (chk.IsChecked==true)
               {
                   TextBlock tbmob = (TextBlock)li.FindName("tbMobileNo");
                   lst.Add(tbmob.Text);
               }
           }
           for (int i = 0; i < lst.Count; i++)
           {
               SM objsms = new SM();
               objsms.FKUserId = fkuserid;
               objsms.FKContactId = ((Contact)lbContacts.SelectedItem).PKContactId;
               objsms.Message = txtMessage.Text;
               objsms.SentDate = System.DateTime.Now;
               try
               {
                   proxy.InsertSMSAsync(objsms);
                   MessageBox.Show("SMS sent successfully");
               }
               catch
               {
                   MessageBox.Show("SMS cannot be sent");
               }
           }
       }


can anyone help me to solve this problem
Posted
Updated 9-Jul-11 3:05am
v3
Comments
DaveAuld 9-Jul-11 9:05am    
edit: removed excess pre tag

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900