473,406 Members | 2,954 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,406 software developers and data experts.

DATAGRID - How can I change UpdateCommand dynamically ?

I have 2 grids - one shows a list of table names in a database and
when you click on a table name the other grid dynamically populates
the grid with the table contents. My problem is that I cannot get any
changes I do in the grid of a given table saved back to the database
unless I nominate a particular table in the SQLDataAdapter but this is
not dynamic as the table changes when the user picks another table
I have the first part working nicely this way..

' for the list of table names
dim cmd = New System.Data.SqlClient.SqlCommand("SELECT name FROM
sysobjects WHERE (xtype = 'U') and name <> 'dtproperties'",
SqlConnection)
SqlDataAdapterVOL.SelectCommand = cmd
DataSetVOL.Clear()
SqlDataAdapterVOL.Fill(DataSetVOL)
DataGridVol.Expand(-1)
DataGridVol.DataMember = DataSetVOL.Tables(0).TableName
DataGridVol.Select(0)

'for the table contents
cmd = "SELECT * FROM " &
DataGridVol.Item(DataGridVol.CurrentCell.RowNumber , 0), SqlConnection)
SqlDataAdapterVOL.SelectCommand = cmd
DataSetWI.Tables.Clear()
DataSetWI.Tables.Add(DataGridVol.Item(DataGridVol. CurrentCell.RowNumber,
0))
SqlDataAdapterVOL.Fill(DataSetWI, DataGridVol.Item
DataGridVol.CurrentCell.RowNumber, 0))
DataGridWI.DataSource = DataSetWI
DataGridWI.Expand(-1)
DataGridWI.DataMember = DataSetWI.Tables(0).TableName

but when I want to update a cell in a table it
does not update in the database unless the table is specified in the
adapter with all the sql command for update, delete etc.
Do I have to programmatically update the SQL "update" command for the
new table ? Or is there a way for it to work out the update command
dynamically given I know the newly selected table name ?

'the update bit I use is
DataSetWI.Tables(0).Rows(row).Item(i) = Avalue
DataGridWI.Item(row, i) = Avalue
Dim cb As New System.Data.SqlClient.SqlCommandBuilder(SqlDataAda pterWI)
SqlDataAdapterWI.Update(DataSetWI, DataGridVol.Item
DataGridVol.CurrentCell.RowNumber, 0))

Maybe its not possible to use a SQLDataAdapter on more than one table
?

thanks
Andrew
Nov 15 '05 #1
3 2663
Hello Andrew,

According to the MSDN topic titled "SqlDataAdapter.UpdateCommand Property":

----------------------------------------------------------------------------
---------------------------
During Update, if this property is not set and primary key information is
present in the DataSet, the
UpdateCommand can be generated automatically if you set the SelectCommand
property and use the
SqlCommandBuilder. Then, any additional commands that you do not set are
generated by the
SqlCommandBuilder. This generation logic requires key column information to
be present in the
DataSet. For more information see Automatically Generated Commands.
----------------------------------------------------------------------------
---------------------------

Does this answer your question?

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Andrew Dodgshun" <an*************@eistream.com.au> wrote in message
news:8a**************************@posting.google.c om...
I have 2 grids - one shows a list of table names in a database and
when you click on a table name the other grid dynamically populates
the grid with the table contents. My problem is that I cannot get any
changes I do in the grid of a given table saved back to the database
unless I nominate a particular table in the SQLDataAdapter but this is
not dynamic as the table changes when the user picks another table
I have the first part working nicely this way..

' for the list of table names
dim cmd = New System.Data.SqlClient.SqlCommand("SELECT name FROM
sysobjects WHERE (xtype = 'U') and name <> 'dtproperties'",
SqlConnection)
SqlDataAdapterVOL.SelectCommand = cmd
DataSetVOL.Clear()
SqlDataAdapterVOL.Fill(DataSetVOL)
DataGridVol.Expand(-1)
DataGridVol.DataMember = DataSetVOL.Tables(0).TableName
DataGridVol.Select(0)

'for the table contents
cmd = "SELECT * FROM " &
DataGridVol.Item(DataGridVol.CurrentCell.RowNumber , 0), SqlConnection)
SqlDataAdapterVOL.SelectCommand = cmd
DataSetWI.Tables.Clear()
DataSetWI.Tables.Add(DataGridVol.Item(DataGridVol. CurrentCell.RowNumber,
0))
SqlDataAdapterVOL.Fill(DataSetWI, DataGridVol.Item
DataGridVol.CurrentCell.RowNumber, 0))
DataGridWI.DataSource = DataSetWI
DataGridWI.Expand(-1)
DataGridWI.DataMember = DataSetWI.Tables(0).TableName

but when I want to update a cell in a table it
does not update in the database unless the table is specified in the
adapter with all the sql command for update, delete etc.
Do I have to programmatically update the SQL "update" command for the
new table ? Or is there a way for it to work out the update command
dynamically given I know the newly selected table name ?

'the update bit I use is
DataSetWI.Tables(0).Rows(row).Item(i) = Avalue
DataGridWI.Item(row, i) = Avalue
Dim cb As New System.Data.SqlClient.SqlCommandBuilder(SqlDataAda pterWI)
SqlDataAdapterWI.Update(DataSetWI, DataGridVol.Item
DataGridVol.CurrentCell.RowNumber, 0))

Maybe its not possible to use a SQLDataAdapter on more than one table
?

thanks
Andrew


Nov 15 '05 #2
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote in message news:<Oj**************@TK2MSFTNGP09.phx.gbl>...
Hello Andrew,

According to the MSDN topic titled "SqlDataAdapter.UpdateCommand Property":

----------------------------------------------------------------------------
---------------------------
During Update, if this property is not set and primary key information is
present in the DataSet, the
UpdateCommand can be generated automatically if you set the SelectCommand
property and use the
SqlCommandBuilder. Then, any additional commands that you do not set are
generated by the
SqlCommandBuilder. This generation logic requires key column information to
be present in the
DataSet. For more information see Automatically Generated Commands.
----------------------------------------------------------------------------
---------------------------

Does this answer your question?

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE


Thanks for the suggestion but ....- I tried this exactly as described
in the help and still nothing gets populated in the Update Command (or
the delete or insert). The Select command gets updated correctly and
there is a primary key in the table ?
Here is my code ...

Dim cmd As System.Data.SqlClient.SqlCommand
cmd = New System.Data.SqlClient.SqlCommand("SELECT * FROM " &
DataGridVol.Item(DataGridVol.CurrentCell.RowNumber , 0), SqlConnection)
SqlDataAdapterWI.SelectCommand = cmd
Dim custCB As System.Data.SqlClient.SqlCommandBuilder = New
System.Data.SqlClient.SqlCommandBuilder(SqlDataAda pterWI)
custCB.QuotePrefix = "["
custCB.QuoteSuffix = "]"
custCB.RefreshSchema()
SqlDataAdapterWI.Fill(DataSetWI, DataGridVol.Item
DataGridVol.CurrentCell.RowNumber, 0))
Nov 15 '05 #3
Hold everything !!
It did work it just doesn't show the sql statement in the watch window
whilst debugging - not sure why but the update works which is what
matters

thanks
Andrew
Nov 15 '05 #4

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

Similar topics

2
by: Sebi | last post by:
Hello all is it possible to add a checkbox in a DataGrid for Boolean Data? Thanks in advance
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
1
by: Rick | last post by:
Hello all, I hope all is well with you. I am having a seriously difficult time with this problem. Allow me to set up the problem. I have a System.Web.UI.Page with the following controls...
1
by: MrMike | last post by:
Hi. My application has dozens of datagrids but for some reason an exception occurs when one of them is updated. When a user edits a datagrid row and then clicks 'Update' the following exception...
2
by: Deepesh | last post by:
Good day, I have a specific case of the DataGrid in my solution which is causing the ItemCommand Event Not Firing. So I'm creating a "Skinnable" set of controls. I seperate the actual ASCX file...
0
by: herman404 | last post by:
Hi everyone, I'm trying to dynamically add 3 button columns to a DataGrid object that I have on an ASP.net webpage, but I don't see any documentation on how to do it. I've seen plenty of examples...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
0
by: arlie_maija | last post by:
Hey - I'm writing a control that contains a DataGrid, and I'm unable to get the update event to fire. When I click the update link, the edit event fires. heres the details... my control...
2
by: pozze | last post by:
Hi, I need to display images and other record information retrieved from an SQL 2005 database in a datagrid on a web page. I'm coding in VB .net I have recently changed over from VB ASP and i'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.