Question updating the cellvalues of datagridview

jak

Member
Joined
Feb 21, 2009
Messages
19
Programming Experience
Beginner
hello Experts,
i have a datagridview and a textbox. i want to update the cells programetically but there is error "Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information." here is my code:


fill datagrid button code:

ada = New SqlDataAdapter

ds = New DataSet

con.ConnectionString = strCon

con.Open()

ada.SelectCommand = New SqlCommand("SELECT transaction_id,payment_no,due_date_of_payment,monthly_payment FROM customer_transaction where transaction_id='" & TextBox1.Text & "'", con)

cb = New SqlCommandBuilder(ada)

ada.Fill(ds)

DataGridView1.DataSource = ds.Tables(0)

con.Close()



update button code:


For i = 0 To ds.Tables(0).Rows.Count - 1

ds.Tables(0).Rows(i).BeginEdit()

ds.Tables(0).Rows(i).Item("monthly_payment") = val(textbox2.text)--- update by this value

ds.Tables(0).Rows(i).EndEdit()

Next


If ds.HasChanges Then


ada.Update(ds)


where i m doing wrong....... in my table (transaction_id + payment_no) is primary key....please help me...!!!!!!!!!
 
Your query is returning column information but no primary key information. You need to set the MissingSchemaAction property of your DataAdapter to AddWithKey, instead of the default Add.

By the way, there's no real need or point to calling BeginEdit and EndEdit unless you might call CancelEdit or the row might enter an invalid state in between.
 
hi jmcilhinney,

Actually i m new so could u please write some code for me...
thanx in advance....
 
Last edited:
New or not, you already know how to set properties so you don't need me to show you how to do it. If you can set a ConnectionString property, a SelectCommand property and a DataSource property then you can set a MissingSchemaAction property.
 
Actually i m new so could u please write some code for me...

Microsoft already did. I'd advise anyone new to read and understand the DW2 link in my signature, section Creating a Simple Data App. It contains a great tutorial that shows you how to do your data access properly. Also google for forms over data video.

Some of the things youre doing in your data access code here are very wrong; you need to get out of bad habits sooner rather than later
 
Back
Top