ASPX VB.Net OleDb Insert Parameter into Query

Multi tool use
Multi tool use


ASPX VB.Net OleDb Insert Parameter into Query



This is my first time writing in VB.Net for aspx pages.



The problem I having is that the parameter is not going into the query at the line for cmd.Parameters.Add.


cmd.Parameters.Add



The error I am getting is



No value given for one or more required parameters.



on the line


reader = cmd.ExecuteReader;



I have tried:


PARAMETERS



OleDbType.Integer


OleDbType.SmallInt


OleDbType.BigInt



I know the query works as I can place it into MS Access and will run once I add the parameter. But not when I run it in Visual Studio.


Dim reader As OleDbDataReader
Dim cmd As OleDbCommand
Dim SQL As String = "PARAMETERS [@ID] Long; " &
"SELECT tblField.FieldName, " &
"tblField.FieldCaption, " &
"tblField.FieldMinCharNum, " &
"tblField.FieldMaxCharNum, " &
"tblField.FieldDefault, " &
"tblField.FieldSection, " &
"tblField.FirstQuestion, " &
"tblField.FieldDescription, " &
"tblField.FieldRegEx " &
"FROM tblField " &
"WHERE tblField.FieldID = [@ID];"

cmd = New OleDbCommand(SQL, Connection.Connection)
cmd.Parameters.Add("[@ID]", OleDbType.Integer).Value = ID

reader = cmd.ExecuteReader



I have a work around to make it work by just pre-inserting the parameter into the SQL string. But I want to make this work for other areas of the page that are yet to be written. Where user inputs are coming back into database so inputs are sanitised.



Thanks to everyone.





Get rid of the first line of the SQL code. All you need is the SELECT statement. Also get rid of the brackets around the parameter. Brackets are used to force something to be interpreted as an identifier so you only put them around identifiers and, even then, they are only required if the text would not be interpreted as an identifier otherwise, i.e. it is a reserved word or contains spaces or other special characters.
– jmcilhinney
Jul 1 at 5:25



SELECT





What is the data type of the column in the database? That's what determines what data type you use for the parameter. It's not arbitrary and there's no need to guess. As the documentation states, BigInt is for 64-bit numbers, Integer is for 32-bit numbers and SmallInt is for 16-bit numbers.
– jmcilhinney
Jul 1 at 5:31


BigInt


Integer


SmallInt





@jmcilhinney The field is an Access AutoNumber so 32-bit which is why I tried Integer first but changed it just to make sure that I am not making a mistake.
– Bullfrog
Jul 1 at 10:37





@jmcilhinney The around the variables where originally not there and it made no difference. I added them to make sure. And the PARAMETERS was added after it failed the first time to see if that would fix it.
– Bullfrog
Jul 1 at 10:39



PARAMETERS





The code you posted is obviously wrong. Edit your question and post the code that you think it should be and we'll deal with that.
– jmcilhinney
Jul 1 at 10:52




2 Answers
2



OLEDB doesn't use @ to identify parameters. It uses ? and allocates parameters in the order they appear in the SQL amend your code to...


Dim reader As OleDbDataReader
Dim cmd As OleDbCommand
Dim SQL As String = "SELECT tblField.FieldName, " &
"tblField.FieldCaption, " &
"tblField.FieldMinCharNum, " &
"tblField.FieldMaxCharNum, " &
"tblField.FieldDefault, " &
"tblField.FieldSection, " &
"tblField.FirstQuestion, " &
"tblField.FieldDescription, " &
"tblField.FieldRegEx " &
"FROM tblField " &
"WHERE tblField.FieldID = ?"

cmd = New OleDbCommand(SQL, Connection.Connection)
cmd.Parameters.Add("?", OleDbType.Integer).Value = ID

reader = cmd.ExecuteReader





that has worked thank you
– Bullfrog
Jul 3 at 22:18



I don't understand why your mentioning SQL are you retrieving the data from SQL Query or are you going to insert data into the table.



your using Dim cmd As OleDbCommand means use to insert the input values into the database like see below sample code.


Dim cmd As OleDbCommand


query = "INSERT INTO ds.students (ID,NAME,PIC)" & _
"VALUES (@ID,@NAME,@PIC);"
Dim cmd As OracleCommand = New OracleCommand(query, con)
cmd.Parameters.Add("@ID", Convert.ToInt32(TextBox1.Text))
cmd.Parameters.Add("@NAME", Convert.ToString(TextBox2.Text))
cmd.Parameters.Add("@PIC", arrImage)
con.Open()
cmd.ExecuteNonQuery()
con.Close()



you can try this also


Using cn As OracleConnection = New OracleConnection(connectionString)

cn.Open()

Using cmd As OracleCommand = New OracleCommand()

Const sql As String = "Insert into test_table (val1, val2) values (:var1, :var2)"
cmd.Connection = cn
cmd.Parameters.Add(New OracleParameter("var1", TxtField1.Text))
cmd.Parameters.Add(New OracleParameter("var2", TxtField2.Text))
cmd.CommandText = sql
cmd.ExecuteNonQuery()

End Using

End Using



if you want to insert the values into the database change your code according to given samples.



Hope this will help you.





You'r first example won't work against Oracle as it uses colons to identify parameters. The @ is used by SQL/Server.
– Ciarán
Jul 3 at 7:53






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

YLEKTEbUBL11tqkq
2,k NMd1Txh T0Kf a9ek7kx1XQ,BV692WD,AcOyE0RGgdvDFjPm AJl3vG6Vv23nY8C7c7TgjXhStFW3puA ytP6BvlFnf0m5poeOhG 6Z

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications