i am having an sql query in catch block. the code is:

Dim str_exception As String = ex.Message 
 

Dim sql5 As String = "insert into table_data(type,description) values('ERROR'," + str_exception + ")"  
 

Dim cmd_sql5 As New SqlCommand(sql5, connection) 
cmd_sql5.ExecuteScalar()

it throws an exception at cmd_sql5.ExecuteScalar(). the exception is - incorrect syntax near 'records' . i tried adding single quotes like ' incorrect syntax near 'records' .' .but still it is giving me the same error. I think it is because of the quotes near records. this exception is thrown somewhere else in the code and then it comes to cath block. i want to save the exception in the database.

how can i get rid of this error?

Recommended Answers

All 3 Replies

Use ExecuteNonQuery() method, instead of ExecuteScalar().
ExecuteScalar() method in only used to get, set only one value.

You might also want to change your query to

Dim sql5 As String = "insert into table_data(type,description) values('ERROR','" + str_exception + "')"

\
(Added single quotes to str_exception)

you may want to try this:

Dim sql5 As String = "insert into table_data(type,description) values('ERROR','" + str_exception + "')"

Dim cmd_sql5 As New SqlCommand(sql5, connection)

connection.Open()
	cmd_sql5.ExecuteNonQuery()
connection.Close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.