/…../
using MongoDB;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
MongoClient mongoClient { get; set; }
MongoServer server { get; set; }
MongoDatabase database { get; set; }
MongoCollection<facultyData> collection { get; set; }
public MainWindow()
{
InitializeComponent();
loginDialog dlg = new loginDialog();
dlg.ShowDialog();
var dateTime = DateTime.Now;
dateTime_label.Content = dateTime;
}
public void Window_Loaded(object sender, RoutedEventArgs e)
{
MongoClient mongoClient = new MongoClient();
MongoServer server = mongoClient.GetServer();
var database = server.GetDatabase("facultyDataAndSchedule");
MongoCollection<facultyData> collection = database.GetCollection<facultyData>("faculty");
var results = collection.FindAll();
List<facultyData> resultList = results.ToList<facultyData>();
// Bind result data to WPF view.
if (resultList.Count() > 0)
{
Binding bind = new Binding(); //create a new binding to be used on the wpf
facultyDataGrid.DataContext = resultList; //sets the data binding for the control
facultyDataGrid.SetBinding(DataGrid.ItemsSourceProperty, bind); //syncs the data
facultyID_Textbox.DataContext = resultList;
facultyID_Textbox.SetBinding(DataGrid.ItemsSourceProperty, bind);
lastName_TextBox.DataContext = resultList;
lastName_TextBox.SetBinding(DataGrid.ItemsSourceProperty, bind);
}
}
private void viewMenu_click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello", "hiThere");
}
private void fRViewMenu_click(object sender, RoutedEventArgs e)
{
documentViewer docViewer = new documentViewer();
docViewer.ShowDialog();
}
private void selectionChanged(object sender, SelectionChangedEventArgs e)
{
facultyData row = (facultyData)facultyDataGrid.SelectedItem;
facultyID_Textbox.Text = row.facultyID;
lastName_TextBox.Text = row.lastName;
firstName_TextBox.Text = row.firstName;
middleName_TextBox.Text = row.middleName;
age_TextBox.Text = row.age.ToString();
}
public void addData_Click(object sender, RoutedEventArgs e)
{
try
{
var entity = new facultyData { facultyID = "F0003" , acadYear ="421", age=1, firstName="fags", dateOfBirth="11/11/2011", lastName="gsdg", middleName="fg", program="gag", rank="gegs", services="gegsg", status="geh", yearsOfTeachingO=1, yearsOfTeachingS=1};
collection.Insert(entity);
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
}
class facultyData
{
public ObjectId _id { get; set; }
public string facultyID { get; set; }
public string acadYear { get; set; }
public string program { get; set; }
public string lastName { get; set; }
public string firstName { get; set; }
public string middleName { get; set; }
public string dateOfBirth { get; set; }
public int age { get; set; }
public string rank { get; set; }
public int yearsOfTeachingS { get; set; }
public int yearsOfTeachingO { get; set; }
public string status { get; set; }
public string services { get; set; }
}
}
It is working fine on my window_loaded method, but not on the addData_Click.
...