I really don't know what to do. Could you please help me?
What I'm trying to do is that when a person watns to modify his data shown at screen taken from the DataBase when they finish modifing they click in a button and that's where the stored procedure comes in a checks if the data was modified it updates but if it isn't modified...well it doesn't modify the DataBase BUT when i run the proyect it just doesn't work.
The funny thing is that the Stored Procedure works at SQLServer but at C# won't.
Here's my code:
Stored Procedure:
Create Procedure SP_PDF
(@nombre varchar(50), @tel int, @dir varchar(50), @rfc char(13))
as
declare @cont int
set @cont = 0
while(@cont < 4)
Begin
if((select nombre from cat_cliente where rfc = @rfc) != @nombre)
Begin
update cat_cliente set nombre = @nombre
End
else
Begin
set @cont += 1
End
if((select telefono from cat_cliente where rfc = @rfc) != @tel)
Begin
update cat_cliente set telefono = @tel
End
else
Begin
set @cont += 1
End
if((select direccion from cat_cliente where rfc = @rfc) != @dir)
Begin
update cat_cliente set direccion = @dir
End
else
Begin
set @cont += 1
End
End
C#:
String query = "exec SP_PDF @nombre, @tel, @dir, @rfc";
SqlCommand cmd = new SqlCommand(query, cnx);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@nombre", SqlDbType.VarChar).Value = Nombre.Text;
cmd.Parameters.AddWithValue("@tel", SqlDbType.Int).Value = Telefono.Text;
cmd.Parameters.AddWithValue("@dir", SqlDbType.VarChar).Value = Direccion.Text;
cmd.Parameters.AddWithValue("@rfc", SqlDbType.Char).Value = RFC.Text;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet set = new DataSet();
try
{
cnx.Open();
cmd.ExecuteNonQuery();
adp.Fill(set);
Response.Write("<script>window.alert('¡Funciono!');</script>");
}
catch (Exception ex)
{
throw ex;
}
finally
{
cnx.Close();
}
Try This Alter your SP as below
ALTER Procedure SP_PDF
(@nombre varchar(50), @tel int, @dir varchar(50), @rfc char(13))
as
declare @cont int
set @cont = 0
declare @sp_nombre varchar(50)
declare @sp_tel int
declare @sp_dir varchar(50)
declare @sp_rfc char(13)
select @sp_nombre=nombre,@sp_tel=telefono,@sp_dir=direccion from cat_cliente where rfc = @rfc
while(@cont < 4)
Begin
if( @sp_nombre <> @nombre)
Begin
update cat_cliente set nombre = @nombre
End
else
Begin
set @cont += 1
End
if(@sp_tel <> @tel)
Begin
update cat_cliente set telefono = @tel
End
else
Begin
set @cont += 1
End
if(@sp_dir <> @dir)
Begin
update cat_cliente set direccion = @dir
End
else
Begin
set @cont += 1
End
End
Write C# code as below
String query = "SP_PDF";
SqlCommand cmd = new SqlCommand(query, cnx);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@nombre", SqlDbType.VarChar).Value = Nombre.Text;
cmd.Parameters.AddWithValue("@tel", SqlDbType.Int).Value = Telefono.Text;
cmd.Parameters.AddWithValue("@dir", SqlDbType.VarChar).Value = Direccion.Text;
cmd.Parameters.AddWithValue("@rfc", SqlDbType.Char).Value = RFC.Text;
try
{
cnx.Open();
cmd.ExecuteNonQuery();
Response.Write("<script>window.alert('¡Funciono!');</script>");
}
catch (Exception ex)
{
throw ex;
}
finally
{
cnx.Close();
}
Modify like the following
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnx;
cmd.Text = "SP_PDF";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@nombre", SqlDbType.VarChar).Value = Nombre.Text;
cmd.Parameters.AddWithValue("@tel", SqlDbType.Int).Value = Telefono.Text;
cmd.Parameters.AddWithValue("@dir", SqlDbType.VarChar).Value = Direccion.Text;
cmd.Parameters.AddWithValue("@rfc", SqlDbType.Char).Value = RFC.Text;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet set = new DataSet();
try
{
cmd.Connection = cnx;
cnx.Open();
cmd.ExecuteNonQuery();
adp.Fill(set);
Response.Write("<script>window.alert('¡Funciono!');</script>");
}
catch (Exception ex)
{
throw ex;
}
finally
{
cnx.Close();
}
If you modify store procedure then again execute
ALTER Procedure SP_PDF
(@nombre varchar(50), @tel int, @dir varchar(50), @rfc char(13))
as
declare @cont int
set @cont = 0
while(@cont < 4)
Begin
if((select nombre from cat_cliente where rfc = @rfc) != @nombre)
Begin
update cat_cliente set nombre = @nombre
End
else
Begin
set @cont += 1
End
if((select telefono from cat_cliente where rfc = @rfc) != @tel)
Begin
update cat_cliente set telefono = @tel
End
else
Begin
set @cont += 1
End
if((select direccion from cat_cliente where rfc = @rfc) != @dir)
Begin
update cat_cliente set direccion = @dir
End
else
Begin
set @cont += 1
End
End
and then execute the code again.
As you mention that it's funny thing is that SP working in sql server and not in c#. B'coz there is some mistake in your code.
String query = "SP_PDF";
SqlCommand cmd = new SqlCommand(query, cnx);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@nombre", SqlDbType.VarChar).Value = Nombre.Text;
cmd.Parameters.AddWithValue("@tel", SqlDbType.Int).Value = Telefono.Text;
cmd.Parameters.AddWithValue("@dir", SqlDbType.VarChar).Value = Direccion.Text;
cmd.Parameters.AddWithValue("@rfc", SqlDbType.Char).Value = RFC.Text;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet set = new DataSet();
try
{
cnx.Open();
cmd.ExecuteNonQuery();
adp.Fill(set);
Response.Write("<script>window.alert('¡Funciono!');</script>");
}
catch (Exception ex)
{
throw ex;
}
finally
{
cnx.Close();
}
OR
Go to this site :- http://www.aspsnippets.com/Articles/Calling-Update-SQL-Server-Stored-Procedures-using-ADO.Net.aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.