I wrote an insert function for a discussion forum website to insert new discussion threads in database. The function is as follows:
public int createDiscuss(Discussion dis)
{
query = "insert into [MyForums].[dbo].[Discussions](desid, text, date, time, replyto, uid, isPrivate) values(@id, @text, @date, @time, @replyto, @uid, @pvp)";
string did=getDesID();
if (did == null)
return -1;
try
{
com = new SqlCommand(query, con);
com.Parameters.AddWithValue("@id", did);
com.Parameters.AddWithValue("@text", dis.gettext());
com.Parameters.AddWithValue("@date", SqlDateTime.Parse(DateTime.Now.ToString()));
com.Parameters.AddWithValue("@time", SqlDateTime.Parse(DateTime.Now.ToString()));
com.Parameters.AddWithValue("@replyto", dis.getreplyto());
com.Parameters.AddWithValue("@uid", dis.getuid());
if (dis.IsPrivate() == false)
{ com.Parameters.AddWithValue("@pvp", 1); }
else
{ com.Parameters.AddWithValue("@pvp", 2); }
con.Open();
int r=com.ExecuteNonQuery();
if (r <= 0)
{
return -1;
}
con.Close();
}
catch (Exception)
{
return -1;
}
return 0;
}
This function if encounters an error or exception than it return -1 and which is checked by the calling function and then the calling function shows error. The problem is that this insert function is not executing query. When I checked that if the values are properly passing or not to the AddWithValue function as an argument, I found that every value is passed as it was given on the asp.net page. But when control comes to com.ExecuteNonQuery() function. It is returning -1 that is error. Can anyone tell me whats wrong with my code? or how can I check why com.ExecuteNonQuery() not returning any effected row??
This exception showing
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
An exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll but was not handled in user code