Recently i have started playing with C# in Visual Studio 2013 Community. I was creating a simple data binding while i encountered following error. What i was doing i was using drag-n-drop fr om my database to main window. Visual studio generated required XAML and .cs file. There was following error present with the code. I searched a lot and didn't found a solution. It was showing on
Error: "an object reference is required to access non-static field method or property"
following is my XAML.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:rrplproj" x:Class="rrplproj.MainWindow" Title="MainWindow" Height="700" Width="1024" WindowState="Maximized" Loaded="Window_Loaded"><Window.Resources><local:rDataSet x:Key="rDataSet"/><CollectionViewSource x:Key="dateViewSource" Source="{Binding Date, Source={StaticResource rDataSet}}"/><CollectionViewSource x:Key="deliveryViewSource" Source="{Binding Delivery,
Source={StaticResource rDataSet}}"/></Window.Resources>
<Grid Background="#FFDBFDF8"><Grid.ColumnDefinitions><ColumnDefinition Width="165*"/><ColumnDefinition Width="172*"/><ColumnDefinition Width="171*"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height="56*"/><RowDefinition Height="149*"/><RowDefinition Height="18*"/></Grid.RowDefinitions><Grid x:Name="grid1" DataContext="{StaticResource dateViewSource}" HorizontalAlignment="Left"
Margin="19,23,0,0" Grid.Row="1" VerticalAlignment="Top" Height="32" Width="151"><Grid.ColumnDefinitions><ColumnDefinition Width="Auto"/><ColumnDefinition Width="Auto"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height="Auto"/></Grid.RowDefinitions><Label Content="Date:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0"
VerticalAlignment="Center"/><DatePicker x:Name="dateDatePicker" Grid.Column="1" HorizontalAlignment="Left" Margin="3" Grid.Row="0"
SelectedDate="{Binding Date, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
VerticalAlignment="Center"/></Grid><Grid x:Name="grid2" Grid.Column="1" DataContext="{StaticResource deliveryViewSource}"
HorizontalAlignment="Left" Margin="64,18,0,0" Grid.Row="1" VerticalAlignment="Top"><Grid.ColumnDefinitions><ColumnDefinition Width="Auto"/><ColumnDefinition Width="Auto"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height="Auto"/></Grid.RowDefinitions><Label Content="Truck No:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0"
VerticalAlignment="Center"/><TextBox x:Name="truckNoTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3"
Grid.Row="0" Text="{Binding TruckNo, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
VerticalAlignment="Center" Width="120"/></Grid></Grid>
and following is auto generated code by ide in C#.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace rrplproj { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { // Load data into the table Date. You can modify this code as needed. rrplproj.rDataSetTableAdapters.DateTableAdapter rDataSetDateTableAdapter =
new rrplproj.rDataSetTableAdapters.DateTableAdapter(); rDataSetDateTableAdapter.Fill(rDataSet.Date); System.Windows.Data.CollectionViewSource dateViewSource =
((System.Windows.Data.CollectionViewSource)(this.FindResource("dateViewSource"))); dateViewSource.View.MoveCurrentToFirst(); // Load data into the table Delivery. You can modify this code as needed. rrplproj.rDataSetTableAdapters.DeliveryTableAdapter rDataSetDeliveryTableAdapter =
new rrplproj.rDataSetTableAdapters.DeliveryTableAdapter(); rDataSetDeliveryTableAdapter.Fill(rDataSet.Delivery); System.Windows.Data.CollectionViewSource deliveryViewSource =
((System.Windows.Data.CollectionViewSource)(this.FindResource("deliveryViewSource"))); deliveryViewSource.View.MoveCurrentToFirst(); } } }
The error is shown here:
rDataSetDateTableAdapter.Fill(rDataSet.Date);
and here:
rDataSetDeliveryTableAdapter.Fill(rDataSet.Delivery);it is showing red line under
(rDataSet.Date)and
(rDataSet.Delivery)
Your help is appreciated.
Thank you for reading till the last line.