473,511 Members | 17,164 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Storing data to Access from Visual Studio VB ASP

18 New Member
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


Expand|Select|Wrap|Line Numbers
  1.     Function SubmitRequest() As Boolean
  2.  
  3.         Dim myConn As OleDbConnection
  4.         Dim myCmd As OleDbCommand
  5.         Dim myDr As OleDbDataReader
  6.         Dim RequestTable As New DataTable
  7.         Dim UserID As Integer = CType(Session.Item("UserID"), String)
  8.         Dim Title As String = txtRequestTitle.Text
  9.         Dim Description As String = txtProblemDescription.Text
  10.  
  11.  
  12.  
  13.  
  14.         myConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\Helpdesk\Test\Database.accdb;Persist Security Info=True")
  15.         myConn.Open()
  16.         'myCmd = New OleDbCommand("select ID, Title, ProblemDescription from Request where StaffID=" & UserID & "", myConn)
  17.         'myCmd = New OleDbCommand("INSERT StaffID, Title, ProblemDescription) VALUES ( & UserID & Title & Description" & "", myConn)
  18.         'myCmd = New OleDbCommand("INSERT Request StaffID, Title, ProblemDescription) VALUES " & '"UserID, Title, Description"'"), myConn)
  19.         'myCmd = New OleDbCommand("INSERT INTO Request StaffID, Title, ProblemDescription) VALUES (@UserID, @Title, @Description)", myConn)
  20.  
  21.         myCmd = New OleDbCommand("INSERT INTO Request StaffID, Title, ProblemDescription) VALUES "(UserID, Title, Description), myConn)
  22.         'myDr = myCmd.ExecuteReader
  23.         'RequestTable.Load(myDr)
  24.  
  25.  
  26.  
  27.         Return True
  28.  
  29.  
  30.     End Function
  31.  
Oct 19 '13 #1
29 1940
Rabbit
12,516 Recognized Expert Moderator MVP
The correct format for an insert query is:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tableName (comma delimited field list)
  2. VALUES (comma delimited value list)
Oct 19 '13 #2
NiallP17
18 New Member
Expand|Select|Wrap|Line Numbers
  1. 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'.
Oct 20 '13 #3
Rabbit
12,516 Recognized Expert Moderator MVP
You didn't append your value list correctly.
Oct 21 '13 #4
NiallP17
18 New Member
So I am listing them wrong? How am I suppose to list them?
Oct 21 '13 #5
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.
Oct 21 '13 #6
NiallP17
18 New Member
I'm really sorry but I don't understand what that means
Oct 21 '13 #7
Rabbit
12,516 Recognized Expert Moderator MVP
What I mean is this:
Expand|Select|Wrap|Line Numbers
  1. myCmd = New OleDbCommand("INSERT INTO Request (StaffID, Title, Description) VALUES (" & UserID & ",'" & Title & "','" & Description & "')", myConn)
Oct 22 '13 #8
NiallP17
18 New Member
That line of code worked but now I am getting an error on the line
Expand|Select|Wrap|Line Numbers
  1. 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.
Oct 22 '13 #9
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.
Oct 22 '13 #10
NiallP17
18 New Member
Expand|Select|Wrap|Line Numbers
  1. myCmd = New OleDbCommand("INSERT INTO Request (StaffID, Title, Description) VALUES (" & UserID & ",'" & Title & "','" & Description & "')", myConn)
That is the SQL I am using
Oct 22 '13 #11
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.
Oct 22 '13 #12
NiallP17
18 New Member
It's the same code that is posted in the first comment.
Oct 22 '13 #13
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.
Oct 22 '13 #14
NiallP17
18 New Member
Isn't the SQL in the code?
Oct 22 '13 #15
Rabbit
12,516 Recognized Expert Moderator MVP
Part of the SQL is in the code. The full SQL is what I want to see.
Oct 22 '13 #16
NiallP17
18 New Member
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?
Oct 22 '13 #17
Rabbit
12,516 Recognized Expert Moderator MVP
Expand|Select|Wrap|Line Numbers
  1. x = "world"
  2. string = "bob" + x
My question is: What's in string?

This is the answer you're giving me:
Expand|Select|Wrap|Line Numbers
  1. string = "bob" + x
The answer I'm looking for is:
Expand|Select|Wrap|Line Numbers
  1. helloworld
I need to know what's in the SQL, not the code that builds the SQL. I already have the code.
Oct 23 '13 #18
NiallP17
18 New Member
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.
Oct 23 '13 #19
Rabbit
12,516 Recognized Expert Moderator MVP
The values help, the actual SQL is better.
Oct 23 '13 #20
NiallP17
18 New Member
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
Oct 23 '13 #21
Rabbit
12,516 Recognized Expert Moderator MVP
I need the actual values that you tried when the code failed...
Oct 23 '13 #22
NiallP17
18 New Member
Title was 'Wifi not working' description was 'The wifi is not working'
Oct 23 '13 #23
Rabbit
12,516 Recognized Expert Moderator MVP
Is that with or without the quote?
Oct 23 '13 #24
NiallP17
18 New Member
Without the quotes on both.
Oct 23 '13 #25
Rabbit
12,516 Recognized Expert Moderator MVP
Then I will need to see the SQL. It's most likely formatted incorrectly.
Oct 24 '13 #26
NiallP17
18 New Member
Do you mean what's in the sub when the button is clicked as the code above is just for the function?
Oct 24 '13 #27
Rabbit
12,516 Recognized Expert Moderator MVP
I mean the value in the variable just before it is submitted to the SQL Server.
Oct 24 '13 #28
NiallP17
18 New Member
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.
Oct 24 '13 #29
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):
Expand|Select|Wrap|Line Numbers
  1. '!!!Before this code is executed you should validate the user input!!!
  2.   Dim userID As Integer = CType(Session.Item("UserID"), String)
  3.   Dim title As String = txtRequestTitle.Text
  4.   Dim description As String = txtProblemDescription.Text
  5.  
  6.   Dim connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\Helpdesk\Test\Database.accdb;Persist Security Info=True"
  7.   Dim queryString = "INSERT INTO Request (StaffID, Title, ProblemDescription) VALUES (?, ?, ?)"
  8.   Dim numberOfRowsAffected As Integer = 0
  9.  
  10.   Using connection As New OleDbConnection(connectionString)
  11.     connection.Open()
  12.     Dim command As New OleDbCommand(queryString, connection)
  13.     command.Parameters.Add(userID)
  14.     command.Parameters.Add(title)
  15.     command.Parameters.Add(description)
  16.     numberOfRowsAffected = command.ExecuteNonQuery()
  17.   End Using 
  18. ' you can use the numberOfRowsAffected  
  19. ' to determine if your insert worked correctly
-Frinny
Oct 24 '13 #30

Sign in to post your reply or Sign up for a free account.

Similar topics

63
5827
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...
1
3096
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...
1
2566
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...
0
877
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...
5
2176
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...
0
1724
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...
18
9106
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...
0
7297
jwwicks
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...
1
2156
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...
0
1419
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...
0
7355
Oralloy
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,...
0
7423
jinu1996
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...
0
7510
tracyyun
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...
0
5668
agi2029
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,...
0
4737
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...
0
3225
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...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
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 ...
0
447
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.