0

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.

1
  • 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. Commented Apr 7, 2020 at 16:12

2 Answers 2

1

I wanted to write a comment but there is not enough reputation, I apologize. In this line you do not have spaces before the variable TableName

"INSERT INTO" + TableName + "(ErrorType, StackTrace, OccuranceTime)";

In the end, you get it

"INSERT INTOTableName(ErrorType...'

Also, you do not have the square brackets '[~]' in the table specification

@"INSERT INTO [ {TableName} ]..."

Also check your connectionString for errors, and does catch throw anything?

If this does not help, write, I will try to help :)

Sign up to request clarification or add additional context in comments.

1 Comment

I have included the space after INTO as well as the brackets around TableName, but I'm still getting the same null reference as I was before. I have checked to ensure my connection string is correct.
1

Look at the exception message. Your SQL is wrong - it is missing a space:

string query = "INSERT INTO " + TableName + @"(ErrorType, StackTrace, OccuranceTime)
Values (@ErrorType, @StackTrace, @OccuranceTime, @ProjectName)";

See the space after the INTO ? As a side note, it is usually a bad idea to parameterize the table name; that seems very... unusual. If you must do that, it may be worth comparing it against a list of known-permitted values (or perhaps take an enum on the API, and worry about the name inside the method), and you should also use [] syntax:

string tableName  = GetTableName(errorTableEnumThing);
string query = "INSERT INTO [" + tableName + @"](ErrorType, StackTrace, OccuranceTime)
Values (@ErrorType, @StackTrace, @OccuranceTime, @ProjectName)";

2 Comments

Hello Marc, I have included the space after INTO as well as the brackets around TableName, but I'm still getting the same null reference as I was before. I have checked to ensure my connection string is correct.
@DominikWillaford you realize this is the first time you've mentioned a null reference, right? What line?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.