473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem updating dataset to database


The below is some test code to help me learn how to update a dataset.
It is supposed to read the value of UserCounter and write it back.

The UserCounter is being read correctly as 0, is 1 when written back, but is
not being written back to the database.

Although I've used a foreach loop there is actually only 1 record in the
dataset.

I'm going to eventually use transactions but have commented them out for now
to test this problem.

I've tried using SqlCommandBuilder to build the Update and also manually.

The manual sql is commented out right now and does not work.

On the manual SQL I am unsure how to handle a field name and the @ when the
field name has a space in it.

int UserCounter;

SqlCommand commandUpdate = new SqlCommand ();

string sqlUsers;

string sqlUsersUpdate;

sqlUsers = string.Format ("SELECT [PO Next] FROM [Users] WHERE [User ID] =
'{0}'",
UserIDOfRecord);

sqlUsersUpdate = string.Format ("UPDATE [Users] SET [PO Next] = [@PO Next]
WHERE [User ID] = [@User ID]",
UserIDOfRecord);

conn_counts.Open ();

//tran transactionCount = conn_counts.BeginTransaction ();

DataSet dsUsers = new DataSet ();

SqlDataAdapter daUsers = new SqlDataAdapter ();

UserCounter = -1;

daUsers.SelectCommand = new SqlCommand ();

daUsers.SelectCommand.CommandText = sqlUsers;

daUsers.SelectCommand.Connection = conn_counts;

//tran daUsers.SelectCommand.Transaction = transactionCount;

SqlCommandBuilder custCB = new SqlCommandBuilder (daUsers);

/*

daUsers.UpdateCommand = new SqlCommand ();

daUsers.UpdateCommand.CommandText = sqlUsersUpdate;

daUsers.UpdateCommand.Connection = conn_counts;

daUsers.UpdateCommand.Transaction = transactionCount;

*/

daUsers.Fill (dsUsers, "Users");

foreach (DataRow row in dsUsers.Tables ["Users"].Rows)
UserCounter = (int) row [0];

foreach (DataRow row in dsUsers.Tables ["Users"].Rows)
row [0] = UserCounter;

dsUsers.AcceptChanges ();

daUsers.Update (dsUsers, "Users");

//tran transactionCount.Commit ();

================================================== =
Zachary Hilbun
Software Consultant http://www.vianova.com
Via Nova OpenGL, Open Inventor 3D products, Windows 95/NT
Dallas, Texas VC++, C#, embedded systems, VxD's
1977 SSII SRX31185
Glock 30
Nov 16 '05 #1
7 1837
You are calling AcceptChanges right before update - this will ensure that
your update will Never get written back to the db b/c all of the rowstates
are reset. Take that out and you should be ok. However why are you using a
CommandBuilder and specifying the update logic. The one thing
CommandBuilders do for you is build the Crud logic based on Select
statements?

I'd run through the Configuration wizard and just call update - Also, you
may want to check out Bill Vaughn's Article on Weaning Developers from the
CommandBUilder at www.betav.com -> Articles -> MSDN - I found it very
helpful when I was learning ADO.NET.

BTW, doesn't look like your connection is getting closed and even if that
line is left out - if you don't wrap it in a using Block or a
try/catch/finally - you run the risk of not having it closed. That can cause
some real drama for you.

The biggest problem w/ the update though is the AcceptChanges right before
you call Update.

HTH,

Bill

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Zachary Hilbun" <gu****@vianova.com> wrote in message
news:Ka*****************@newssvr11.news.prodigy.co m...

The below is some test code to help me learn how to update a dataset.
It is supposed to read the value of UserCounter and write it back.

The UserCounter is being read correctly as 0, is 1 when written back, but is not being written back to the database.

Although I've used a foreach loop there is actually only 1 record in the
dataset.

I'm going to eventually use transactions but have commented them out for now to test this problem.

I've tried using SqlCommandBuilder to build the Update and also manually.

The manual sql is commented out right now and does not work.

On the manual SQL I am unsure how to handle a field name and the @ when the field name has a space in it.

int UserCounter;

SqlCommand commandUpdate = new SqlCommand ();

string sqlUsers;

string sqlUsersUpdate;

sqlUsers = string.Format ("SELECT [PO Next] FROM [Users] WHERE [User ID] =
'{0}'",
UserIDOfRecord);

sqlUsersUpdate = string.Format ("UPDATE [Users] SET [PO Next] = [@PO Next]
WHERE [User ID] = [@User ID]",
UserIDOfRecord);

conn_counts.Open ();

//tran transactionCount = conn_counts.BeginTransaction ();

DataSet dsUsers = new DataSet ();

SqlDataAdapter daUsers = new SqlDataAdapter ();

UserCounter = -1;

daUsers.SelectCommand = new SqlCommand ();

daUsers.SelectCommand.CommandText = sqlUsers;

daUsers.SelectCommand.Connection = conn_counts;

//tran daUsers.SelectCommand.Transaction = transactionCount;

SqlCommandBuilder custCB = new SqlCommandBuilder (daUsers);

/*

daUsers.UpdateCommand = new SqlCommand ();

daUsers.UpdateCommand.CommandText = sqlUsersUpdate;

daUsers.UpdateCommand.Connection = conn_counts;

daUsers.UpdateCommand.Transaction = transactionCount;

*/

daUsers.Fill (dsUsers, "Users");

foreach (DataRow row in dsUsers.Tables ["Users"].Rows)
UserCounter = (int) row [0];

foreach (DataRow row in dsUsers.Tables ["Users"].Rows)
row [0] = UserCounter;

dsUsers.AcceptChanges ();

daUsers.Update (dsUsers, "Users");

//tran transactionCount.Commit ();

================================================== =
Zachary Hilbun
Software Consultant http://www.vianova.com
Via Nova OpenGL, Open Inventor 3D products, Windows 95/NT
Dallas, Texas VC++, C#, embedded systems, VxD's
1977 SSII SRX31185
Glock 30

Nov 16 '05 #2
I removed the AcceptChanges and it now appears to be trying to update the
datbase. SqlCommandBuilder is incorrectly generating the SQL. Some of the
field names have a space in them and SqlCommandBuilder is not putting []
around them when it uses them.
When I first wrote this test code I was building the update SQL by hand but
couldn't figure out how to refer to these fields, either as [@User ID] or
@[User ID].
I didn't show my connection close or the try/catch statments.
================================================== =
Zachary Hilbun
Software Consultant http://www.vianova.com
Via Nova OpenGL, Open Inventor 3D products, Windows 95/NT
Dallas, Texas VC++, C#, embedded systems, VxD's
1977 SSII SRX31185
Glock 30
"W.G. Ryan eMVP" <Wi*********@gmail.com> wrote in message
news:O7**************@TK2MSFTNGP12.phx.gbl...
You are calling AcceptChanges right before update - this will ensure that
your update will Never get written back to the db b/c all of the rowstates
are reset. Take that out and you should be ok. However why are you using
a
CommandBuilder and specifying the update logic. The one thing
CommandBuilders do for you is build the Crud logic based on Select
statements?

I'd run through the Configuration wizard and just call update - Also,
you
may want to check out Bill Vaughn's Article on Weaning Developers from the
CommandBUilder at www.betav.com -> Articles -> MSDN - I found it very
helpful when I was learning ADO.NET.

BTW, doesn't look like your connection is getting closed and even if that
line is left out - if you don't wrap it in a using Block or a
try/catch/finally - you run the risk of not having it closed. That can
cause
some real drama for you.

The biggest problem w/ the update though is the AcceptChanges right before
you call Update.

HTH,

Bill

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Zachary Hilbun" <gu****@vianova.com> wrote in message
news:Ka*****************@newssvr11.news.prodigy.co m...

The below is some test code to help me learn how to update a dataset.
It is supposed to read the value of UserCounter and write it back.

The UserCounter is being read correctly as 0, is 1 when written back, but

is
not being written back to the database.

Although I've used a foreach loop there is actually only 1 record in the
dataset.

I'm going to eventually use transactions but have commented them out for

now
to test this problem.

I've tried using SqlCommandBuilder to build the Update and also manually.

The manual sql is commented out right now and does not work.

On the manual SQL I am unsure how to handle a field name and the @ when

the
field name has a space in it.

int UserCounter;

SqlCommand commandUpdate = new SqlCommand ();

string sqlUsers;

string sqlUsersUpdate;

sqlUsers = string.Format ("SELECT [PO Next] FROM [Users] WHERE [User ID]
=
'{0}'",
UserIDOfRecord);

sqlUsersUpdate = string.Format ("UPDATE [Users] SET [PO Next] = [@PO
Next]
WHERE [User ID] = [@User ID]",
UserIDOfRecord);

conn_counts.Open ();

//tran transactionCount = conn_counts.BeginTransaction ();

DataSet dsUsers = new DataSet ();

SqlDataAdapter daUsers = new SqlDataAdapter ();

UserCounter = -1;

daUsers.SelectCommand = new SqlCommand ();

daUsers.SelectCommand.CommandText = sqlUsers;

daUsers.SelectCommand.Connection = conn_counts;

//tran daUsers.SelectCommand.Transaction = transactionCount;

SqlCommandBuilder custCB = new SqlCommandBuilder (daUsers);

/*

daUsers.UpdateCommand = new SqlCommand ();

daUsers.UpdateCommand.CommandText = sqlUsersUpdate;

daUsers.UpdateCommand.Connection = conn_counts;

daUsers.UpdateCommand.Transaction = transactionCount;

*/

daUsers.Fill (dsUsers, "Users");

foreach (DataRow row in dsUsers.Tables ["Users"].Rows)
UserCounter = (int) row [0];

foreach (DataRow row in dsUsers.Tables ["Users"].Rows)
row [0] = UserCounter;

dsUsers.AcceptChanges ();

daUsers.Update (dsUsers, "Users");

//tran transactionCount.Commit ();

================================================== =
Zachary Hilbun
Software Consultant http://www.vianova.com
Via Nova OpenGL, Open Inventor 3D products, Windows 95/NT
Dallas, Texas VC++, C#, embedded systems, VxD's
1977 SSII SRX31185
Glock 30


Nov 16 '05 #3
Have you set the QuotePrefix and QuoteSuffix properties? That should
address the space problem you mention - although it's probably worth it to
bite the bullet and get rid of the names with spaces in them - even if you
always remember to address this issue - another newer developer will
probably forget - lot's of downside for little upside.

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Zachary Hilbun" <gu****@vianova.com> wrote in message
news:2A*****************@newssvr11.news.prodigy.co m...
I removed the AcceptChanges and it now appears to be trying to update the
datbase. SqlCommandBuilder is incorrectly generating the SQL. Some of the field names have a space in them and SqlCommandBuilder is not putting []
around them when it uses them.
When I first wrote this test code I was building the update SQL by hand but couldn't figure out how to refer to these fields, either as [@User ID] or
@[User ID].
I didn't show my connection close or the try/catch statments.
================================================== =
Zachary Hilbun
Software Consultant http://www.vianova.com
Via Nova OpenGL, Open Inventor 3D products, Windows 95/NT
Dallas, Texas VC++, C#, embedded systems, VxD's
1977 SSII SRX31185
Glock 30
"W.G. Ryan eMVP" <Wi*********@gmail.com> wrote in message
news:O7**************@TK2MSFTNGP12.phx.gbl...
You are calling AcceptChanges right before update - this will ensure that your update will Never get written back to the db b/c all of the rowstates are reset. Take that out and you should be ok. However why are you using a
CommandBuilder and specifying the update logic. The one thing
CommandBuilders do for you is build the Crud logic based on Select
statements?

I'd run through the Configuration wizard and just call update - Also,
you
may want to check out Bill Vaughn's Article on Weaning Developers from the CommandBUilder at www.betav.com -> Articles -> MSDN - I found it very
helpful when I was learning ADO.NET.

BTW, doesn't look like your connection is getting closed and even if that line is left out - if you don't wrap it in a using Block or a
try/catch/finally - you run the risk of not having it closed. That can
cause
some real drama for you.

The biggest problem w/ the update though is the AcceptChanges right before you call Update.

HTH,

Bill

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Zachary Hilbun" <gu****@vianova.com> wrote in message
news:Ka*****************@newssvr11.news.prodigy.co m...

The below is some test code to help me learn how to update a dataset.
It is supposed to read the value of UserCounter and write it back.

The UserCounter is being read correctly as 0, is 1 when written back, but
is
not being written back to the database.

Although I've used a foreach loop there is actually only 1 record in
the dataset.

I'm going to eventually use transactions but have commented them out for now
to test this problem.

I've tried using SqlCommandBuilder to build the Update and also

manually.
The manual sql is commented out right now and does not work.

On the manual SQL I am unsure how to handle a field name and the @ when

the
field name has a space in it.

int UserCounter;

SqlCommand commandUpdate = new SqlCommand ();

string sqlUsers;

string sqlUsersUpdate;

sqlUsers = string.Format ("SELECT [PO Next] FROM [Users] WHERE [User ID] =
'{0}'",
UserIDOfRecord);

sqlUsersUpdate = string.Format ("UPDATE [Users] SET [PO Next] = [@PO
Next]
WHERE [User ID] = [@User ID]",
UserIDOfRecord);

conn_counts.Open ();

//tran transactionCount = conn_counts.BeginTransaction ();

DataSet dsUsers = new DataSet ();

SqlDataAdapter daUsers = new SqlDataAdapter ();

UserCounter = -1;

daUsers.SelectCommand = new SqlCommand ();

daUsers.SelectCommand.CommandText = sqlUsers;

daUsers.SelectCommand.Connection = conn_counts;

//tran daUsers.SelectCommand.Transaction = transactionCount;

SqlCommandBuilder custCB = new SqlCommandBuilder (daUsers);

/*

daUsers.UpdateCommand = new SqlCommand ();

daUsers.UpdateCommand.CommandText = sqlUsersUpdate;

daUsers.UpdateCommand.Connection = conn_counts;

daUsers.UpdateCommand.Transaction = transactionCount;

*/

daUsers.Fill (dsUsers, "Users");

foreach (DataRow row in dsUsers.Tables ["Users"].Rows)
UserCounter = (int) row [0];

foreach (DataRow row in dsUsers.Tables ["Users"].Rows)
row [0] = UserCounter;

dsUsers.AcceptChanges ();

daUsers.Update (dsUsers, "Users");

//tran transactionCount.Commit ();

================================================== =
Zachary Hilbun
Software Consultant http://www.vianova.com
Via Nova OpenGL, Open Inventor 3D products, Windows 95/NT
Dallas, Texas VC++, C#, embedded systems, VxD's
1977 SSII SRX31185
Glock 30



Nov 16 '05 #4
Adding QuotePrefix and QuoteSuffix fixed it. If I want to build the Update
SQL manually do I use [@User ID] or @[User ID]? Also, do I have to specify
Insert and Delete SQL if I manually specify SQL for Update and don't use
SqlCommandBuilder?

Thanks for the help on this. At least I have something I know works.
================================================== =
Zachary Hilbun
Software Consultant http://www.vianova.com
Via Nova OpenGL, Open Inventor 3D products, Windows 95/NT
Dallas, Texas VC++, C#, embedded systems, VxD's
1977 SSII SRX31185
Glock 30

"W.G. Ryan eMVP" <Wi*********@gmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Have you set the QuotePrefix and QuoteSuffix properties? That should
address the space problem you mention - although it's probably worth it to
bite the bullet and get rid of the names with spaces in them - even if you
always remember to address this issue - another newer developer will
probably forget - lot's of downside for little upside.

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Zachary Hilbun" <gu****@vianova.com> wrote in message
news:2A*****************@newssvr11.news.prodigy.co m...
I removed the AcceptChanges and it now appears to be trying to update the
datbase. SqlCommandBuilder is incorrectly generating the SQL. Some of

the
field names have a space in them and SqlCommandBuilder is not putting []
around them when it uses them.
When I first wrote this test code I was building the update SQL by hand

but
couldn't figure out how to refer to these fields, either as [@User ID] or
@[User ID].
I didn't show my connection close or the try/catch statments.
================================================== =
Zachary Hilbun
Software Consultant http://www.vianova.com
Via Nova OpenGL, Open Inventor 3D products, Windows 95/NT
Dallas, Texas VC++, C#, embedded systems, VxD's
1977 SSII SRX31185
Glock 30
"W.G. Ryan eMVP" <Wi*********@gmail.com> wrote in message
news:O7**************@TK2MSFTNGP12.phx.gbl...
> You are calling AcceptChanges right before update - this will ensure that > your update will Never get written back to the db b/c all of the rowstates > are reset. Take that out and you should be ok. However why are you using > a
> CommandBuilder and specifying the update logic. The one thing
> CommandBuilders do for you is build the Crud logic based on Select
> statements?
>
> I'd run through the Configuration wizard and just call update -
> Also,
> you
> may want to check out Bill Vaughn's Article on Weaning Developers from the > CommandBUilder at www.betav.com -> Articles -> MSDN - I found it very
> helpful when I was learning ADO.NET.
>
> BTW, doesn't look like your connection is getting closed and even if that > line is left out - if you don't wrap it in a using Block or a
> try/catch/finally - you run the risk of not having it closed. That can
> cause
> some real drama for you.
>
> The biggest problem w/ the update though is the AcceptChanges right before > you call Update.
>
> HTH,
>
> Bill
>
> --
> W.G. Ryan, MVP
>
> www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
> "Zachary Hilbun" <gu****@vianova.com> wrote in message
> news:Ka*****************@newssvr11.news.prodigy.co m...
>>
>> The below is some test code to help me learn how to update a dataset.
>> It is supposed to read the value of UserCounter and write it back.
>>
>> The UserCounter is being read correctly as 0, is 1 when written back, but > is
>> not being written back to the database.
>>
>> Although I've used a foreach loop there is actually only 1 record in the >> dataset.
>>
>> I'm going to eventually use transactions but have commented them out for > now
>> to test this problem.
>>
>> I've tried using SqlCommandBuilder to build the Update and also manually. >>
>> The manual sql is commented out right now and does not work.
>>
>> On the manual SQL I am unsure how to handle a field name and the @
>> when
> the
>> field name has a space in it.
>>
>>
>>
>> int UserCounter;
>>
>> SqlCommand commandUpdate = new SqlCommand ();
>>
>> string sqlUsers;
>>
>> string sqlUsersUpdate;
>>
>> sqlUsers = string.Format ("SELECT [PO Next] FROM [Users] WHERE [User ID] >> =
>> '{0}'",
>> UserIDOfRecord);
>>
>> sqlUsersUpdate = string.Format ("UPDATE [Users] SET [PO Next] = [@PO
>> Next]
>> WHERE [User ID] = [@User ID]",
>> UserIDOfRecord);
>>
>> conn_counts.Open ();
>>
>> //tran transactionCount = conn_counts.BeginTransaction ();
>>
>> DataSet dsUsers = new DataSet ();
>>
>> SqlDataAdapter daUsers = new SqlDataAdapter ();
>>
>> UserCounter = -1;
>>
>> daUsers.SelectCommand = new SqlCommand ();
>>
>> daUsers.SelectCommand.CommandText = sqlUsers;
>>
>> daUsers.SelectCommand.Connection = conn_counts;
>>
>> //tran daUsers.SelectCommand.Transaction = transactionCount;
>>
>>
>>
>> SqlCommandBuilder custCB = new SqlCommandBuilder (daUsers);
>>
>> /*
>>
>> daUsers.UpdateCommand = new SqlCommand ();
>>
>> daUsers.UpdateCommand.CommandText = sqlUsersUpdate;
>>
>> daUsers.UpdateCommand.Connection = conn_counts;
>>
>> daUsers.UpdateCommand.Transaction = transactionCount;
>>
>> */
>>
>> daUsers.Fill (dsUsers, "Users");
>>
>> foreach (DataRow row in dsUsers.Tables ["Users"].Rows)
>> UserCounter = (int) row [0];
>>
>> foreach (DataRow row in dsUsers.Tables ["Users"].Rows)
>> row [0] = UserCounter;
>>
>> dsUsers.AcceptChanges ();
>>
>> daUsers.Update (dsUsers, "Users");
>>
>> //tran transactionCount.Commit ();
>>
>>
>>
>> ================================================== =
>> Zachary Hilbun
>> Software Consultant http://www.vianova.com
>> Via Nova OpenGL, Open Inventor 3D products, Windows 95/NT
>> Dallas, Texas VC++, C#, embedded systems, VxD's
>> 1977 SSII SRX31185
>> Glock 30
>>
>>
>
>



Nov 16 '05 #5


--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Zachary Hilbun" <gu****@vianova.com> wrote in message
news:ED*****************@newssvr11.news.prodigy.co m...
Adding QuotePrefix and QuoteSuffix fixed it. --Good deal
If I want to build the Update SQL manually do I use [@User ID] or @[User ID]? --You don't need to use [] around parameter names.
Also, do I have to specify Insert and Delete SQL if I manually specify SQL for Update and don't use
SqlCommandBuilder? --You'll need to specify a command for each type of action that's taken.
Let's say on a particular screen that you don't allow Deletes. then you
wouldn't need a delete command. When you call Update on the Adapter, it
loops through each row on the table and checks rowstate- then it maps the
rowstate to the respective command (Modified will map to the Update command,
Added will map to the insert command and Deleted will map to the delete
command).

HTH,

Bill
Thanks for the help on this. At least I have something I know works.
================================================== =
Zachary Hilbun
Software Consultant http://www.vianova.com
Via Nova OpenGL, Open Inventor 3D products, Windows 95/NT
Dallas, Texas VC++, C#, embedded systems, VxD's
1977 SSII SRX31185
Glock 30

"W.G. Ryan eMVP" <Wi*********@gmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Have you set the QuotePrefix and QuoteSuffix properties? That should
address the space problem you mention - although it's probably worth it to bite the bullet and get rid of the names with spaces in them - even if you always remember to address this issue - another newer developer will
probably forget - lot's of downside for little upside.

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Zachary Hilbun" <gu****@vianova.com> wrote in message
news:2A*****************@newssvr11.news.prodigy.co m...
I removed the AcceptChanges and it now appears to be trying to update the datbase. SqlCommandBuilder is incorrectly generating the SQL. Some of

the
field names have a space in them and SqlCommandBuilder is not putting [] around them when it uses them.
When I first wrote this test code I was building the update SQL by hand

but
couldn't figure out how to refer to these fields, either as [@User ID] or @[User ID].
I didn't show my connection close or the try/catch statments.
================================================== =
Zachary Hilbun
Software Consultant http://www.vianova.com
Via Nova OpenGL, Open Inventor 3D products, Windows 95/NT
Dallas, Texas VC++, C#, embedded systems, VxD's
1977 SSII SRX31185
Glock 30
"W.G. Ryan eMVP" <Wi*********@gmail.com> wrote in message
news:O7**************@TK2MSFTNGP12.phx.gbl...
> You are calling AcceptChanges right before update - this will ensure

that
> your update will Never get written back to the db b/c all of the

rowstates
> are reset. Take that out and you should be ok. However why are you

using
> a
> CommandBuilder and specifying the update logic. The one thing
> CommandBuilders do for you is build the Crud logic based on Select
> statements?
>
> I'd run through the Configuration wizard and just call update -
> Also,
> you
> may want to check out Bill Vaughn's Article on Weaning Developers from
the
> CommandBUilder at www.betav.com -> Articles -> MSDN - I found it very
> helpful when I was learning ADO.NET.
>
> BTW, doesn't look like your connection is getting closed and even if

that
> line is left out - if you don't wrap it in a using Block or a
> try/catch/finally - you run the risk of not having it closed. That
can > cause
> some real drama for you.
>
> The biggest problem w/ the update though is the AcceptChanges right

before
> you call Update.
>
> HTH,
>
> Bill
>
> --
> W.G. Ryan, MVP
>
> www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
> "Zachary Hilbun" <gu****@vianova.com> wrote in message
> news:Ka*****************@newssvr11.news.prodigy.co m...
>>
>> The below is some test code to help me learn how to update a dataset. >> It is supposed to read the value of UserCounter and write it back.
>>
>> The UserCounter is being read correctly as 0, is 1 when written

back, but
> is
>> not being written back to the database.
>>
>> Although I've used a foreach loop there is actually only 1 record in

the
>> dataset.
>>
>> I'm going to eventually use transactions but have commented them out

for
> now
>> to test this problem.
>>
>> I've tried using SqlCommandBuilder to build the Update and also

manually.
>>
>> The manual sql is commented out right now and does not work.
>>
>> On the manual SQL I am unsure how to handle a field name and the @
>> when
> the
>> field name has a space in it.
>>
>>
>>
>> int UserCounter;
>>
>> SqlCommand commandUpdate = new SqlCommand ();
>>
>> string sqlUsers;
>>
>> string sqlUsersUpdate;
>>
>> sqlUsers = string.Format ("SELECT [PO Next] FROM [Users] WHERE [User

ID]
>> =
>> '{0}'",
>> UserIDOfRecord);
>>
>> sqlUsersUpdate = string.Format ("UPDATE [Users] SET [PO Next] = [@PO
>> Next]
>> WHERE [User ID] = [@User ID]",
>> UserIDOfRecord);
>>
>> conn_counts.Open ();
>>
>> //tran transactionCount = conn_counts.BeginTransaction ();
>>
>> DataSet dsUsers = new DataSet ();
>>
>> SqlDataAdapter daUsers = new SqlDataAdapter ();
>>
>> UserCounter = -1;
>>
>> daUsers.SelectCommand = new SqlCommand ();
>>
>> daUsers.SelectCommand.CommandText = sqlUsers;
>>
>> daUsers.SelectCommand.Connection = conn_counts;
>>
>> //tran daUsers.SelectCommand.Transaction = transactionCount;
>>
>>
>>
>> SqlCommandBuilder custCB = new SqlCommandBuilder (daUsers);
>>
>> /*
>>
>> daUsers.UpdateCommand = new SqlCommand ();
>>
>> daUsers.UpdateCommand.CommandText = sqlUsersUpdate;
>>
>> daUsers.UpdateCommand.Connection = conn_counts;
>>
>> daUsers.UpdateCommand.Transaction = transactionCount;
>>
>> */
>>
>> daUsers.Fill (dsUsers, "Users");
>>
>> foreach (DataRow row in dsUsers.Tables ["Users"].Rows)
>> UserCounter = (int) row [0];
>>
>> foreach (DataRow row in dsUsers.Tables ["Users"].Rows)
>> row [0] = UserCounter;
>>
>> dsUsers.AcceptChanges ();
>>
>> daUsers.Update (dsUsers, "Users");
>>
>> //tran transactionCount.Commit ();
>>
>>
>>
>> ================================================== =
>> Zachary Hilbun
>> Software Consultant http://www.vianova.com
>> Via Nova OpenGL, Open Inventor 3D products, Windows 95/NT
>> Dallas, Texas VC++, C#, embedded systems, VxD's
>> 1977 SSII SRX31185
>> Glock 30
>>
>>
>
>



Nov 16 '05 #6
Bill,

I have never seen anybody complaining in the dotNet newsgroups about the
commandbuilder with a simple select except Bill and you (I remember me one
time when somebody used more than 100 items in a datarow, what is as well
not a pretty thing to do by hand and I know about a joining table).

I have followed you both often in my messages, however are there serious
complaints about its use in a simple single not joined use?

Cor
Nov 16 '05 #7
There's a lot wrong with CommandBuilders but my biggest gripes are that they
are performance pigs, you have 0 control over concurrency and you can't use
procedures. So today you might be fine using a CommandBuilder- but if the
concurrency scenario changes - you are totally screwed and Must write new
code to deal with it - and a LOT of code

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Cor Ligthert" <no************@planet.nl> wrote in message
news:eX**************@tk2msftngp13.phx.gbl...
Bill,

I have never seen anybody complaining in the dotNet newsgroups about the
commandbuilder with a simple select except Bill and you (I remember me one
time when somebody used more than 100 items in a datarow, what is as well
not a pretty thing to do by hand and I know about a joining table).

I have followed you both often in my messages, however are there serious
complaints about its use in a simple single not joined use?

Cor

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Frnak McKenney | last post by:
I'm using an in-core DataSet as an image of my application's 'database' (a multi-table Access97 mdb file). Updates are made to the DataTables within the DataSet via forms with bound TextBoxes,...
1
by: Steven Blair | last post by:
Hi, Here is a short decsription of my problem. I have written a dll for Database accessing. I have one method which can return a Dataset and another method which takes a Dataset and upates a...
3
by: Steven Blair | last post by:
Hi, I have a problem understanding the SqlDataAdaptor and the DataSet. I have a dll which has various methods for running stored procedures etc. It also has a method which allows the client to...
5
by: junglist | last post by:
Hi guys, I've been trying to implement an editable datagrid and i have been succesful up to the point where i can update my datagrid row by row. However what used to happen was that once i updated...
14
by: Lars Netzel | last post by:
A little background: I use three Datagrids that are in a child parent relation. I Use Negative Autoincrement on the the DataTables and that's workning nice. My problem is when I Update these...
1
by: r2destini | last post by:
Hi Friends, I am new to .Net. So I don't know much. I am facing a problem in updating database through ADO.Net I am creating the dataset and there is no problem in the updation and...
15
by: Scotty | last post by:
I like to have a good insert, update and delete code The code below sometimes workl ok sometimes doesnt work, what i am doing wrong?? Sub SaveAny() Dim command_builder As New...
0
by: Johnny | last post by:
I have a PocketPC mobile application that gets its data from the Sql Server database via a web service. The web service returns a dataset that I need to load into the SqlCe database on the mobile...
5
by: Brad Baker | last post by:
I'm trying to write a simple asp.net page which updates some data in a SQL database. At the top of the page I have the following code: <%@ Page Language="C#" Debug="true" %> <%@ import...
3
by: sachinkale123 | last post by:
I am using SQL Adapter to select data from a table in SQL Server Database. After creating Object I am filling that to a datatable. Assum that dataset has 5 row. After that I m updating that dataset...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.