Database doesn't update from datagrid using dataset

A

Assimalyst

Hi,

I'm attempting to update an SQL Server database from a datagrid. I have
followed one of microsofts walkthroughs as far as possible, but I am
using stored procedures where it did not, though i don't think that
should be a problem.

Here's the code:

private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();

TextBox tb;
tb = (TextBox)(e.Item.Cells[5].Controls[0]);
string strHistology = tb.Text;

sqlDataAdapter1.UpdateCommand.Parameters["@sampleNo"].Value = key;
sqlDataAdapter1.UpdateCommand.Parameters["@histology"].Value =
strHistology;

sqlDataAdapter1.Update(dsSample1);

DataGrid1.EditItemIndex = -1;
}

The string values key & strHistology are getting written correctly (87
& Squamous respectively) and the update query works with these values
using query analyser, but for some reason the updates are either not
been written to the dataset and/or the database using the above code.

Any ideas?

Thanks.
 
A

AMDRIT

It appears as though you are trying to update a dataset with a single
adapter, that is not how it is supposed to work.
If you setting the datasource of the datagrid to a datatable (part of a
dataset). Then all you have to do is update the datatable, you don't even
need to read the values from the grid to do it.

example:

class temp

public readonly property daTable as dataadapter

dim objcmdBuilder as commandbuilder
dim objdaTable as dataadataper
dim objConn as dataconnection

objConn = new dataconnection = "someconnnectrionstring"

objdaTable = new dataadapter("SomeSelectStatement",objConn)
objcmdBuilder = new commandbuilder(objdaTable)

return objdaTable

end property

end class


class Form1

private m_CTemp as Temp

sub UpdateData()
'I do not pretend to know what is in the webcontrol for a datagrid
'I imagine that is is still the same as what is in the winform control

dim dt as Datatable

'Get all the changes from last postback
dt = ctype(datagrid1.datasource, datatable).getchanges(added, updated,
deleted)

'Apply them to the repository
if not dt is nothing then m_CTemp.dsTable.update(dt)

end sub

end Class

I know you are using c#, I hope you can get the just of what I am doing from
my VB sample.

hope that helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top