I am trying to store three values that I take from my web form and place them into a database that I have.
At the minute when I try this, the (UserID, Title, Description) line doesn't accept and the Title and Description are underlined in blue. I have tried putting in & instead of the comma but then it takes it all as one long string.
I have tried so many ways to try and get it to place the right data into the right space in my database and I cannot seem to work out what I'm doing wrong.
Any help would be greatly appreciated.
Many thanks -
Function SubmitRequest() As Boolean
-
-
Dim myConn As OleDbConnection
-
Dim myCmd As OleDbCommand
-
Dim myDr As OleDbDataReader
-
Dim RequestTable As New DataTable
-
Dim UserID As Integer = CType(Session.Item("UserID"), String)
-
Dim Title As String = txtRequestTitle.Text
-
Dim Description As String = txtProblemDescription.Text
-
-
-
-
-
myConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\Helpdesk\Test\Database.accdb;Persist Security Info=True")
-
myConn.Open()
-
'myCmd = New OleDbCommand("select ID, Title, ProblemDescription from Request where StaffID=" & UserID & "", myConn)
-
'myCmd = New OleDbCommand("INSERT StaffID, Title, ProblemDescription) VALUES ( & UserID & Title & Description" & "", myConn)
-
'myCmd = New OleDbCommand("INSERT Request StaffID, Title, ProblemDescription) VALUES " & '"UserID, Title, Description"'"), myConn)
-
'myCmd = New OleDbCommand("INSERT INTO Request StaffID, Title, ProblemDescription) VALUES (@UserID, @Title, @Description)", myConn)
-
-
myCmd = New OleDbCommand("INSERT INTO Request StaffID, Title, ProblemDescription) VALUES "(UserID, Title, Description), myConn)
-
'myDr = myCmd.ExecuteReader
-
'RequestTable.Load(myDr)
-
-
-
-
Return True
-
-
-
End Function
-
29 1940 Rabbit 12,516
Recognized Expert Moderator MVP
The correct format for an insert query is: - INSERT INTO tableName (comma delimited field list)
-
VALUES (comma delimited value list)
- myCmd = New OleDbCommand("INSERT INTO Request (StaffID, Title, Description) VALUES "(UserID, Title, Description), myConn)
Do you mean like that? Because Title, Description are underlined blue as 'Too many arguments to 'Public ReadOnly Default Property Chars(index as integer) As Char'.
Rabbit 12,516
Recognized Expert Moderator MVP
You didn't append your value list correctly.
So I am listing them wrong? How am I suppose to list them?
Rabbit 12,516
Recognized Expert Moderator MVP
The commas and parentheses need to be a part of the string. The variables you want to append need to be outside of the string.
I'm really sorry but I don't understand what that means
Rabbit 12,516
Recognized Expert Moderator MVP
What I mean is this: - myCmd = New OleDbCommand("INSERT INTO Request (StaffID, Title, Description) VALUES (" & UserID & ",'" & Title & "','" & Description & "')", myConn)
That line of code worked but now I am getting an error on the line - myDr = myCmd.ExecuteReader
It is saying 'Syntax Error (missing operator) in query expression 'the wifi is not working')'.
The expression is what i am trying to store as the description in my table.
Rabbit 12,516
Recognized Expert Moderator MVP
Please output the full SQL query it is attempting to run. The most likely cause is that you have quotes in your string which you need to escape.
- myCmd = New OleDbCommand("INSERT INTO Request (StaffID, Title, Description) VALUES (" & UserID & ",'" & Title & "','" & Description & "')", myConn)
That is the SQL I am using
Rabbit 12,516
Recognized Expert Moderator MVP
That's your code. I want to see the SQL string that is being passed to the server.
It's the same code that is posted in the first comment.
Rabbit 12,516
Recognized Expert Moderator MVP
I know. I want to see the SQL that is passed to the server, not the code. I don't want to see the code because that doesn't tell me what is passed to the server.
Isn't the SQL in the code?
Rabbit 12,516
Recognized Expert Moderator MVP
Part of the SQL is in the code. The full SQL is what I want to see.
I must be missing some then because that's all I have. I'm new to this so I'm not 100% sure how to word it all. What exactly am I missing?
Rabbit 12,516
Recognized Expert Moderator MVP - x = "world"
-
string = "bob" + x
My question is: What's in string?
This is the answer you're giving me:
The answer I'm looking for is:
I need to know what's in the SQL, not the code that builds the SQL. I already have the code.
Do you mean what values I am trying to store? Because I get the values from 2 text boxes and the StaffID from a session variable I set up.
Rabbit 12,516
Recognized Expert Moderator MVP
The values help, the actual SQL is better.
The values are from 2 text boxes, the user enters the title of their problem in the title field and the description of their problem in the description text box. These values are then stored in the variables Title and Description
Rabbit 12,516
Recognized Expert Moderator MVP
I need the actual values that you tried when the code failed...
Title was 'Wifi not working' description was 'The wifi is not working'
Rabbit 12,516
Recognized Expert Moderator MVP
Is that with or without the quote?
Without the quotes on both.
Rabbit 12,516
Recognized Expert Moderator MVP
Then I will need to see the SQL. It's most likely formatted incorrectly.
Do you mean what's in the sub when the button is clicked as the code above is just for the function?
Rabbit 12,516
Recognized Expert Moderator MVP
I mean the value in the variable just before it is submitted to the SQL Server.
Both variables are empty until the user inputs values from the text boxes and when they click the button it stores them in said variables.
Frinavale 9,735
Recognized Expert Moderator Expert
The most obvious thing that jumps out at me is that you are attempting to use the ExecuteReader method but you should be using the ExecuteNonQuery method to do inserts and updates.
You should also be using the OleDbCommand.Parameters property to pass user-provided values into your query. There are a lot of reasons to use parameters and I recommend that you research this topic.
For example (this code is not tested and is meant to be a guideline for you to understand what I am talking about): -
'!!!Before this code is executed you should validate the user input!!!
-
Dim userID As Integer = CType(Session.Item("UserID"), String)
-
Dim title As String = txtRequestTitle.Text
-
Dim description As String = txtProblemDescription.Text
-
-
Dim connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\Helpdesk\Test\Database.accdb;Persist Security Info=True"
-
Dim queryString = "INSERT INTO Request (StaffID, Title, ProblemDescription) VALUES (?, ?, ?)"
-
Dim numberOfRowsAffected As Integer = 0
-
-
Using connection As New OleDbConnection(connectionString)
-
connection.Open()
-
Dim command As New OleDbCommand(queryString, connection)
-
command.Parameters.Add(userID)
-
command.Parameters.Add(title)
-
command.Parameters.Add(description)
-
numberOfRowsAffected = command.ExecuteNonQuery()
-
End Using
-
' you can use the numberOfRowsAffected
-
' to determine if your insert worked correctly
-Frinny
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Jerome |
last post by:
Hi, I'm a bit confused ... when would I rather write an database
application using MS Access and Visual Basic and when (and why) would I
rather write it using Visual Studio .Net?
Is it as easy...
|
by: Dave Taylor |
last post by:
I'm trying to create a simple Schema and associated document in Visual
Studio that will store information regarding units and conversion (meters,
feet, inches, degrees C, F, K, etc.)
I create a...
|
by: Johann Blake |
last post by:
I am looking for a good solution on how to implement data access in an
application so that there is a clean separation between the data access
layer, the business layer and the GUI layer. I am...
|
by: Doug |
last post by:
Hi
I have been working through Scott Mitchell's book on asp.net in 24 hours and
found the book very informative - within the web matrix API.
However when I try to move those new skills into...
|
by: hfk0 |
last post by:
Hi,
I'm new to ASP.net, SQL Server and visual studio.net, and I'm having
problem inserting and storing data from a web form to a SQL database.
I created a simple ASP.NET web form, a simple SQL...
| |
by: Kevin G. Anderson |
last post by:
What: CAUG Meeting - Visual Studio 2005 Tools for Office
When: Thursday, April 27, 2006, 6PM
Who: Jacob Cynamon, Microsoft
Where: The Information Management Group
200 W. Monroe St. Suite...
|
by: surfrat_ |
last post by:
Hi,
I am having the following problems in getting Microsoft Visual Studio
2005 Professional to link to an Access .mdb
database. Please help me to sort this out.
Problem 1:
The Microsoft...
|
by: jwwicks |
last post by:
Introduction
This tutorial describes how to use Visual Studio to create a new C++ program, compile/run a program, resume work on an existing program and debug a program. It is aimed at the...
|
by: Puja Patel |
last post by:
hi all,
am not sure if this is the right place for this post.
I created a website on .net framework 2.0 using visual studio 2005 and web
service software factory. I created all my business...
|
by: Ganesh Kamthe |
last post by:
Hi,
I have 2 computer conncted. I have set one as server & other as client. So my client pc is not responding for Visual studio 2008. But my server PC supports ot in good manner.
So can use...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |