473,396 Members | 1,754 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,396 software developers and data experts.

data grid update

The following code:
Try
da.Update(ds.Tables(dg1Name))
Catch ex As Exception
Console.WriteLine(ex.ToString)
MessageBox.Show(ex.ToString)
End Try

causes this error:

System.InvalidOperationException: Update requires a valid UpdateCommand when
passed DataRow collection with modified rows.
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows,
DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at comments.Form1.Button1_Click(Object sender, EventArgs e) in
G:\Shared\VBShare\dotnet\Kevin\comments\Form1.vb:l ine 161

The code is behiend a button on the form called Update. If I change the data
in the grid I get the error when I press the update button. Any advice will
be much appreciated.

Kevin

Nov 21 '05 #1
10 1442
Kevin,

How did you create your update command?

You have as far as I know 3 possibilities
- by hand yourself
- using an AdapterWizard
- using an commandbuilder.

Now your error can be everything.

Cor
Nov 21 '05 #2
Cor,
I did not create an update command, I used the following code to fill the
grid, changed one of the records and thought that the dataset and data
adaptor took care of the rest?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim commands As String = Microsoft.VisualBasic.Command
Dim strDatabase, strSQL As String

strDatabase = Mid(commands, 1, 7)
gstrCustomer = Mid(commands, 9, 12)
gstrOrder = Mid(commands, 22, 8)
gstrOrdType = Mid(commands, 31, 1)

Call connectSQL(strDatabase)

'fill the grid
strSQL = "select '" & "Y" & "' as Selected, seqNo, comment from
sql_oe_comments where custNo = '" & gstrCustomer & "'"
cb = New SqlCommandBuilder(da)

ds = New DataSet()

da = New SqlDataAdapter(strSQL, sqlConn)

da.Fill(ds, dg1Name)

With DG1 'this is the datagrid on the form
.DataSource = ds.Tables(dg1Name)
.AllowSorting = True
.AlternatingBackColor = System.Drawing.Color.Bisque
End With

'format the grid
Call formatGridDG1()
End Sub
"Cor Ligthert" wrote:
Kevin,

How did you create your update command?

You have as far as I know 3 possibilities
- by hand yourself
- using an AdapterWizard
- using an commandbuilder.

Now your error can be everything.

Cor

Nov 21 '05 #3
Kevin,

There is a commandbuilder in the code you showed and that I asked for.

By the way, are you sure that this works
da.Fill(ds, dg1Name)

And not just this
da.Fill(ds)

Let us assume that you have a button on your form on what you click and in
that click event you set this code

BindingContext(ds.Tables(0)).EndCurrentEdit()
Try
da.Update(ds.Tables(0))
Catch ex As Exception
Console.WriteLine(ex.ToString)
MessageBox.Show(ex.ToString)
End Try

Than it is bad code, however it will probably work and when you are a newbie
is that one of the first things that is important.

I hope this helps

Cor.

Nov 21 '05 #4
Cor,
Thanks for the reply. dg1Name is a public constant. I found (by trial and
error) that if I don't use it in these three places: da.Fill(ds, dg1Name),
..DataSource = ds.Tables(dg1Name), ts.MappingName = dg1Name I get an error on
this line of code: Me.DG1.TableStyles(0).GridColumnStyles(2).HeaderTe xt =
"COMMENTS"

I took out the grid foramtting and used ds.fill(0) as you suggested adding
the binding code and received the same error originally reported about the
command builder? If there is a better way to do this please let me know, the
book I have suggests that it should work, obviously I must be doing something
wrong.

Thanks,

Kevin
Kevin,

There is a commandbuilder in the code you showed and that I asked for.

By the way, are you sure that this works
da.Fill(ds, dg1Name)

And not just this
da.Fill(ds)

Let us assume that you have a button on your form on what you click and in
that click event you set this code

BindingContext(ds.Tables(0)).EndCurrentEdit()
Try
da.Update(ds.Tables(0))
Catch ex As Exception
Console.WriteLine(ex.ToString)
MessageBox.Show(ex.ToString)
End Try

Than it is bad code, however it will probably work and when you are a newbie
is that one of the first things that is important.

I hope this helps

Cor.

Nov 21 '05 #5
Kevin,

Your select statement looks not simple for me to translate for the
commandbuilder, can you not first try it with a simple one as

"Select * from MyTable"

Cor
Nov 21 '05 #6
Cor,
I tried a simple statement and got the error: "update requires a valid
update command..." I have a command builder object but have not structured
any update statements for it? Is there a repository of code samples that show
how this is done? The book I have isn't clear about how the update works.

Thanks,

Kevin

"Cor Ligthert" wrote:
Kevin,

Your select statement looks not simple for me to translate for the
commandbuilder, can you not first try it with a simple one as

"Select * from MyTable"

Cor

Nov 21 '05 #7
Kevin,

Can you show us a part of your code as you have it now, the commandbuilder
needs in most cases like yours, nothing extra than that it is called.

Cor
Nov 21 '05 #8
Cor,
Here's the code I'm currently using to fill the grid:
Public ds As New Data.DataSet()
Public da As SqlClient.SqlDataAdapter
Public cmd As SqlClient.SqlCommandBuilder()
Private Sub Form Load
strConn = "Initial Catalog=data_05;Data Source=kevin;User
ID=test;password=test;"
strSQL = "select * from sql_oe_comments"
da = New SqlClient.SqlDataAdapter(strSQL, strConn)
da.Fill(ds)
dg1.DataSource = ds.Tables(0)

End Sub
Then I modify data in the grid and click the update button which has the
following code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
dg1.Update()
'da.update(ds.tables(0))

Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try

End Sub
With this code I do not get an error when I press the button but the sql
data does not change in the database? If I uncomment the da.update... then I
get an error that a valid update command is required?
"Cor Ligthert" wrote:
Kevin,

Can you show us a part of your code as you have it now, the commandbuilder
needs in most cases like yours, nothing extra than that it is called.

Cor

Nov 21 '05 #9
Kevin,

I am glad I asked it to show some code, you don't use a commandbuilder, you
only create a placeholder for it. Can you try the changes I wrote in line. I
did it in this message so watch typos.
Public ds As New Data.DataSet()
Public da As SqlClient.SqlDataAdapter ............ Private Sub Form Load
strConn = "Initial Catalog=data_05;Data Source=kevin;User
ID=test;password=test;"
strSQL = "select * from sql_oe_comments"
da = New SqlClient.SqlDataAdapter(strSQL, strConn)
da.Fill(ds)
dim cmd as new sqlClient.SqlCommandBuilder(da)
dg1.DataSource = ds.Tables(0).defaultview
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click Me.BindingContext(dg1.DataSource).EndCurrentEdit()
Try da.update(ds.tables(0)) Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try

End Sub


I hope this helps,

Cor
Nov 21 '05 #10
Thank you very much - it works now.

"Cor Ligthert" wrote:
Kevin,

I am glad I asked it to show some code, you don't use a commandbuilder, you
only create a placeholder for it. Can you try the changes I wrote in line. I
did it in this message so watch typos.
Public ds As New Data.DataSet()
Public da As SqlClient.SqlDataAdapter

............
Private Sub Form Load
strConn = "Initial Catalog=data_05;Data Source=kevin;User
ID=test;password=test;"
strSQL = "select * from sql_oe_comments"
da = New SqlClient.SqlDataAdapter(strSQL, strConn)
da.Fill(ds)


dim cmd as new sqlClient.SqlCommandBuilder(da)
dg1.DataSource = ds.Tables(0).defaultview
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Me.BindingContext(dg1.DataSource).EndCurrentEdit()
Try

da.update(ds.tables(0))
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try

End Sub


I hope this helps,

Cor

Nov 21 '05 #11

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

Similar topics

3
by: pmud | last post by:
Hi, I have 4 columns in my sql database table. I added a data adapter , data set & a data grid to view this information. But the data grid is displaying those 4 columns TWICE , i.e in the data...
1
by: jijo kuruvila | last post by:
actually my code looks like this private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { DataGrid1.Columns.Visible=true;...
6
by: Tejpal Garhwal | last post by:
I have datagrid filled with some data rows. At the run time i want know how many total rows are there in the data grid ? Any idea ? Any Suggestions ? Thanks in advance Tej
13
by: nyt | last post by:
I have a problem of number and text field. I got the database file(mdb) that contains many combo boxes used and its list values are created by "value list" For eg field Field name= 'furniture'...
3
by: Vijendra Malhotra | last post by:
What is the fastest way to refresh contents in a data grid. I would like to populate a grid based on message from a EMS queue with a frequency of about 100 messages per second. what is the best...
2
by: marty | last post by:
I did some searchs, but can't find the answer to my problems. I have a editable DataGrid. I do an update of one of the rows. Postback "OnUpdateCommand" Call Oracle proc to do that actual...
7
by: John J. Hughes II | last post by:
I have a DataGridView with a TextBoxColumn. I setting the data source to a List<stringvalue in a static class. The list is filled from a background thread. So far all is fine and it works...
9
by: Anil Gupte | last post by:
After reading a tutorial and fiddling, I finally got this to work. I can now put two tables created with a DataTable class into a DataRelation. Phew! And it works! Dim tblSliceInfo As New...
6
by: insirawali | last post by:
Hi all, I have this problem, i need to know is there a way i cn use the data adapter's update method in this scenario. i have 3 tables as below create table table1{ id1 int identity(1,1)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.