How do I Insert Record MVVM through WCF
Posted
by
DG KOL
on Stack Overflow
See other posts from Stack Overflow
or by DG KOL
Published on 2010-12-29T10:10:36Z
Indexed on
2010/12/29
10:54 UTC
Read the original article
Hit count: 227
Hello,
I’m new in MVVM Pattern (Using Galasoft MVVM Light toolkit). I’ve created a Test Project where I want to fetch some records from Database through WCF. This is working fine but I’ve failed to insert new record from View; Here is My Code:
Database
Table Name: TestUser (First Name, LastName)
WCF (NWCustomer)
Two Methods
Public List<TestUser> GetAllUsers() [“LINQ2SQL Operation”]
Public bool AddUser(TestUser testuser)
Public bool AddUser(TestUser testuser)
{
try
{
using (DBDataContext db = new DBDataContext())
{
TestUser test = new TestUser()
{
FirstName = testuser.FirstName,
LastName = testuser.LastName
};
db.TestUser.InsertOnSubmit(test);
db.SubmitChanges();
}
}
catch (Exception ex)
{
return false;
}
return true;
}
Silverlight Project
MODEL consists
ITestUserService.cs
TestUserService.cs
public void AddTestTable(TestTableViewModel testuser, Action<bool> callback)
{
NWCustomerClient client = new NWCustomerClient("BasicHttpBinding_NWCustomer");
client.AddTestUserCompleted += (s, e) =>
{
var userCallback = e.UserState as Action<bool>;
if (userCallback == null)
{
return;
}
if (e.Error == null)
{
userCallback(e.Result);
return;
}
userCallback(false);
};
client.AddTestUserAsync(testuser.Model);
}
VIEWMODEL
TestUserViewModel
public TestUser User
{
get;
private set;
}
public const string DirtyVisibilityPropertyName = "DirtyVisibility";
private Visibility _dirty = Visibility.Collapsed;
public Visibility DirtyVisibility
{
get
{
return _dirty;
}
set
{
if (_dirty == value)
{
return;
}
_dirty = value;
RaisePropertyChanged(DirtyVisibilityPropertyName);
}
}
public TestUserViewModel (TestUser user)
{
User = user;
user.PropertyChanged += (s, e) =>
{
DirtyVisibility = Visibility.Visible;
};
}
MainViewModel
public ObservableCollection<TestUserViewModel> TestTables
{
get;
private set;
}
public const string ErrorMessagePropertyName = "ErrorMessage";
private string _errorMessage = string.Empty;
public string ErrorMessage
{
get
{
return _errorMessage;
}
set
{
if (_errorMessage == value)
{
return;
}
_errorMessage = value;
RaisePropertyChanged(ErrorMessagePropertyName);
}
}
private ITestUserService _service;
public RelayCommand< TestUserViewModel> AddTestUserRecord
{
get;
private set;
}
public MainTestTableViewModel (ICustomerService service)
{
_service = service;
TestTables = new ObservableCollection<TestTableViewModel>();
service.GetAllTestTable(HandleResult);
}
private void HandleResult(IEnumerable<TestTable> result, Exception ex)
{
TestTables.Clear();
if (ex != null)
{
//Error
return;
}
if (result == null)
{
return;
}
foreach (var test in result)
{
var table = new TestTableViewModel(test);
TestTables.Add(table);
}
}
XAML
<Grid x:Name="LayoutRoot">
<StackPanel>
<TextBox Text="FirstName" />
<TextBox Text="LastName" />
<Button Content="Add Record" Command="{Binding AddTestUserRecord}" />
</StackPanel>
</Grid>
I want to add records into TestTable (Database Table). How do I insert record? In XAML two text box and a button control is present.
Thanking you.
DG
© Stack Overflow or respective owner