473,386 Members | 1,817 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.

why is dataAdapter.UpdateCommand not updating?

Dim da As New SqlDataAdapter("Select * from tbl1", conn)
dim tblx As New DataTable
da.Fill(tblx) '--works OK up to this point

da.UpdateCommand = New SqlCommand
da.UpdateCommand.Connection = conn
da.UpdateCommand.CommandText = "Update tbl1 set fld1 = 'test' where ID = 1"

da.Update(tblx) '--tblx/tbl1 not getting updated here.

But if I do this---then it works OK (not using dataAdapter though)

Dim cmd As New SqlCommand
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "Update tbl1 set fld1 = 'test' where ID = 1"
conn.Open
cmd.ExecuteNonQuery
cmd.Close

This does update tbl1 correctly. But I need to use the dataAdapter. What
do I need to do to the dataAdapter.UpdateCommand code to make that update
correctly?

Thanks,
Rich
Jun 5 '06 #1
6 13966
I think I figured this out (because I finally got it to work). What I did
was to assign a datarow object to the row I wanted to update and Parameters
to the UpdateCommand and modified the commandtext to use parameters. Then I
updated the items in the datarow that I wanted to change. Then I updated the
datatable. Now I can see the update in tblx(datatable) tbl1 (sql server
table).
Dim da As New SqlDataAdapter("Select * from tbl1", conn)
dim tblx As New DataTable
da.Fill(tblx)

da.UpdateCommand = New SqlCommand
da.UpdateCommand.Connection = conn
da.UpdateCommand.CommandText = "Update tbl1 set fld1 = @p1 where ID =@p0"

da.UpdateCommand.Parameters.Add(New SqlParameter("@p0, SqlDBtype.Int, 4, "ID")
da.UpdateCommand.Parameters.Add(New SqlParameter("@p1, SqlDBtype.varchar,
10, "fld1")
Dim dr As DataRow = tblx.Rows(0)
dr("fld1") = "test"

da.Update(tblx) '--now I can see the update in tblx/tbl1
the example using cmd was only updating tbl1 on the sql server. But when I
was trying to use the Adapter - I was not updating tblx which is what updates
tbl1 on the sql server. Now I can see the update in the client app as well
as the sql server.
"Rich" wrote:
Dim da As New SqlDataAdapter("Select * from tbl1", conn)
dim tblx As New DataTable
da.Fill(tblx) '--works OK up to this point

da.UpdateCommand = New SqlCommand
da.UpdateCommand.Connection = conn
da.UpdateCommand.CommandText = "Update tbl1 set fld1 = 'test' where ID = 1"

da.Update(tblx) '--tblx/tbl1 not getting updated here.

But if I do this---then it works OK (not using dataAdapter though)

Dim cmd As New SqlCommand
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "Update tbl1 set fld1 = 'test' where ID = 1"
conn.Open
cmd.ExecuteNonQuery
cmd.Close

This does update tbl1 correctly. But I need to use the dataAdapter. What
do I need to do to the dataAdapter.UpdateCommand code to make that update
correctly?

Thanks,
Rich

Jun 5 '06 #2
Rich,

Your update command is incomplete, with a dataadapter you normally test if
there is no problem with the optimistic concurrency. It is done using the
SQL command. Have a look at this most simple sample on our website.

http://www.vb-tips.com/default.aspx?...2-d7c12bbb3726

If you have a simple update as this mostly the commandbuilder makes your
life much easier.

All the insert, update and delete handling for a table are made dynamicly
while using the dataadapter after doing this command.

dim cmb as new SQLCommandBuilder(da)

I hope this helps,

Cor

"Rich" <Ri**@discussions.microsoft.com> schreef in bericht
news:31**********************************@microsof t.com...
I think I figured this out (because I finally got it to work). What I did
was to assign a datarow object to the row I wanted to update and
Parameters
to the UpdateCommand and modified the commandtext to use parameters. Then
I
updated the items in the datarow that I wanted to change. Then I updated
the
datatable. Now I can see the update in tblx(datatable) tbl1 (sql server
table).
Dim da As New SqlDataAdapter("Select * from tbl1", conn)
dim tblx As New DataTable
da.Fill(tblx)

da.UpdateCommand = New SqlCommand
da.UpdateCommand.Connection = conn
da.UpdateCommand.CommandText = "Update tbl1 set fld1 = @p1 where ID =@p0"

da.UpdateCommand.Parameters.Add(New SqlParameter("@p0, SqlDBtype.Int, 4,
"ID")
da.UpdateCommand.Parameters.Add(New SqlParameter("@p1, SqlDBtype.varchar,
10, "fld1")
Dim dr As DataRow = tblx.Rows(0)
dr("fld1") = "test"

da.Update(tblx) '--now I can see the update in tblx/tbl1
the example using cmd was only updating tbl1 on the sql server. But when
I
was trying to use the Adapter - I was not updating tblx which is what
updates
tbl1 on the sql server. Now I can see the update in the client app as
well
as the sql server.
"Rich" wrote:
Dim da As New SqlDataAdapter("Select * from tbl1", conn)
dim tblx As New DataTable
da.Fill(tblx) '--works OK up to this point

da.UpdateCommand = New SqlCommand
da.UpdateCommand.Connection = conn
da.UpdateCommand.CommandText = "Update tbl1 set fld1 = 'test' where ID =
1"

da.Update(tblx) '--tblx/tbl1 not getting updated here.

But if I do this---then it works OK (not using dataAdapter though)

Dim cmd As New SqlCommand
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "Update tbl1 set fld1 = 'test' where ID = 1"
conn.Open
cmd.ExecuteNonQuery
cmd.Close

This does update tbl1 correctly. But I need to use the dataAdapter.
What
do I need to do to the dataAdapter.UpdateCommand code to make that update
correctly?

Thanks,
Rich

Jun 6 '06 #3
Hi Cor,

Thank you for your reply. I looked at your website, but I came up with
another method using dataRow.BeginEdit and dataRow.EndEdit. The
UpdateCommand on my DataAdapter seems to work now.

I have been reading several columns/articles that advise against using the
commandBuilder. One of my goals for using the DataAdpater is specifically
for checking for optimistic concurrency. May I ask how this is done? I
admit that I had a little bit of trouble following the example on your site
(due to my lack of experience).

Thanks,
Rich
"Cor Ligthert [MVP]" wrote:
Rich,

Your update command is incomplete, with a dataadapter you normally test if
there is no problem with the optimistic concurrency. It is done using the
SQL command. Have a look at this most simple sample on our website.

http://www.vb-tips.com/default.aspx?...2-d7c12bbb3726

If you have a simple update as this mostly the commandbuilder makes your
life much easier.

All the insert, update and delete handling for a table are made dynamicly
while using the dataadapter after doing this command.

dim cmb as new SQLCommandBuilder(da)

I hope this helps,

Cor

"Rich" <Ri**@discussions.microsoft.com> schreef in bericht
news:31**********************************@microsof t.com...
I think I figured this out (because I finally got it to work). What I did
was to assign a datarow object to the row I wanted to update and
Parameters
to the UpdateCommand and modified the commandtext to use parameters. Then
I
updated the items in the datarow that I wanted to change. Then I updated
the
datatable. Now I can see the update in tblx(datatable) tbl1 (sql server
table).
Dim da As New SqlDataAdapter("Select * from tbl1", conn)
dim tblx As New DataTable
da.Fill(tblx)

da.UpdateCommand = New SqlCommand
da.UpdateCommand.Connection = conn
da.UpdateCommand.CommandText = "Update tbl1 set fld1 = @p1 where ID =@p0"

da.UpdateCommand.Parameters.Add(New SqlParameter("@p0, SqlDBtype.Int, 4,
"ID")
da.UpdateCommand.Parameters.Add(New SqlParameter("@p1, SqlDBtype.varchar,
10, "fld1")
Dim dr As DataRow = tblx.Rows(0)
dr("fld1") = "test"

da.Update(tblx) '--now I can see the update in tblx/tbl1
the example using cmd was only updating tbl1 on the sql server. But when
I
was trying to use the Adapter - I was not updating tblx which is what
updates
tbl1 on the sql server. Now I can see the update in the client app as
well
as the sql server.
"Rich" wrote:
Dim da As New SqlDataAdapter("Select * from tbl1", conn)
dim tblx As New DataTable
da.Fill(tblx) '--works OK up to this point

da.UpdateCommand = New SqlCommand
da.UpdateCommand.Connection = conn
da.UpdateCommand.CommandText = "Update tbl1 set fld1 = 'test' where ID =
1"

da.Update(tblx) '--tblx/tbl1 not getting updated here.

But if I do this---then it works OK (not using dataAdapter though)

Dim cmd As New SqlCommand
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "Update tbl1 set fld1 = 'test' where ID = 1"
conn.Open
cmd.ExecuteNonQuery
cmd.Close

This does update tbl1 correctly. But I need to use the dataAdapter.
What
do I need to do to the dataAdapter.UpdateCommand code to make that update
correctly?

Thanks,
Rich


Jun 6 '06 #4
Rich,

I have seen those messages about the commandbuilder as well. I have seen
too, that some of those who wrote that in past are now advising the
commandbuilder. In my idea is there nothing wrong with the commandbuilder
for simple updates of one table. Like the designer it can only use one not
joined table with a primary key and hold a maximum of 100 fields. I have
never seen in this newsgroups or by myself a real wrong behaviour of the
commandbuilder. This is not a guarantee of course that there cannot be
something wrong.

But for the code about concurrency checking focus your eyes only on that
update command that is in the sample on our website. It does the same with
the delete and the insert by the way.

'cmdUpdate
cmdUpdate.CommandText = "UPDATE Sample SET WhatEver = @WhatEver
WHERE (AutoId = @Original_AutoId) " & _
"AND (WhatEver = @Original_WhatEver OR @Original_WhatEver IS NULL
AND WhatEver IS NULL); " & _
"SELECT AutoId, WhatEver FROM Sample WHERE (AutoId = @AutoId)"
cmdUpdate.Connection = Conn
cmdUpdate.Parameters.Add(New SqlParameter("@AutoId", SqlDbType.Int,
4, "AutoId"))
cmdUpdate.Parameters.Add(New SqlParameter("@WhatEver",
SqlDbType.NVarChar, 50, "WhatEver"))
cmdUpdate.Parameters.Add(New SqlParameter("@Original_AutoId",
SqlDbType.Int, 4, _
ParameterDirection.Input, False, nb, nb, _
"AutoId", DataRowVersion.Original, Nothing))
cmdUpdate.Parameters.Add(New SqlParameter("@Original_WhatEver", _
SqlDbType.NVarChar, 50, ParameterDirection.Input, False, nb, nb, _
"WhatEver", DataRowVersion.Original, Nothing))
'
What you see is that there is a Update and a Select in it.

The Select reads the database confirm the keys as are in your dataset.
The dataadapter checks if everything is still the same to the original rows
which are still in your dataset before you do acceptchanges or that the
dataadapter does that acceptchanges.
If not than there is a concurrency error.
Simple is it not?

You can see those original rows by writing an dataset with changes to disk
using

ds.writeXML("path", xmlwritemode.diffgram)

I hope to makes it something more clear.

Cor
"Rich" <Ri**@discussions.microsoft.com> schreef in bericht
news:D9**********************************@microsof t.com...
Hi Cor,

Thank you for your reply. I looked at your website, but I came up with
another method using dataRow.BeginEdit and dataRow.EndEdit. The
UpdateCommand on my DataAdapter seems to work now.

I have been reading several columns/articles that advise against using the
commandBuilder. One of my goals for using the DataAdpater is specifically
for checking for optimistic concurrency. May I ask how this is done? I
admit that I had a little bit of trouble following the example on your
site
(due to my lack of experience).

Thanks,
Rich
"Cor Ligthert [MVP]" wrote:
Rich,

Your update command is incomplete, with a dataadapter you normally test
if
there is no problem with the optimistic concurrency. It is done using the
SQL command. Have a look at this most simple sample on our website.

http://www.vb-tips.com/default.aspx?...2-d7c12bbb3726

If you have a simple update as this mostly the commandbuilder makes your
life much easier.

All the insert, update and delete handling for a table are made dynamicly
while using the dataadapter after doing this command.

dim cmb as new SQLCommandBuilder(da)

I hope this helps,

Cor

"Rich" <Ri**@discussions.microsoft.com> schreef in bericht
news:31**********************************@microsof t.com...
>I think I figured this out (because I finally got it to work). What I
>did
> was to assign a datarow object to the row I wanted to update and
> Parameters
> to the UpdateCommand and modified the commandtext to use parameters.
> Then
> I
> updated the items in the datarow that I wanted to change. Then I
> updated
> the
> datatable. Now I can see the update in tblx(datatable) tbl1 (sql
> server
> table).
>
>
> Dim da As New SqlDataAdapter("Select * from tbl1", conn)
> dim tblx As New DataTable
> da.Fill(tblx)
>
> da.UpdateCommand = New SqlCommand
> da.UpdateCommand.Connection = conn
> da.UpdateCommand.CommandText = "Update tbl1 set fld1 = @p1 where ID
> =@p0"
>
> da.UpdateCommand.Parameters.Add(New SqlParameter("@p0, SqlDBtype.Int,
> 4,
> "ID")
> da.UpdateCommand.Parameters.Add(New SqlParameter("@p1,
> SqlDBtype.varchar,
> 10, "fld1")
> Dim dr As DataRow = tblx.Rows(0)
> dr("fld1") = "test"
>
> da.Update(tblx) '--now I can see the update in tblx/tbl1
>
>
> the example using cmd was only updating tbl1 on the sql server. But
> when
> I
> was trying to use the Adapter - I was not updating tblx which is what
> updates
> tbl1 on the sql server. Now I can see the update in the client app as
> well
> as the sql server.
>
>
> "Rich" wrote:
>
>> Dim da As New SqlDataAdapter("Select * from tbl1", conn)
>> dim tblx As New DataTable
>> da.Fill(tblx) '--works OK up to this point
>>
>> da.UpdateCommand = New SqlCommand
>> da.UpdateCommand.Connection = conn
>> da.UpdateCommand.CommandText = "Update tbl1 set fld1 = 'test' where ID
>> =
>> 1"
>>
>> da.Update(tblx) '--tblx/tbl1 not getting updated here.
>>
>> But if I do this---then it works OK (not using dataAdapter though)
>>
>> Dim cmd As New SqlCommand
>> cmd.Connection = conn
>> cmd.CommandType = CommandType.Text
>> cmd.CommandText = "Update tbl1 set fld1 = 'test' where ID = 1"
>> conn.Open
>> cmd.ExecuteNonQuery
>> cmd.Close
>>
>> This does update tbl1 correctly. But I need to use the dataAdapter.
>> What
>> do I need to do to the dataAdapter.UpdateCommand code to make that
>> update
>> correctly?
>>
>> Thanks,
>> Rich


Jun 6 '06 #5
Thank you. This is a little bit more clear. It appears that the concurrency
check comes from the commandtext

cmdUpdate.CommandText = "UPDATE Sample SET WhatEver = @WhatEver
WHERE (AutoId = @Original_AutoId) AND (WhatEver = @Original_WhatEver OR
@Original_WhatEver IS NULL AND WhatEver IS NULL); " & _
"SELECT AutoId, WhatEver FROM Sample WHERE (AutoId = @AutoId)"
I use a Select statement in the InsertCommand in order to retrieve
@@Identity from a sql table. May I ask what the Select statement in the
UpdateCommand performs?
"Cor Ligthert [MVP]" wrote:
Rich,

I have seen those messages about the commandbuilder as well. I have seen
too, that some of those who wrote that in past are now advising the
commandbuilder. In my idea is there nothing wrong with the commandbuilder
for simple updates of one table. Like the designer it can only use one not
joined table with a primary key and hold a maximum of 100 fields. I have
never seen in this newsgroups or by myself a real wrong behaviour of the
commandbuilder. This is not a guarantee of course that there cannot be
something wrong.

But for the code about concurrency checking focus your eyes only on that
update command that is in the sample on our website. It does the same with
the delete and the insert by the way.

'cmdUpdate
cmdUpdate.CommandText = "UPDATE Sample SET WhatEver = @WhatEver
WHERE (AutoId = @Original_AutoId) " & _
"AND (WhatEver = @Original_WhatEver OR @Original_WhatEver IS NULL
AND WhatEver IS NULL); " & _
"SELECT AutoId, WhatEver FROM Sample WHERE (AutoId = @AutoId)"
cmdUpdate.Connection = Conn
cmdUpdate.Parameters.Add(New SqlParameter("@AutoId", SqlDbType.Int,
4, "AutoId"))
cmdUpdate.Parameters.Add(New SqlParameter("@WhatEver",
SqlDbType.NVarChar, 50, "WhatEver"))
cmdUpdate.Parameters.Add(New SqlParameter("@Original_AutoId",
SqlDbType.Int, 4, _
ParameterDirection.Input, False, nb, nb, _
"AutoId", DataRowVersion.Original, Nothing))
cmdUpdate.Parameters.Add(New SqlParameter("@Original_WhatEver", _
SqlDbType.NVarChar, 50, ParameterDirection.Input, False, nb, nb, _
"WhatEver", DataRowVersion.Original, Nothing))
'
What you see is that there is a Update and a Select in it.

The Select reads the database confirm the keys as are in your dataset.
The dataadapter checks if everything is still the same to the original rows
which are still in your dataset before you do acceptchanges or that the
dataadapter does that acceptchanges.
If not than there is a concurrency error.
Simple is it not?

You can see those original rows by writing an dataset with changes to disk
using

ds.writeXML("path", xmlwritemode.diffgram)

I hope to makes it something more clear.

Cor
"Rich" <Ri**@discussions.microsoft.com> schreef in bericht
news:D9**********************************@microsof t.com...
Hi Cor,

Thank you for your reply. I looked at your website, but I came up with
another method using dataRow.BeginEdit and dataRow.EndEdit. The
UpdateCommand on my DataAdapter seems to work now.

I have been reading several columns/articles that advise against using the
commandBuilder. One of my goals for using the DataAdpater is specifically
for checking for optimistic concurrency. May I ask how this is done? I
admit that I had a little bit of trouble following the example on your
site
(due to my lack of experience).

Thanks,
Rich
"Cor Ligthert [MVP]" wrote:
Rich,

Your update command is incomplete, with a dataadapter you normally test
if
there is no problem with the optimistic concurrency. It is done using the
SQL command. Have a look at this most simple sample on our website.

http://www.vb-tips.com/default.aspx?...2-d7c12bbb3726

If you have a simple update as this mostly the commandbuilder makes your
life much easier.

All the insert, update and delete handling for a table are made dynamicly
while using the dataadapter after doing this command.

dim cmb as new SQLCommandBuilder(da)

I hope this helps,

Cor

"Rich" <Ri**@discussions.microsoft.com> schreef in bericht
news:31**********************************@microsof t.com...
>I think I figured this out (because I finally got it to work). What I
>did
> was to assign a datarow object to the row I wanted to update and
> Parameters
> to the UpdateCommand and modified the commandtext to use parameters.
> Then
> I
> updated the items in the datarow that I wanted to change. Then I
> updated
> the
> datatable. Now I can see the update in tblx(datatable) tbl1 (sql
> server
> table).
>
>
> Dim da As New SqlDataAdapter("Select * from tbl1", conn)
> dim tblx As New DataTable
> da.Fill(tblx)
>
> da.UpdateCommand = New SqlCommand
> da.UpdateCommand.Connection = conn
> da.UpdateCommand.CommandText = "Update tbl1 set fld1 = @p1 where ID
> =@p0"
>
> da.UpdateCommand.Parameters.Add(New SqlParameter("@p0, SqlDBtype.Int,
> 4,
> "ID")
> da.UpdateCommand.Parameters.Add(New SqlParameter("@p1,
> SqlDBtype.varchar,
> 10, "fld1")
> Dim dr As DataRow = tblx.Rows(0)
> dr("fld1") = "test"
>
> da.Update(tblx) '--now I can see the update in tblx/tbl1
>
>
> the example using cmd was only updating tbl1 on the sql server. But
> when
> I
> was trying to use the Adapter - I was not updating tblx which is what
> updates
> tbl1 on the sql server. Now I can see the update in the client app as
> well
> as the sql server.
>
>
> "Rich" wrote:
>
>> Dim da As New SqlDataAdapter("Select * from tbl1", conn)
>> dim tblx As New DataTable
>> da.Fill(tblx) '--works OK up to this point
>>
>> da.UpdateCommand = New SqlCommand
>> da.UpdateCommand.Connection = conn
>> da.UpdateCommand.CommandText = "Update tbl1 set fld1 = 'test' where ID
>> =
>> 1"
>>
>> da.Update(tblx) '--tblx/tbl1 not getting updated here.
>>
>> But if I do this---then it works OK (not using dataAdapter though)
>>
>> Dim cmd As New SqlCommand
>> cmd.Connection = conn
>> cmd.CommandType = CommandType.Text
>> cmd.CommandText = "Update tbl1 set fld1 = 'test' where ID = 1"
>> conn.Open
>> cmd.ExecuteNonQuery
>> cmd.Close
>>
>> This does update tbl1 correctly. But I need to use the dataAdapter.
>> What
>> do I need to do to the dataAdapter.UpdateCommand code to make that
>> update
>> correctly?
>>
>> Thanks,
>> Rich


Jun 6 '06 #6
Rich,

In SQLClient with autokey the key is changed in the Dataset, what is not
done by OleDb.

May I ask what the Select statement in the UpdateCommand performs?

Get the values to check for the concurency checking

Cor

"Rich" <Ri**@discussions.microsoft.com> schreef in bericht
news:42**********************************@microsof t.com...
Thank you. This is a little bit more clear. It appears that the
concurrency
check comes from the commandtext

cmdUpdate.CommandText = "UPDATE Sample SET WhatEver = @WhatEver
WHERE (AutoId = @Original_AutoId) AND (WhatEver = @Original_WhatEver OR
@Original_WhatEver IS NULL AND WhatEver IS NULL); " & _
"SELECT AutoId, WhatEver FROM Sample WHERE (AutoId = @AutoId)"
I use a Select statement in the InsertCommand in order to retrieve
@@Identity from a sql table. May I ask what the Select statement in the
UpdateCommand performs?
"Cor Ligthert [MVP]" wrote:
Rich,

I have seen those messages about the commandbuilder as well. I have seen
too, that some of those who wrote that in past are now advising the
commandbuilder. In my idea is there nothing wrong with the commandbuilder
for simple updates of one table. Like the designer it can only use one
not
joined table with a primary key and hold a maximum of 100 fields. I have
never seen in this newsgroups or by myself a real wrong behaviour of the
commandbuilder. This is not a guarantee of course that there cannot be
something wrong.

But for the code about concurrency checking focus your eyes only on that
update command that is in the sample on our website. It does the same
with
the delete and the insert by the way.

'cmdUpdate
cmdUpdate.CommandText = "UPDATE Sample SET WhatEver = @WhatEver
WHERE (AutoId = @Original_AutoId) " & _
"AND (WhatEver = @Original_WhatEver OR @Original_WhatEver IS NULL
AND WhatEver IS NULL); " & _
"SELECT AutoId, WhatEver FROM Sample WHERE (AutoId = @AutoId)"
cmdUpdate.Connection = Conn
cmdUpdate.Parameters.Add(New SqlParameter("@AutoId",
SqlDbType.Int,
4, "AutoId"))
cmdUpdate.Parameters.Add(New SqlParameter("@WhatEver",
SqlDbType.NVarChar, 50, "WhatEver"))
cmdUpdate.Parameters.Add(New SqlParameter("@Original_AutoId",
SqlDbType.Int, 4, _
ParameterDirection.Input, False, nb, nb, _
"AutoId", DataRowVersion.Original, Nothing))
cmdUpdate.Parameters.Add(New SqlParameter("@Original_WhatEver", _
SqlDbType.NVarChar, 50, ParameterDirection.Input, False, nb, nb,
_
"WhatEver", DataRowVersion.Original, Nothing))
'
What you see is that there is a Update and a Select in it.

The Select reads the database confirm the keys as are in your dataset.
The dataadapter checks if everything is still the same to the original
rows
which are still in your dataset before you do acceptchanges or that the
dataadapter does that acceptchanges.
If not than there is a concurrency error.
Simple is it not?

You can see those original rows by writing an dataset with changes to
disk
using

ds.writeXML("path", xmlwritemode.diffgram)

I hope to makes it something more clear.

Cor
"Rich" <Ri**@discussions.microsoft.com> schreef in bericht
news:D9**********************************@microsof t.com...
> Hi Cor,
>
> Thank you for your reply. I looked at your website, but I came up with
> another method using dataRow.BeginEdit and dataRow.EndEdit. The
> UpdateCommand on my DataAdapter seems to work now.
>
> I have been reading several columns/articles that advise against using
> the
> commandBuilder. One of my goals for using the DataAdpater is
> specifically
> for checking for optimistic concurrency. May I ask how this is done?
> I
> admit that I had a little bit of trouble following the example on your
> site
> (due to my lack of experience).
>
> Thanks,
> Rich
>
>
> "Cor Ligthert [MVP]" wrote:
>
>> Rich,
>>
>> Your update command is incomplete, with a dataadapter you normally
>> test
>> if
>> there is no problem with the optimistic concurrency. It is done using
>> the
>> SQL command. Have a look at this most simple sample on our website.
>>
>> http://www.vb-tips.com/default.aspx?...2-d7c12bbb3726
>>
>> If you have a simple update as this mostly the commandbuilder makes
>> your
>> life much easier.
>>
>> All the insert, update and delete handling for a table are made
>> dynamicly
>> while using the dataadapter after doing this command.
>>
>> dim cmb as new SQLCommandBuilder(da)
>>
>> I hope this helps,
>>
>> Cor
>>
>> "Rich" <Ri**@discussions.microsoft.com> schreef in bericht
>> news:31**********************************@microsof t.com...
>> >I think I figured this out (because I finally got it to work). What
>> >I
>> >did
>> > was to assign a datarow object to the row I wanted to update and
>> > Parameters
>> > to the UpdateCommand and modified the commandtext to use parameters.
>> > Then
>> > I
>> > updated the items in the datarow that I wanted to change. Then I
>> > updated
>> > the
>> > datatable. Now I can see the update in tblx(datatable) tbl1 (sql
>> > server
>> > table).
>> >
>> >
>> > Dim da As New SqlDataAdapter("Select * from tbl1", conn)
>> > dim tblx As New DataTable
>> > da.Fill(tblx)
>> >
>> > da.UpdateCommand = New SqlCommand
>> > da.UpdateCommand.Connection = conn
>> > da.UpdateCommand.CommandText = "Update tbl1 set fld1 = @p1 where ID
>> > =@p0"
>> >
>> > da.UpdateCommand.Parameters.Add(New SqlParameter("@p0,
>> > SqlDBtype.Int,
>> > 4,
>> > "ID")
>> > da.UpdateCommand.Parameters.Add(New SqlParameter("@p1,
>> > SqlDBtype.varchar,
>> > 10, "fld1")
>> > Dim dr As DataRow = tblx.Rows(0)
>> > dr("fld1") = "test"
>> >
>> > da.Update(tblx) '--now I can see the update in tblx/tbl1
>> >
>> >
>> > the example using cmd was only updating tbl1 on the sql server. But
>> > when
>> > I
>> > was trying to use the Adapter - I was not updating tblx which is
>> > what
>> > updates
>> > tbl1 on the sql server. Now I can see the update in the client app
>> > as
>> > well
>> > as the sql server.
>> >
>> >
>> > "Rich" wrote:
>> >
>> >> Dim da As New SqlDataAdapter("Select * from tbl1", conn)
>> >> dim tblx As New DataTable
>> >> da.Fill(tblx) '--works OK up to this point
>> >>
>> >> da.UpdateCommand = New SqlCommand
>> >> da.UpdateCommand.Connection = conn
>> >> da.UpdateCommand.CommandText = "Update tbl1 set fld1 = 'test' where
>> >> ID
>> >> =
>> >> 1"
>> >>
>> >> da.Update(tblx) '--tblx/tbl1 not getting updated here.
>> >>
>> >> But if I do this---then it works OK (not using dataAdapter though)
>> >>
>> >> Dim cmd As New SqlCommand
>> >> cmd.Connection = conn
>> >> cmd.CommandType = CommandType.Text
>> >> cmd.CommandText = "Update tbl1 set fld1 = 'test' where ID = 1"
>> >> conn.Open
>> >> cmd.ExecuteNonQuery
>> >> cmd.Close
>> >>
>> >> This does update tbl1 correctly. But I need to use the
>> >> dataAdapter.
>> >> What
>> >> do I need to do to the dataAdapter.UpdateCommand code to make that
>> >> update
>> >> correctly?
>> >>
>> >> Thanks,
>> >> Rich
>>
>>
>>


Jun 7 '06 #7

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

Similar topics

0
by: D Witherspoon | last post by:
When sending a dataset back to a dataadapter with an update command (SQL Stored Proc) i get an error saying missing paremeter for paramters that have their sourcefield a value of null. This will...
2
by: Rich | last post by:
Hello, On a form I have 3 textboxes, txt0, txt1, txt2 which contain all integers. I have also placed a connection component (conn), dataadapter component (da1) and dataset component (ds1) from...
13
by: Doug Bell | last post by:
Hi, I thought I had this sorted this morning but it is still a problem. My application has a DataAccess Class. When it starts, it: Connects to a DB (OLE DB) If it connects it uses an...
3
by: GatorBait | last post by:
Hi All, I have a dataset that has more than one table in it so I am unable to use the CommandBuilder to automatically generate an update command so I have to do it by hand. I am a little...
2
by: susan.f.barrett | last post by:
Hi, Despite me being able to type the following in to SQL Server and it updating 1 row: > updatestockcategory 1093, 839 In my code, it is not updating any rows. dataSet = new DataSet();
1
by: Rich | last post by:
Hello, I can update a dataset from my client app using a dataAdapter.Updatecommand when I add parameter values outside of the param declaration. But If I add the param values inline with the...
2
by: BobLewiston | last post by:
I can read in an SQL table ("Person.Contact") from AdventureWorks and step through it one row at a time, but when I try to save a record, either one I'm inserting or one I'm editting, I get the...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.