473,785 Members | 2,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Sql Client.SqlComma nd("SELECT name FROM
sysobjects WHERE (xtype = 'U') and name <> 'dtproperties'" ,
SqlConnection)
SqlDataAdapterV OL.SelectComman d = cmd
DataSetVOL.Clea r()
SqlDataAdapterV OL.Fill(DataSet VOL)
DataGridVol.Exp and(-1)
DataGridVol.Dat aMember = DataSetVOL.Tabl es(0).TableName
DataGridVol.Sel ect(0)

'for the table contents
cmd = "SELECT * FROM " &
DataGridVol.Ite m(DataGridVol.C urrentCell.RowN umber, 0), SqlConnection)
SqlDataAdapterV OL.SelectComman d = cmd
DataSetWI.Table s.Clear()
DataSetWI.Table s.Add(DataGridV ol.Item(DataGri dVol.CurrentCel l.RowNumber,
0))
SqlDataAdapterV OL.Fill(DataSet WI, DataGridVol.Ite m
DataGridVol.Cur rentCell.RowNum ber, 0))
DataGridWI.Data Source = DataSetWI
DataGridWI.Expa nd(-1)
DataGridWI.Data Member = DataSetWI.Table s(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 programmaticall y 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.Table s(0).Rows(row). Item(i) = Avalue
DataGridWI.Item (row, i) = Avalue
Dim cb As New System.Data.Sql Client.SqlComma ndBuilder(SqlDa taAdapterWI)
SqlDataAdapterW I.Update(DataSe tWI, DataGridVol.Ite m
DataGridVol.Cur rentCell.RowNum ber, 0))

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

thanks
Andrew
Nov 15 '05 #1
3 2695
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
SqlCommandBuild er. Then, any additional commands that you do not set are
generated by the
SqlCommandBuild er. 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.goo gle.com...
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.Sql Client.SqlComma nd("SELECT name FROM
sysobjects WHERE (xtype = 'U') and name <> 'dtproperties'" ,
SqlConnection)
SqlDataAdapterV OL.SelectComman d = cmd
DataSetVOL.Clea r()
SqlDataAdapterV OL.Fill(DataSet VOL)
DataGridVol.Exp and(-1)
DataGridVol.Dat aMember = DataSetVOL.Tabl es(0).TableName
DataGridVol.Sel ect(0)

'for the table contents
cmd = "SELECT * FROM " &
DataGridVol.Ite m(DataGridVol.C urrentCell.RowN umber, 0), SqlConnection)
SqlDataAdapterV OL.SelectComman d = cmd
DataSetWI.Table s.Clear()
DataSetWI.Table s.Add(DataGridV ol.Item(DataGri dVol.CurrentCel l.RowNumber,
0))
SqlDataAdapterV OL.Fill(DataSet WI, DataGridVol.Ite m
DataGridVol.Cur rentCell.RowNum ber, 0))
DataGridWI.Data Source = DataSetWI
DataGridWI.Expa nd(-1)
DataGridWI.Data Member = DataSetWI.Table s(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 programmaticall y 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.Table s(0).Rows(row). Item(i) = Avalue
DataGridWI.Item (row, i) = Avalue
Dim cb As New System.Data.Sql Client.SqlComma ndBuilder(SqlDa taAdapterWI)
SqlDataAdapterW I.Update(DataSe tWI, DataGridVol.Ite m
DataGridVol.Cur rentCell.RowNum ber, 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.c om> wrote in message news:<Oj******* *******@TK2MSFT NGP09.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
SqlCommandBuild er. Then, any additional commands that you do not set are
generated by the
SqlCommandBuild er. 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.Sql Client.SqlComma nd
cmd = New System.Data.Sql Client.SqlComma nd("SELECT * FROM " &
DataGridVol.Ite m(DataGridVol.C urrentCell.RowN umber, 0), SqlConnection)
SqlDataAdapterW I.SelectCommand = cmd
Dim custCB As System.Data.Sql Client.SqlComma ndBuilder = New
System.Data.Sql Client.SqlComma ndBuilder(SqlDa taAdapterWI)
custCB.QuotePre fix = "["
custCB.QuoteSuf fix = "]"
custCB.RefreshS chema()
SqlDataAdapterW I.Fill(DataSetW I, DataGridVol.Ite m
DataGridVol.Cur rentCell.RowNum ber, 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
3156
by: Sebi | last post by:
Hello all is it possible to add a checkbox in a DataGrid for Boolean Data? Thanks in advance
3
4885
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 the best method? Do you have a sample of how to do this?
1
4340
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 (watch the layout, some have child controls):
1
1592
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 occurs: --------------------- "Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index" --------------------- I've placed breakpoints with the UpdateCommand event, but...
2
3375
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 and .CS file. When I initialize my .CS file, in that code there is a method that goes: Page.LoadControl(FILENAME) Which associates a .ascx file with my .CS file, allowing me to plug in any
0
2035
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 on how to add regular columns, but how do you declare a button column? My code is posted below, I have a placeholder on a webform, and the datagrid will be added on the placeholder once it is initalised. Thanks! System.Web.UI.WebControls.Label...
9
2729
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 code: <script runat="server"> Dim sqlConn As New SqlConnection(".....") Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) If Not (Page.IsPostBack) Then FillDataGrid()
0
1535
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 overrides CreateChildControls and dynamically creates the DataGrid, creates an EditCommandColumn which it adds to the
2
2800
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 still getting used to .net I can successfully upload images and other data into the SQL Database, I'm currently storing RecordID, File Name, Type of File (mime), File Size, and File Data. I can retreive non binary data to a datagrid but I cannot...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10327
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.