473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Some Help on Insert

Hi All

I have the following code..

Basically I have two fields on my form Fname and Sname and I want to insert the contents of these two text field into my SQL databse.. But I keep on getting the following error.

"Error executing SQL: The name 'Firstname' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted.

Can anyone help.


Expand|Select|Wrap|Line Numbers
  1. Private Sub btnExecute_Click(ByVal sender As Object, _
  2.  ByVal e As System.EventArgs) Handles btnExecute.Click
  3.         Dim oCmd As SqlClient.SqlCommand
  4.         Dim strConn As String
  5.         Dim Firstname As String
  6.         Dim secondname As String
  7.  
  8.         Try
  9.                        strConn &= "Data Source=MyServer;"
  10.             strConn &= "Initial Catalog=MyDatabase;"
  11.             strConn &= "User ID=MyUsername;"
  12.             strConn &= "Password=MyPassword;"
  13.  
  14.                         oCmd = New SqlClient.SqlCommand()
  15.                         oCmd.Connection = _
  16.                         New SqlClient.SqlConnection(strConn)
  17.                        oCmd.Connection.Open()
  18.                        Firstname = Fname.Text
  19.                         secondname = Sname.Text
  20.  
  21.             oCmd.CommandText = String.Format("Insert into Information(First_Name, Last_Name) Values  ([Firstname], [Secondname])")
  22.                         txtRows.Text = _
  23.              oCmd.ExecuteNonQuery().ToString()
  24.  
  25.             MessageBox.Show("SQL statement succeeded", _
  26.              "btnExecute_Click()")
  27.  
  28.  
  29.             oCmd.Connection.Close()
  30.  
  31.         Catch oExcept As Exception
  32.             txtRows.Text = 0.ToString()
  33.             MessageBox.Show("Error executing SQL: " & _
  34.              oExcept.Message, "btnExecute_Click()")
  35.  
  36.         End Try
  37.     End Sub
  38.  
  39. End Class
Regards

Douglas Bell
Mar 26 '07 #1
2 920
Try this:

oCmd.CommandText = String.Format("Insert into Information(First_Name, Last_Name) Values ( '" + Firstname + "', '" + Secondname + "')")

or

oCmd.CommandText = String.Format("Insert into Information(First_Name, Last_Name) Values ('{0}','{1}'", Firstname , Secondname)
Mar 26 '07 #2
Try this:

oCmd.CommandText = String.Format("Insert into Information(First_Name, Last_Name) Values ( '" + Firstname + "', '" + Secondname + "')")

or

oCmd.CommandText = String.Format("Insert into Information(First_Name, Last_Name) Values ('{0}','{1}'", Firstname , Secondname)

Perfect Cheers for the help..
Mar 26 '07 #3

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

Similar topics

15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
0
by: BW | last post by:
Please ignore the rest of the code, except for the highlighted part (or the line 'ent1=Entry(topf, width=25)' to line 'ent1.insert(INSERT, wrong, if you cannot see the color). You can copy this into...
2
by: Spanky | last post by:
Thanks for any help in advance! I have this order form where you add rows as you need them. The routine to add fields is working fine. I am trying to add the ability to delete rows if you...
0
by: Tony Johansson | last post by:
Hello! Below is a small program using the adaptor pattern. We have four classes these are: PegAdapter, RoundPeg, SquarePeg and a class TestPegs acting like a client In class PegAdapter has...
4
by: louise raisbeck | last post by:
I have this scenario (simplified) function addnewdata () { check for partial match already in db for information entered by user if (partialmatch succeeds) { open new window aspx page (using...
34
by: Karam Chand | last post by:
Hello I have been working with Access and MySQL for pretty long time. Very simple and able to perform their jobs. I dont need to start a flame anymore :) I have to work with PGSQL for my...
2
kiss07
by: kiss07 | last post by:
Dear debas and friends I attend interview last week . some qusetions below any body..help. 1) How can i find out how many times trigger is executed? which one trigger body....
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
2
by: hisham123 | last post by:
I had the table with 5 columns and many rows. Now i want to delete the records which meets column1,column2,column3 are same and keep one record on it. Create Table T1 (Number int,name1...
2
by: cephal0n | last post by:
I have this peroblem thats really bugging me for days, please have a patience to read it and help me find the probplem because I knew I missed it and just cant tell where. I have a table named...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.