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

DataAdapter UpdateCommand not updating record

Hi,

Despite me being able to type the following in to SQL Server and it
updating 1 row:
updatestockcategory 1093, 839


In my code, it is not updating any rows.

dataSet = new DataSet();
dataAdapter = new SqlDataAdapter(SQLCommandString, conn);
dataAdapter.Fill(dataSet, "StockCategory");

dataTable = dataSet.Tables["StockCategory"];

dataAdapter.UpdateCommand = new
SqlCommand("UpdateStockCategory", conn);
dataAdapter.UpdateCommand.CommandType =
CommandType.StoredProcedure;

myParam =
dataAdapter.UpdateCommand.Parameters.Add("@ParentS tockCategoryId",
SqlDbType.Int, 0, "ParentStockCategoryId");
myParam.Value = ParentStockCategoryId;

myParam =
dataAdapter.UpdateCommand.Parameters.Add("@StockCa tegoryId",
SqlDbType.Int, 0, "StockCategoryId");
myParam.Value = StockCategoryId;

MessageBox.Show(dataAdapter.Update(dataSet,
"StockCategory").ToString());

The last line displays how many records were affected, which in this
case is 0. I am absoluetely sure that ParentStockCategoryId and
StockCategoryId have the right values so I don't know what I could be
doing wrong, it's been bothering me for hours!

Thanks

Mar 3 '06 #1
2 13919
Susan,
I think this was already covered in a similar post. The DataTable in your
dataset that you want to pass to your dataAdapter for the Update method has
one or more rows. Each of those rows has a RowState property. You may wish
to check this property on each row your source Datatable to see what I'm
referring to.
In order for the DataAdapter to actually call the update SQL or Stored Proc
on a particular row, its RowState must be "Modified".
Usually this happens because you have edited data in a datagrid for example,
where this datatable is the underlying DataSource. So if you edit a cell in
the Datagrid, the unnderlying row's RowState will be changed to Modified.
However in this case where you are simply pulling data out of the Database,
I don't think that will happen.
Hope that helps.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"su*************@googlemail.com" wrote:
Hi,

Despite me being able to type the following in to SQL Server and it
updating 1 row:
updatestockcategory 1093, 839


In my code, it is not updating any rows.

dataSet = new DataSet();
dataAdapter = new SqlDataAdapter(SQLCommandString, conn);
dataAdapter.Fill(dataSet, "StockCategory");

dataTable = dataSet.Tables["StockCategory"];

dataAdapter.UpdateCommand = new
SqlCommand("UpdateStockCategory", conn);
dataAdapter.UpdateCommand.CommandType =
CommandType.StoredProcedure;

myParam =
dataAdapter.UpdateCommand.Parameters.Add("@ParentS tockCategoryId",
SqlDbType.Int, 0, "ParentStockCategoryId");
myParam.Value = ParentStockCategoryId;

myParam =
dataAdapter.UpdateCommand.Parameters.Add("@StockCa tegoryId",
SqlDbType.Int, 0, "StockCategoryId");
myParam.Value = StockCategoryId;

MessageBox.Show(dataAdapter.Update(dataSet,
"StockCategory").ToString());

The last line displays how many records were affected, which in this
case is 0. I am absoluetely sure that ParentStockCategoryId and
StockCategoryId have the right values so I don't know what I could be
doing wrong, it's been bothering me for hours!

Thanks

Mar 3 '06 #2
P.S. -- I think what you really are wanting to do is something more like this:

dataSet = new DataSet();
dataAdapter = new SqlDataAdapter(SQLCommandString, conn);
dataAdapter.Fill(dataSet, "StockCategory");
dataTable = dataSet.Tables["StockCategory"];

SqlCommand UpdateCommand = new SqlCommand("UpdateStockCategory", conn);
UpdateCommand.CommandType =CommandType.StoredProcedure;
foreach(DataRow row in dataTable)
{
myParam=UpdateCommand.Parameters.Add("@ParentStock CategoryId",
SqlDbType.Int, 0, "ParentStockCategoryId");
myParam.Value = row["ParentStockCategoryId"];
myParam
=dataAdapter.UpdateCommand.Parameters.Add("@StockC ategoryId",SqlDbType.Int,
0, "StockCategoryId");
myParam.Value = row["StockCategoryId"];
int result=UpdateCommand.ExecuteNonQuery();
}
conn.Close();

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"su*************@googlemail.com" wrote:
Hi,

Despite me being able to type the following in to SQL Server and it
updating 1 row:
updatestockcategory 1093, 839


In my code, it is not updating any rows.

dataSet = new DataSet();
dataAdapter = new SqlDataAdapter(SQLCommandString, conn);
dataAdapter.Fill(dataSet, "StockCategory");

dataTable = dataSet.Tables["StockCategory"];

dataAdapter.UpdateCommand = new
SqlCommand("UpdateStockCategory", conn);
dataAdapter.UpdateCommand.CommandType =
CommandType.StoredProcedure;

myParam =
dataAdapter.UpdateCommand.Parameters.Add("@ParentS tockCategoryId",
SqlDbType.Int, 0, "ParentStockCategoryId");
myParam.Value = ParentStockCategoryId;

myParam =
dataAdapter.UpdateCommand.Parameters.Add("@StockCa tegoryId",
SqlDbType.Int, 0, "StockCategoryId");
myParam.Value = StockCategoryId;

MessageBox.Show(dataAdapter.Update(dataSet,
"StockCategory").ToString());

The last line displays how many records were affected, which in this
case is 0. I am absoluetely sure that ParentStockCategoryId and
StockCategoryId have the right values so I don't know what I could be
doing wrong, it's been bothering me for hours!

Thanks

Mar 3 '06 #3

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

Similar topics

2
by: hch | last post by:
dataAdapter.Update(data, "TableName") won’t work! I was about to deploy my first website on the Internet only to discover that the dataAdapter.Update() throws the Server Error in the third...
2
by: Joe Fetters via .NET 247 | last post by:
Have googled and read the VS.NET documentation can't seem to getthe answer to the following. Environment: Framework 1.1 VB.NET WinForm Access database Using all automagic tools (DataAdapter...
1
by: Julia Sats | last post by:
Hi, This is code from MSDN help (OracleDataAdapter.UpdateCommand Property) ' Create the UpdateCommand. cmd = New OracleCommand("UPDATE Dept SET DeptNo = pDeptNo, DName = pDName " & _ "WHERE...
2
by: Rich | last post by:
Hello, On a form I have 3 textboxes, txt0, txt1, txt2 which contain all integers. I have also placed a connection component (conn), dataadapter component (da1) and dataset component (ds1) from...
8
by: Zorpiedoman | last post by:
I keep getting a concurrency exception the second time I make a change and attempt to update a dataadapter. It appears this is by design, so there must be something I can do to avoid it. ...
1
by: Rich | last post by:
Hello, I can update a dataset from my client app using a dataAdapter.Updatecommand when I add parameter values outside of the param declaration. But If I add the param values inline with the...
6
by: Rich | last post by:
Dim da As New SqlDataAdapter("Select * from tbl1", conn) dim tblx As New DataTable da.Fill(tblx) '--works OK up to this point da.UpdateCommand = New SqlCommand da.UpdateCommand.Connection =...
3
by: Rich | last post by:
What is the diffeence bewtween a dataAdapter.InsertCommand and dataAdapter.SelectCommand (and dataAdapter.UpdateCommand for that matter)? Dim da As SqlDataAdapter conn.Open da.SelectCommand =...
2
by: BobLewiston | last post by:
I can read in an SQL table ("Person.Contact") from AdventureWorks and step through it one row at a time, but when I try to save a record, either one I'm inserting or one I'm editting, I get the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.