I'm currently working on writing an error logging program that will take a connection string and a table name and insert the data that I provide it from other programs into a database. The problem that I'm facing though is that my method doesn't return any values and nothing is loaded into the database. This is a standard class library as well
public string ErrorLevelOne(string ConnectionString, string TableName, string ProjectName, string exception, string stackTrace)
{
try
{
SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open();
string query = "INSERT INTO" + TableName + "(ErrorType, StackTrace, OccuranceTime)";
query += "Values (@ErrorType, @StackTrace, @OccuranceTime, @ProjectName)";
SqlCommand command = new SqlCommand(query, conn);
command.Parameters.AddWithValue("@ErrorType", exception);
command.Parameters.AddWithValue("@StackTrace", stackTrace);
command.Parameters.AddWithValue("@OccuranceTime", DateTime.Now);
command.Parameters.AddWithValue("@ProjectName", ProjectName);
command.ExecuteNonQuery();
conn.Close();
return "Error has been posted into the database";
}
catch(Exception err)
{
Debug.WriteLine("Error in connecting or inserting data into " + TableName + ": " + err.Message);
return "Error hasn't been posted into the database";
}
}
Any help or suggestions would be greatly appreciated.
The problem that I'm facing though is that my method doesn't return any values and nothing is loaded into the database;command.ExecuteNonQuery()would return how many rows were affected, you're not assigning anything to this. Are there any errors, have you debugged your code, please edit your post.