Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to retrieve SQL Query (last record id) + Execute it in ASP.NET & concatenate (append/prepend) it with a String
    primarykey
    data
    text
    <p>I'm trying to retrieve the last ID, in a table, using SQL query and then append the result together with a TextBox value (which is a String):</p> <pre class="lang-vb prettyprint-override"><code>Dim searchforID = New OleDbCommand("select ID from [table1] where ID = (select max(id) from [table1])", con) Dim variable1 = searchforID + TextBox1.Text </code></pre> <p>But it throws this error:</p> <blockquote> <p>Compiler Error Message: BC30452: Operator '+' is not defined for types 'System.Data.OleDb.OleDbCommand' and 'String'.</p> </blockquote> <p><BR><h2>Trying to fix it</h2> -- So i tried to convert the returned record ID like this:</p> <pre class="lang-vb prettyprint-override"><code>Dim variable1 = Convert.ToString(searchforID) + TextBox1.Text </code></pre> <p>There is no error now but unfortunately the result returned is "System.Data.OleDb.OleDb" + the textbox value, as this example:<BR> <strong>System.Data.OleDb.OleDbtest</strong></p> <p>-- I too thought to convert the returned record ID to integer throws an error:<br></p> <blockquote> <p>Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Data.OleDb.OleDbCommand' to type 'System.IConvertible'.</p> </blockquote> <h3>So why does retrieving the last record ID fail? Might my SQL query be false?</h3> <p>Thank you.</p> <hr> <h1>So straight forward solution for problem if someone may find it useful:</h1> <pre class="lang-asp prettyprint-override"><code>Dim searchforID = New OleDbCommand("select MAX(ID) from [table1]", con) Dim variable1 = searchforID.ExecuteScalar() &amp; TextBox1.Text </code></pre> <p>Also the answers and comments below have useful information.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload