Groups | Blog | Home
all groups > c# > february 2008 >

c# : LINQ - problems updating entries in DB


Jon Skeet [C# MVP]
2/24/2008 7:06:43 PM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
K Viltersten
2/24/2008 8:01:22 PM
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]
2/24/2008 8:05:37 PM
[quoted text, click to view]

<snip>

[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
K Viltersten
2/24/2008 8:58:13 PM
[quoted text, click to view]

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]
2/24/2008 10:26:54 PM
[quoted text, click to view]

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

[quoted text, click to view]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Jon Skeet [C# MVP]
2/24/2008 10:31:06 PM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
K Viltersten
2/24/2008 11:09:38 PM
[quoted text, click to view]

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
K Viltersten
2/24/2008 11:37:35 PM
[quoted text, click to view]

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
K Viltersten
2/25/2008 1:33:01 AM
[quoted text, click to view]

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

[quoted text, click to view]

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]
2/25/2008 6:33:59 AM
[quoted text, click to view]

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 Skeet [C# MVP]
2/25/2008 7:30:52 AM
[quoted text, click to view]

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?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Jon Skeet [C# MVP]
2/25/2008 8:22:24 AM
[quoted text, click to view]

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.

[quoted text, click to view]

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

[quoted text, click to view]

:(

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Jon Skeet [C# MVP]
2/25/2008 8:25:16 AM
[quoted text, click to view]

<snip>

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Jon Skeet [C# MVP]
2/25/2008 8:28:04 AM
[quoted text, click to view]

<snip>

[quoted text, click to view]

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.

K Viltersten
2/25/2008 9:27:34 AM
[quoted text, click to view]

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.

[quoted text, click to view]

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

[quoted text, click to view]

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
K Viltersten
2/25/2008 3:43:54 PM
[quoted text, click to view]

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
K Viltersten
2/25/2008 5:39:58 PM
[quoted text, click to view]

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."



[quoted text, click to view]

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. :)

[quoted text, click to view]

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
K Viltersten
2/25/2008 7:24:15 PM
[quoted text, click to view]

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:
[quoted text, click to view]

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
AddThis Social Bookmark Button