LINQ - Databinding questions

D

Dan Tallent

I have a form I'm playing around with that has some text boxes on it. I am
using SQL 2005 for my database.
I have a single table which I have created an object entity for. Customer

On the form I set the DataBindings Text value to "customerBindingSource -
CompanyName" on one of the textboxes.
The customerBindingSource was generated by the IDE and is pointing to the
Customer entity I created earlier. The CompanyName is a field within the
entity and underlying table.

During runtime I am using the follow code:

//----------------------------------------------------------------------------------
// This line is where my problem is.

this.CDC = new CustSiteDataContext(ConnString);


// Retreive customer record


var queryCust = from cust in CDC.Customers

where (cust.CustomerID == 100) && (cust.CompanyID == 5)

select cust;

this.customerBindingSource.DataSource = queryCust;

//----------------------------------------------------------------------------------

This works great, it reads the data in, I can make modifications and when I
click my button to save I execute:

this.CDC.SubmitChanges();

If the user wishes to abandon the changes, the idea is they can click a
button to do so. This code appears to work however I have to recreate a
new instance of the CustSiteDataContext for this to work.

In prior attempts I was looking at the refresh method, but I could not
figure out how to write it.
// CDC.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues);

Is the method I am using above the best/only way to implement "abandon"
changes ?



Thanks,

Dan
 
M

Marc Gravell

Largely, yes. Getting a fresh data-context is the safest way of
ensuring you aren't keeping any abandoned changes. You shouldn't keep
a data-context around for too long anyway; they are intended to be
locally scoped, not last for the duration of an app.

Marc
 
D

Dan Tallent

Thanks for the response. My idea was to only keep the DataContext while the
user resides on that particular form. Its great to know I'm not completely
lost.

Thanks
Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top