Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

LINQ - problems updating entries in DB

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
K Viltersten - 24 Feb 2008 18:45 GMT
I was going to perform an update of an
existing row in my table in the DB by:

 Table<MyType> data = db.GetTable<MyType>();
 MessageBox.Show (data.Count ());
 foreach (MyType d in data) {
   MessageBox.Show (d.name);
   d.name = "changed";
 }
 db.SubmitChanges ();

and while i get the message box to present
the contents correctly, they seem not to
change ever.

An other strange thing i've noticed is
that even after i've added more rows to the
table (using the designer in VS), the
number of entries returned didn't grow. I
have the feeling that the two issues are
connected somehow.

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 24 Feb 2008 19:06 GMT
> I was going to perform an update of an
> existing row in my table in the DB by:
[quoted text clipped - 17 lines]
> have the feeling that the two issues are
> connected somehow.

It sounds like perhaps you've actually got two different databases (or
two different tables) and you aren't connecting to the database you
think you are.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

K Viltersten - 24 Feb 2008 19:41 GMT
>> I was going to perform an update of an
>> existing row in my table in the DB by:
[quoted text clipped - 15 lines]
> and you aren't connecting to the database you
> think you are.

Allright, that solved the second issue. Thanks
for that. Now, the harder part - the writing to
the DB. I know i connect to the right DB because
the changes i make there using the wizard can be
read and displayed in the program. Still, this:
 d.name = "changed";
 db.SubmitChanges ();
should cause the field to be changed (and
updated) in my DB but it doesn't. Suggestions?

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 24 Feb 2008 20:05 GMT
<snip>

> Allright, that solved the second issue. Thanks
> for that. Now, the harder part - the writing to
[quoted text clipped - 5 lines]
> should cause the field to be changed (and
> updated) in my DB but it doesn't. Suggestions?

Try setting the log output for the context so it can show what's going
on - that should give you hints as to why it's not working.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

K Viltersten - 24 Feb 2008 21:57 GMT
>> Allright, that solved the second issue. Thanks
>> for that. Now, the harder part - the writing to
[quoted text clipped - 9 lines]
> can show what's going on - that should give you
> hints as to why it's not working.

I used the following.
 DataContext db = new DataContext (@"C:\deebee.sdf");
  db.Log = Console.Out;

I can't see any output in the output window,
though. It says it's set for showing the
output for build and it's the only option
available, so i guess it's correct...

Any other hints?

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 24 Feb 2008 22:26 GMT
> >> Allright, that solved the second issue. Thanks
> >> for that. Now, the harder part - the writing to
[quoted text clipped - 18 lines]
> output for build and it's the only option
> available, so i guess it's correct...

Well, the obvious next step is to explicitly write something to the
console so you can see if that makes it.

> Any other hints?

If you can't see console output, write to a file instead.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

K Viltersten - 24 Feb 2008 22:21 GMT
>> Allright, that solved the second issue. Thanks
>> for that. Now, the harder part - the writing to
[quoted text clipped - 9 lines]
> can show what's going on - that should give you
> hints as to why it's not working.

I changed my application into console app and got
the output to a command window. It then tells me:

SELECT COUNT(*) AS [value]
FROM [Test01] AS [t0]
-- Context: SqlProvider(SqlCE) Model: AttributedMetaModel Build: 3.5.21022.8

and

SELECT [t0].[name], [t0].[id]
FROM [Test01] AS [t0]
-- Context: SqlProvider(SqlCE) Model: AttributedMetaModel Build: 3.5.21022.8

The "id" and "name" are my columns and "Test01"
is my table.

As far i understand, the first statement is the
one checking for the number of elements, while
the second retrieves the data. There's no third
statement for updating the table with my change!

Suggestions on how to kill this problem?

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 24 Feb 2008 22:31 GMT
> > Try setting the log output for the context so it
> > can show what's going on - that should give you
[quoted text clipped - 22 lines]
>
> Suggestions on how to kill this problem?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Any reason you're using GetTable<T> instead of the autogenerated
property on your particular DataContext, by the way? That could be
relevant.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

K Viltersten - 25 Feb 2008 00:18 GMT
>> I changed my application into console app and got
>> the output to a command window. It then tells me:
[quoted text clipped - 16 lines]
> Could you post a short but complete program
> which demonstrates the problem?

Yes, i'll try. See at the bottom of this post.

> Any reason you're using GetTable<T> instead of
> the autogenerated property on your particular
> DataContext, by the way? That could be relevant.

I don't get anything autogenerated when it comes to
LINQ. For some reason (described in another post) i
get errors when drag-and-dropping.

Here's the code. Of course it's adapted and
rewritten but shows the core of the problem.

namespace WinFormTest01
{
public partial class Settings : Form
{
 public Settings()
 {
  InitializeComponent();
 }
 private void btnAction_Click (object sender, EventArgs e)
 {
  // LINQ part here
  DataContext db = new DataContext (@"C:\deebee.sdf");
  db.Log = Console.Out;
  Table<MyType> data = db.GetTable<MyType>();
  MessageBox.Show ("number read:   " + data.Count ());

  foreach (MyType d in data)
  {
   MessageBox.Show (d.id + "   >>" + d.sector + "<<");
   d.sector = "a";
  }
  db.SubmitChanges ();
 }
}
}

[Table (Name = "Test01")]
public class MyType : INotifyPropertyChanged
{
 private String _id;
 [Column (Name = "id")]
 public String id
   { get { return this._name; } set { this._name = value; } }
 private String _sector;
 [Column (Name = "sector")]
 public String sector
 {
  get { return this._sector; }
  set
  {
   this._sector = value;
   doNotifyChange ("sector");
  }
 }
 // Guid as suggested somewhere, no idea why
 private Guid _ID = Guid.NewGuid ();
 public Guid ID { get { return this._ID; } }

 public event PropertyChangedEventHandler PropertyChanged;

 private void doNotifyChange (String str)
 {
  // PropertyChanged is always null!
  if (PropertyChanged != null)
  {
   Debug.WriteLine ("changing property");
   PropertyChanged (this, new PropertyChangedEventArgs (str));
  }
 }
}

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 25 Feb 2008 07:30 GMT
> > Could you post a short but complete program
> > which demonstrates the problem?
[quoted text clipped - 8 lines]
> LINQ. For some reason (described in another post) i
> get errors when drag-and-dropping.

Hmm... I think your time would be best spent trying to fix that. Doing
it all manually is going to be a pain...

I'll have a closer look at your code later on - it looks okay at a
quick glance, but I'm sure that PropertyChanged should end up being
subscribed to by the DataContext code, but I can't see why that's not
happening :(

Have you tried implementing INotifyPropertyChanging as well?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

K Viltersten - 25 Feb 2008 08:15 GMT
>> I don't get anything autogenerated when it comes to
>> LINQ. For some reason (described in another post) i
[quoted text clipped - 3 lines]
> to fix that. Doing it all manually is going to be a
> pain...

While i believe that doing it the painful way at least
once is a good experience, i do agree that not having
the autogeneration option is a serious obstacle.

The problem is that i have little clue how to "fix" it
since my knowledge within LINQ is rather limited.

> PropertyChanged should end up being subscribed to by
> the DataContext code, but I can't see why that's not
> happening :(

How exactly is the subscription supposed to be done?
(I have no issues typing in the code if i have to.)

> Have you tried implementing INotifyPropertyChanging
> as well?

Yes. The result was pretty much the same, sadly.

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 25 Feb 2008 08:22 GMT
> > Hmm... I think your time would be best spent trying
> > to fix that. Doing it all manually is going to be a
[quoted text clipped - 6 lines]
> The problem is that i have little clue how to "fix" it
> since my knowledge within LINQ is rather limited.

Sure. I'll see if I can find your other posts - I may well not be able
to help, but it's worth a look.

> > PropertyChanged should end up being subscribed to by
> > the DataContext code, but I can't see why that's not
> > happening :(
>
> How exactly is the subscription supposed to be done?
> (I have no issues typing in the code if i have to.)

I'd expect the DataContext code which loads the objects to subscribe to
the events.

> > Have you tried implementing INotifyPropertyChanging
> > as well?
>
> Yes. The result was pretty much the same, sadly.

:(

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Jon Skeet [C# MVP] - 25 Feb 2008 08:25 GMT
> > Any reason you're using GetTable<T> instead of
> > the autogenerated property on your particular
[quoted text clipped - 3 lines]
> LINQ. For some reason (described in another post) i
> get errors when drag-and-dropping.

<snip>

Could you tell me what that thread was called? I can't immediately see
it.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

K Viltersten - 25 Feb 2008 14:28 GMT
>> > Any reason you're using GetTable<T> instead of
>> > the autogenerated property on your particular
[quoted text clipped - 7 lines]
> Could you tell me what that thread was called? I
> can't immediately see it.

These are the three posts (except this one) that i
addressed LINQ in.

LINQ: problem obtaining data
Connecting to a DB / using LINQ
LINQ to SQL: What is the equivalent of timedate in C#

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 25 Feb 2008 14:33 GMT
> > Could you tell me what that thread was called? I
> > can't immediately see it.
[quoted text clipped - 5 lines]
> Connecting to a DB / using LINQ
> LINQ to SQL: What is the equivalent of timedate in C#

Well, in one of the above threads you mention:

<quote>
As i described in another question, when
i D&D a table into the "drop stuf here
to auto-create code"-area, i get error
that the selected object uses an
unsupported data provider. Nobody
answered that question so i assumed that
it's either stupid or complicated.
</quote>

However, I can't find the thread that *that* post references.

In my experience it's been as simple as:

1) Create database connection in Server Explorer.
2) Create new LINQ to SQL classes
3) Drag and drop tables from 1 into 2.

Now, given your error above, which data provider *are* you using? It
could be that you can fix your problem simply by changing data
provider.

Jon
K Viltersten - 25 Feb 2008 16:28 GMT
>>> Could you tell me what that thread was called? I
>>> can't immediately see it.
[quoted text clipped - 20 lines]
> However, I can't find the thread that
> *that* post references.

Ah, sorry, my bad. "LINQ: problem obtaining
data" is the name nme of the post.

Mr. Nicholas Paldino expressed:
"Did you not try the designer in VS.NET 2008
...With the designer, it is really becomes a
drag-and-drop operation."

to which i responded by:
"I believe that i have the designer but when i
drag and drop my table into the area, i get
the error message telling me that the selected
object uses an unsupported data provider.
What i did was to create a new LINQ to SQL
object and try to drag-and-drop the table of
mine onto the empty region in the middle."

> In my experience it's been as simple as:
> 1) Create database connection in Server Explorer.
> 2) Create new LINQ to SQL classes.
> 3) Drag and drop tables from 1 into 2.

I'm not as fortunate as you are. For me, the
step #3 doesn't work. I'm somewhat of an
condensator for stupid issues, i guess.   :)

> Now, given your error above, which data provider
> *are* you using? It could be that you can fix
> your problem simply by changing data provider.

Now, please put it as if you're addressing a
retarded banana, please. What is regarded as a
data provider? I'm using an SDF file stored
locally on my computer. Is that what you ment?

Where/how do i change the said data provider?

Please, don't underestimate my skill in confusing
myself regarded a new concept. You'd be surprised
how effective i can be in that area
(unintentionally, of course).   :)

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 25 Feb 2008 16:28 GMT
<snip>

> > Now, given your error above, which data provider
> > *are* you using? It could be that you can fix
[quoted text clipped - 11 lines]
> how effective i can be in that area
> (unintentionally, of course).   :)

Okay - do you have a Server Explorer window in VS 2008? Do you have a
connection in there? If so, it should list a data provider.
Alternatively, create a new connection and see what that comes up
with.

Try a data source with "Microsoft SQL Server Database File
(SqlClient)". That certainly works for me.

Jon
K Viltersten - 25 Feb 2008 18:09 GMT
>>> Now, given your error above, which data provider
>>> *are* you using? It could be that you can fix
[quoted text clipped - 19 lines]
> Database File (SqlClient)". That certainly works
> for me.

I do not. The closest thing i can access from View
is Database Explorer. In there i have a few data
bases that i've created plus the northwnd DB.

However, i disregarded what you said and went on
freestyling and clicking around like a 5-years old
and, what do you know!, suddently i came to a less
apparent settings called "data provider".

Using my superior competence, i somehow knew that a
good choice that certainly would work for me was:
> Try a data source with "Microsoft SQL Server
> Database File (SqlClient)". That certainly works
> for me.

I haven't tested everything yet but this far i do
know that i can D&D those damn tables into that
equally damn drop area.

Thank you very much for the assistance.
I really hope we killed the issue now!
:))

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.