Click here to Skip to main content
11,705,208 members (39,006 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i am getting this error after i try to binding using this code
(can anyone help me with this error?)
<Grid x:Name="ContentGrid" Margin="0,22,0,-22" Grid.Row="1">
           <ListBox Height="444" HorizontalAlignment="Left" Margin="20,81,0,0" Name="listBox1" VerticalAlignment="Top" Width="434" >
               <ListBox.ItemTemplate>
                   <DataTemplate>
                       <StackPanel Orientation="Horizontal">
                           <TextBlock Text="{Binding username}" />
                           <TextBlock Text="{Binding password}" />
                           <TextBlock Text="{Binding gender}" />
                       </StackPanel>
                   </DataTemplate>
               </ListBox.ItemTemplate>
           </ListBox>
       </Grid>

error
System.Windows.Data Error: BindingExpression path error: 'username' property not found on 'lim' 'System.String' (HashCode=339799040). BindingExpression: Path='username' DataItem='lim' (HashCode=339799040); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..




my whole project code
xaml code
<phone:PhoneApplicationPage
    x:Class="Epromo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True" >
 
    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="83"/>
            <RowDefinition Height="685*"/>
        </Grid.RowDefinitions>
 
        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Margin="9,17,3,0"></StackPanel>
        <Button Content="On going"  HorizontalAlignment="Left" Margin="-5,85,0,0" VerticalAlignment="Top" Grid.RowSpan="2" Width="195"/>
        <Button Content="most view" HorizontalAlignment="Left" Margin="161,85,0,0" VerticalAlignment="Top" Grid.RowSpan="2" Width="186" RenderTransformOrigin="0.5,0.5"/>
        <Button Content="Coming soon" HorizontalAlignment="Left" Margin="321,85,-13,0" VerticalAlignment="Top" Grid.RowSpan="2" Width="170" FontSize="20" Height="72"/>
        <Grid x:Name="ContentGrid" Margin="0,22,0,-22" Grid.Row="1">
            <ListBox Height="444" HorizontalAlignment="Left" Margin="20,81,0,0" Name="listBox1" VerticalAlignment="Top" Width="434" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding username}" />
                            <TextBlock Text="{Binding password}" />
                            <TextBlock Text="{Binding gender}" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
        <Button Content="sign up" FontSize="17" Height="60" Name="signup" Width="120" Click="signup_Click" Margin="371,43,-11,647" Grid.RowSpan="2" />
        <TextBlock x:Name="ApplicationTitle" Style="{StaticResource PhoneTextNormalStyle}" FontSize="24" FontFamily="Times New Roman" Height="34" Margin="4,0,32,49">
 
                <Run Text="Epromo" /></TextBlock>
        <!--ContentPanel - place additional content here-->
    </Grid>
 
</phone:PhoneApplicationPage>


cs code

using Epromo.ServiceReference1;
 
namespace Epromo
{
    public partial class MainPage : PhoneApplicationPage
    {
        private Service1Client _serviceClient;
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Post_Loaded);
        }
        void Post_Loaded(object sender, EventArgs e)
        {
            _serviceClient = new Service1Client();
            _serviceClient.ListPostCompleted += new EventHandler<ListPostCompletedEventArgs>(getList_Completed);
            _serviceClient.ListPostAsync();
 
        }
        void getList_Completed(object sender, ListPostCompletedEventArgs e)
        {
            listBox1.ItemsSource = e.Result;
        }
 
        private void signup_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/signupPage.xaml", UriKind.Relative));
        }
 

 

 
    }
}
Posted 18-Feb-13 5:58am
Comments
Richard MacCutchan at 18-Feb-13 13:16pm
   
What is the structure of the list you post at listBox1.ItemsSource = e.Result;?
Member 9844581 at 18-Feb-13 20:56pm
   
public List ListPost()
{
List postList = new List();
SqlConnection sqlConnection = new SqlConnection(connectionString);
SqlCommand command = sqlConnection.CreateCommand();
command.CommandText = "select * from tbl_user";
sqlConnection.Open();
DataTable tblReport = new DataTable();
tblReport.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
foreach (DataRow dataRow in tblReport.Rows)
{
//reportList.Add(dataRow["EmpID"].ToString());
postList.Add(dataRow["username"].ToString());
postList.Add(dataRow["password"].ToString());
postList.Add(dataRow["gender"].ToString());
postList.Add(" ");
}
return postList;
}
Richard MacCutchan at 19-Feb-13 3:17am
   
I don't see any structure there, you just have a single list of strings, so how can the bindings find the items?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

modify the Binding
<TextBlock Text="{Binding username}" />
to
<TextBlock Text="{Binding}" />

and remove the other two...examine the result! what are you getting?

Hint: your are transforming the DataTable to a List, where the columns of each record makes up a complete new "record" in your list, e.g.:
The DataTable looks like:

User1 | Password1 | male
User2 | Password2 | male
User3 | Password3 | female

THe lsit looks like:
User1
Password1
male
User2
Password2
male
User3
Password3
female


The ItemsControl now tries to display all of those 9 records represented as a string. for each string it applies the DataTemplate. The Binding to e.g. "username" now tries to get a property named "username" on an ionstance of Type String.

Another clue: Modify the Binding, so that it reads:
<TextBlock Text="{Binding Length}" />
(Binding to a Property "Length" that should actually exist on type String)

With this in mind, reexamine the Binding-Error. All this Information is in there!

I hope i could clear up things a little bit
  Permalink  
Comments
Member 9844581 at 19-Feb-13 5:37am
   
how can i doing something like this

http://silverlight1blogpost.s3.amazonaws.com/step84.png[^]

i am new in programming, pls help...thk a lot
Earloc at 19-Feb-13 7:31am
   
you´re on the right way.
Define a class "User" with public properties "Username", "Password", "Gender".
modify the "List" stored in variable listPost in your above example to hold instances of this class.
for each record in the resulting DataSet, instantiate a new Instance of "User" class, setting the Properties according to the corresponding Cell of your DataRow.
Add these new instances to the List (which result in setting the ItemsSource-Property of the ListView.
Modify the Bindings in your DataTemplate to match the "User"-classes propertynames (case sensitive).
Add Eyecandy to your DataTemplate so it looks like the example you posted in the pic

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 550
1 OriginalGriff 244
2 ppolymorphe 126
3 CPallini 82
4 F-ES Sitecore 70
0 OriginalGriff 8,988
1 Sergey Alexandrovich Kryukov 8,272
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,606


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100