alejandro carnero
alejandro carnero

Reputation: 1784

update procedure in asp.net dont work

These procedure works , idcliente is the ID column of the table:

alter procedure  updatenome (@ixliente nvarchar(60))as
select idcliente,nome,endere,tel,celular,CIDADE,iest,cep,nasc from tbcliente
where idcliente like  @ixliente 

These C# code in asp.net dont return error :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;     
string guid =  LblIDCliente.Text;

        if (conx.State != ConnectionState.Open)
        { conx.Open(); }

        SqlDataAdapter da2 = new SqlDataAdapter();
        DataSet dss = new DataSet();
        SqlCommandBuilder constru2 = new SqlCommandBuilder(da2);
        SqlParameter empi = new SqlParameter("@ixliente", SqlDbType.NVarChar);
        empi.Value = guid;
        SqlCommand llena = new SqlCommand("updatenome", conx);
        llena.CommandType = CommandType.StoredProcedure;
        llena.Parameters.Add(empi);
        da2.SelectCommand = llena;
        da2.Fill(dss, "cliente"); 
        DataRow nova = dss.Tables["cliente"].Rows[0];
        nova.BeginEdit();
        nova["nome"] = TxtNome.Text;
        nova.EndEdit();
        da2.Update(dss.Tables["cliente"]);

but is not updating the table tbcliente , the line :

nova["nome"] = TxtNome.Text;

Don't take the changes when i write e a new value in txtnome.text; if i have write the new value directly works like this

nova["nome"] = "street name";

The procedure works the problem is I'm making some mistake, the event don't take the changes I write in the textbox

Upvotes: 1

Views: 91

Answers (3)

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38663

just change this below line

da2.Fill(dss, "cliente"); 
DataRow nova = dss.Tables["cliente"].Rows[0];
nova.BeginEdit();
nova["nome"] = TxtNome.Text;
nova.EndEdit();
da2.Update(dss.Tables["cliente"]);

to

da2.Fill(dss); 
DataRow nova = dss.Tables[0][0];
nova.BeginEdit();
nova["nome"] = TxtNome.Text;
nova.EndEdit();
da2.Update(dss);

or

  da2.Fill(dss, "cliente"); 
  DataRow nova = dss.Tables["cliente"].Rows[0];
  nova.BeginEdit();
  nova["nome"] = TxtNome.Text;
  nova.EndEdit();
  da2.Update(dss.Tables["cliente"]);    
  nova.AcceptChanges();
  nova.SetModified();// Must call this lines

or

Simply first update your table by using update procedure, then fetch data from your table.

more details see this : http://msdn.microsoft.com/en-us/library/33y2221y.aspx

Upvotes: 0

Steve
Steve

Reputation: 216273

Add these lines before calling Update

da2.InsertCommand = constru2.GetInsertCommand();
da2.UpdateCommand = constru2.GetUpdateCommand();
da2.DeleteCommand = constru2.GetDeleteCommand();
da2.Update(dss.Tables["cliente"]);

Now the DataAdapter has the proper command build and can update the changed rows.
Of course this works only if your table has a primarykey defined.

Also I agree with Hans Kesting. Use proper name for your storedproc.
If you look at a procedure called updatesomething what do you think about its internal working?

Upvotes: 0

addy2601
addy2601

Reputation: 387

If you are using Store Procedure with SqlDataAdapter for fetching the data, than you have to specifically write a Store Procedure for update also.

Upvotes: 1

Related Questions