简体   繁体   中英

Not inserting value to DB, returning 0

I've tried about everything I can think of. I am creating and object and then inserting it in a SQL Server database. But for this particular mood, is not working (I recycled from other modules that are working).

public int Insert(int userId, int refNum, string name, string position, string lastPlace, string duration, int currentEmp,
                  int rehire, string reason, string areaStr, string areaWeak, string satisfac, string reasonSep, int form1, int form2,
                  int form3, int form4, int form5, int form6, int form7, int form8, int form9, int form10, int form11, int form12, int form13, int form14)
{
    int sumForm = form1 + form2 + form3 + form4 + form5 + form6 + form7 + form8 + form9 + form10 + form10 + form11 + form12 + form13 + form14;

    // SQL Command para llamar el stored procedure
    SqlCommand comando = new SqlCommand("dbo.[Referencia_Insert]", base.Db);

    // Title
    SqlParameter spUser = new SqlParameter("@userId", System.Data.SqlDbType.Int);
    spUser.Value = userId;
    comando.Parameters.Add(spUser);

    SqlParameter spRef = new SqlParameter("@refNum", System.Data.SqlDbType.Int);
    spRef.Value = refNum;
    comando.Parameters.Add(spRef);

    ....

    SqlParameter spSumForm = new SqlParameter("@sumForm", System.Data.SqlDbType.Int);
    spSumForm.Value = sumForm;
    comando.Parameters.Add(spSumForm);

    HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('checa esto: "+base.ExecuteScalar(comando)+"')</SCRIPT>");

    // Ejecuta la consulta
    return base.ExecuteScalar(comando);
}

The alert is just returning 0. No errors, no anything.

This is the stored procedure:

ALTER PROCEDURE [dbo].[Referencia_Insert]
    @userID int, 
    @refNum int, 
    @name varchar(MAX), 
    @position varchar(MAX), 
    @lastPlace varchar(MAX), 
    @duration varchar(MAX), 
    @currentEmp int, 
    @rehire int, 
    @reason varchar(MAX), 
    @areaStr varchar(MAX), 
    @areaWeak varchar(MAX), 
    @satis varchar(MAX), 
    @reasonSep varchar(MAX), 
    @f1 int, @f2 int, @f3 int, @f4 int, @f5 int, @f6 int, 
    @f7 int, @f8 int, @f9 int, @f10 int, @f11 int, @f12 int, 
    @f13 int, @f14 int, 
    @sumForm int
WITH EXEC AS CALLER
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO [JobApplication].[dbo].[tbl_references] 
           ([userId], [refNum],[name], [position], [lastPlace], [duration],
            [currentEmp], [reHire], [reason], [areaStr], [areaWeak],
            [satisfac], [reasonSep],  
            [form1], [form2], [form3], [form4], [form5], [form6], [form7],
            [form8], [form9], [form10], [form11], [form12], [form13], [form14], [totalForm])
    VALUES (@userId, @refNum, @name, @position, @lastPlace, @duration,
            @currentEmp, @rehire, @reason, @areaStr, @areaWeak,
            @satis, @reasonSep,
            @f1, @f2, @f3, @f4, @f5, @f6, @f7,
            @f8, @f9, @f10, @f11, @f12, @f13, @f14, @sumForm)

    SELECT @@IDENTITY              
END

For some reason, it just returns a 0 in the ExecuteScalar . Why? I don't get it, because every time I try to display the values, they do show. And I am calling it in this section, with some arbitrary values.

It is supposed to return a 1 when it inserts anything, but I am getting just 0's.

protected void btnSend_Click(object sender, EventArgs e)
{
    ASF.HC.JobApplication.BO.Referencia refo = new ASF.HC.JobApplication.BO.Referencia();
    refo.Insert(370,1,"pruebini","tQM","la vida","la vida",1,1,"se fue","fuerza","debil","eu","holini",1,2,3,4,5,6,7,8,9,10,11,12,13,14);
}

EB.

You are missing the command type

SqlCommand comando = new SqlCommand("dbo.[Referencia_Insert]", base.Db);
comando.CommandType = CommandType.StoredProcedure

With this the ADO.NET will execute this query, and then you can get the value from SELECT @@IDENTITY

exec [dbo].[Referencia_Insert] ......

On the other hand, when the command type has not specified, the ADO.NET will execute the dynamic query like

exec sp_executesql N'[dbo].[Referencia_Insert]', N'.............'

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM