Why ExecuteNonQuery() returning -1 without any exception and without inserting the entry in database?

我的未来我决定 提交于 2019-12-25 05:15:43

问题


I have following function:

public Exception createTopic(Topic t)
{
    query = "insert into [DisData].[dbo].[discussions]([title],[description],[usrid],[dateadded],[desid],[likes],[shares],[visit],[replyto],[sno]) values(@title,@des,@uid,@dateadded,@did,@like,@share,@visit,@replyto,@sno)";
    try
    {
        com = new SqlCommand(query, con);
        com.Parameters.AddWithValue("@title", t.getTitle());
        com.Parameters.AddWithValue("@des", t.getDescription());
        com.Parameters.AddWithValue("@uid", t.getUsrID());
        com.Parameters.AddWithValue("@dateadded", t.getDate());
        com.Parameters.AddWithValue("@did", t.getDesID());
        com.Parameters.AddWithValue("@like", 0);
        com.Parameters.AddWithValue("@share", 0);
        com.Parameters.AddWithValue("@visit", 0);
        com.Parameters.AddWithValue("@replyto", t.getReplyToID());
        com.Parameters.AddWithValue("@sno", getDisCount() + 1);
        con.Open();
        com.ExecuteNonQuery();
        con.Close();
        res.Redirect("viewthread.aspx?id=" + t.getDesID());
        return null;
    }
    catch (Exception e)
    {
        con.Close(); return e;
    }
}

The connection string is defined in the constructor of containing class. The problem is that, whenever I try to execute this function, it executes without giving any exception, even not on Visual Studio Debugger Console, and also does not updates the database with new entry provided by the user. When I checked for return value of ExecuteNonQuery() it is returning -1. To me the code seem to be okay or may be I am missing something. Please help me to identify it.

I also tried to execute the query by removing all AddWithValue() statements and making the query pre-defined as

insert into [DisData].[dbo].discussions values('Test','TestDes','TestUID','12-12-2012','sdsd',1,1,1,'sdsd',2)

But problem remains the same...


回答1:


if you have started a transaction somewhere in the code, you should commit it using Transaction.Commit() method. also I can see, you are using a long name for database table including database name, which should be avoided. check if the data is being inserted into DisData database and you are searching in another database...:)




回答2:


For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

More more detail read link MSDN Link



来源:https://stackoverflow.com/questions/10784830/why-executenonquery-returning-1-without-any-exception-and-without-inserting-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!