Insert and update don't work

D

Diana Estrada

Hi, I have a store procedure to insert , and I use tableadapter to
execute this store procedure, when I run the application and execute the
sp , works ok
and after I see the datagridview and the new row is there, but the
problem ir that when I stop the application and look my database in SQL
Server 2005 the table don´t have the new row.
Somebody knows why? or Any idea to execute the store procedure to
insert or delete or update rows in SQL Server 2005 data base using C#
2005.

Thanks a lot!!
Regards!
 
J

Jon Skeet [C# MVP]

Diana Estrada said:
Hi, I have a store procedure to insert , and I use tableadapter to
execute this store procedure, when I run the application and execute the
sp , works ok
and after I see the datagridview and the new row is there, but the
problem ir that when I stop the application and look my database in SQL
Server 2005 the table don´t have the new row.
Somebody knows why? or Any idea to execute the store procedure to
insert or delete or update rows in SQL Server 2005 data base using C#
2005.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
D

Diana Estrada

thanks for your reply, well this is my code:

dbPrensas2006DataSetTableAdapters.spq_PrensaTableAdapter prensa = new
Prensas.dbPrensas2006DataSetTableAdapters.spq_PrensaTableAdapter();
int i = prensa.Update(ID, this.txtNombre.Text,
int.Parse(txtNumero.Text));
dbPrensas2006DataSet dsprensa = new dbPrensas2006DataSet();
prensa.Update(dsprensa);

Where:

Update execute my store procedure, this is my store procedure:

ALTER PROCEDURE spu_Prensa

(
@ID int,
@sNombre varchar(15),
@bNumero int =0
)

AS
if @ID=0
begin
insert into tPrensa (sNombre,bNumero)
values (@sNombre,@bNumero)
end
else
begin
update tPrensa
set sNombre=@sNombre, bNumero=@bNumero
where ID=@ID
end
RETURN

Thnks again, and I hope you can help me!!
 
M

Marc Gravell

In your example it is unclear how the ID is defined; is it possible that it
is currently NULL (either null or DBNull.Value in C#) instead of 0?

If so, your SP (as written) will neither insert nor update anything; in
which case a simple change to the SP would suffice:

IF @ID = 0

becomes

IF ISNULL(@ID, 0) = 0

Marc
 

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