LINQ - problems updating entries in DB

  • Thread starter Thread starter K Viltersten
  • Start date Start date
K

K Viltersten

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.
 
K Viltersten said:
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.

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.
 
I was going to perform an update of an
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.

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?
 
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?

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.
 
Allright, that solved the second issue. Thanks
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.

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?
 
K Viltersten said:
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...

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.
 
K Viltersten said:
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.

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?

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.
 
Allright, that solved the second issue. Thanks
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.

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

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));
}
}
}
 
K Viltersten said:
Yes, i'll try. See at the bottom of this post.


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.

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?
 
K Viltersten said:
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.

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.
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.
Yes. The result was pretty much the same, sadly.

:(
 
K Viltersten said:
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.

<snip>

Could you tell me what that thread was called? I can't immediately see
it.
 
I don't get anything autogenerated when it comes to
Hmm... I think your time would be best spent trying
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.
 
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#

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
 
Any reason you're using GetTable said:
<snip>
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#
 
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). :)

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
 
Could you tell me what that thread was called? I
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.

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). :)
 
Now, given your error above, which data provider
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.

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

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

Similar Threads


Back
Top