Search code examples
c#sqlprocedure

Procedure with @tpv not working from c#


I have a procedure in Sql that get @tpv as a parameter. when I exec the procedure from c# - It is not working, and when I exec the procedure from Sql - It's working perfect. Why?

Edit: when I exec the proc from c# - it is not get an error, seem like it is work fine, but the table didn't change at all and misRow=0 instead of misRow=1.

Procedure:

CREATE procedure [dbo].[UpdateAdsList]
@tvp ListCrc32 readonly
as
 update tb
 set a_update=CONVERT(date,GETDATE(),101)
 from Ads tb
 join @tvp t on t.crc32 = a_crc32

Exec from Sql:

declare @ ListCrc32

insert into @ (crc32)
select 1652908150

exec UpdateAdsList @

Exec from C#:

int mis=1652908150;

//create the dataTable
dt = new DataTable();
dt.Columns.Add("crc32", typeof(int));
var row = dt.NewRow();
row["crc32"] = mis;
dt.Rows.Add(row);

//exec the proc
con.Open();
var cmd = new SqlCommand("UpdateAdsList",con);
cmd.Parameters.Add("@tvp",SqlDbType.Structured);
cmd.Parameters["@tvp"].Value = dt;
cmd.Parameters["@tvp"].TypeName = "ListCrc32";
int misRow=cmd.ExecuteNonQuery();
con.close();

Solution

  • You might try to add the parameter without the type:

    int mis=1652908150;
    
    //create the dataTable
    dt = new DataTable();
    dt.Columns.Add("crc32", typeof(int));
    var row = dt.NewRow();
    row["crc32"] = mis; // I do not see the point of convert.
    dt.Rows.Add(row);
    
    //exec the proc
    var cmd = new SqlCommand("UpdateAdsList",con);
    cmd.CommandType = Syste.Data.CommandType.StoredProcedure;
    
    cmd.Parameters.AddWithValue("@tvp",dt);
    
    con.Open();
    
    int misRow=cmd.ExecuteNonQuery();
    
    con.close();