|
-
Aug 16th, 2012, 03:13 PM
#1
Thread Starter
Junior Member
Insert into sql
Hey guys,
Please can you tell me what
Code:
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = ("Data Source=ANDY-PC\LETTINGS;Initial Catalog=easylet_database;User Id=sa;Password=g3m1n1;")
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO Landlord (Title,FirstName,LastName,Email,Telephone,Telephone2,Fax,Source,Address1,Address2,Address3,Address4,PostCode,Occupation,Bank_Number,Bank_Sortcode,Bank_AccountName,Bank_Company,Notes,Inactive,Date) VALUES (" & Title.Text & "," & FirstName.Text & "," & Surname.Text & "," & Email.Text & "," & Phone1.Text & "," & Phone2.Text & "," & Fax.Text & "," & Source.Text & "," & Ad1.Text & "," & Ad2.Text & "," & Ad3.Text & "," & Ad4.Text & "," & Postcode.Text & "," & Occupation.Text & "," & AccNo.Text & "," & SortCode.Text & "," & AccName.Text & "," & AccCompany.Text & "," & Notes.Text & ")"
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error creating Landlord - Please contact Support")
Finally
con.Close()
End Try
The error pops up: MessageBox.Show("Error creating Landlord - Please contact Support")
I think I have done the sql command syntax wrong.
Cheers.
-
Aug 16th, 2012, 03:36 PM
#2
Re: Insert into sql
Yes you must use 's around text fields
Code:
('" & Title.Text & "',"
-
Aug 17th, 2012, 01:02 PM
#3
Thread Starter
Junior Member
Re: Insert into sql
I seriously cant get this to work, please can you show me using my code on a few of them how its meant to be laid out.
-
Aug 17th, 2012, 01:30 PM
#4
Thread Starter
Junior Member
Re: Insert into sql
Based on what you said,
Code:
Private Sub SaveButton_Click(sender As System.Object, e As System.EventArgs) Handles SaveButton.Click
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = ("Data Source=ANDY-PC\LETTINGS;Initial Catalog=easylet_database;User Id=sa;Password=g3m1n1;")
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO Landlord (Title,FirstName,LastName,Email,Telephone,Telephone2,Fax,Source,Address1,Address2,Address3,Address4,PostCode,Occupation,Bank_Number,Bank_Sortcode,Bank_AccountName,Bank_Company,Notes,Inactive,Date) VALUES ('" & Title.Text & "','" & FirstName.Text & "','" & Surname.Text & "','" & Email.Text & "','" & Phone1.Text & "','" & Phone2.Text & "','" & Fax.Text & "','" & Source.Text & "','" & Ad1.Text & "','" & Ad2.Text & "','" & Ad3.Text & "','" & Ad4.Text & "','" & Postcode.Text & "','" & Occupation.Text & "','" & AccNo.Text & "','" & SortCode.Text & "','" & AccName.Text & "','" & AccCompany.Text & "','" & Notes.Text & "')"
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error creating Landlord - Please contact Support")
Finally
con.Close()
End Try
End Sub
-
Aug 17th, 2012, 02:18 PM
#5
Re: Insert into sql
And what was the result?
You should add a couple of message boxes if you are still having a problem 1 that will show the content of commandText before you try to execute and another in your catch routine that shows what the actual error is
-
Aug 17th, 2012, 02:51 PM
#6
Thread Starter
Junior Member
Re: Insert into sql
Right,
heres my code, please save me before I destroy my life lol!!! Its driving me crazy,
Code:
cmd.CommandText = "INSERT into Landlord (Title,FirstName,LastName,Email,Telephone,Telephone2,Fax,Source,Address1,Address2,Address3,Address4,PostCode,Occupation,Bank_Number,Bank_SortCode,Bank_Accountname,Bank_Company,Notes,Inactive,Notes,AgentID,Date) VALUES ('" & Title.text &"','"& FirstName.text &"', '"& LastName.text"', '" & Email.text & "','" & Telephone & "','" & Telephone2 & "' , '" & Fax & "' , '" & Source & "' , '" & Address1 & "', '" & Address2 & "', '" & Address3 & "' , '" & Address4 & "', '" & PostCode & "' , '" & Occupation & "', '" & Bank_Number & "', '" & Bank_SortCode & "', '" & Bank_Accountname & "' , '" & Bank_Company & "', '" & Notes & "' ,0,AgentIDNumber,GetDate())"
-
Aug 17th, 2012, 02:57 PM
#7
Re: Insert into sql
I can't tell much from your code
You did not say what it is doing
Did you add the message boxes to see what is actually happening and what the error is?
You really should be using parameterized queries rather than building the string that way.
Also note that the 's should only be used around Text and Date fields, never on numeric fields and they do not apply to dates with Access which uses # instead of ' for a date delimiter.
-
Aug 17th, 2012, 02:59 PM
#8
Thread Starter
Junior Member
Re: Insert into sql
I know,
I dont know how to create a SQL stored procedure tho.
MANNNNN lol.
-
Aug 17th, 2012, 03:15 PM
#9
Re: Insert into sql
Example:
Code:
Dim SQLString As String = "INSERT INTO Landlord "
SQLString += "(Title,FirstName,LastName,Email,Telephone,Telephone2,Fax,Source,Address1,Address2,Address3,Address4,PostCode,Occupation,Bank_Number,Bank_Sortcode,Bank_AccountName,Bank_Company,Notes,Inactive,Date)"
SQLString += " VALUES (@Title,@FirstName,@LastName,@Email,@Telephone,@Telephone2,@Fax,@Source,@Address1,@Address2,@Address3,@Address4,@PostCode,@Occupation,@Bank_Number,@Bank_Sortcode,@Bank_AccountName,@Bank_Company,@Notes,@Inactive,@Date"
cmd.CommandText = SQLString
CMD.Parameters.AddWithValue("@Title", Title.Text)
CMD.Parameters.AddWithValue("@FirstName", FirstName.Text)
CMD.Parameters.AddWithValue("@LastName", Surname.Text)
' continue for the rest of your enteries
cmd.ExecuteNonQuery()
I notice that one of your field names is Date which is a reserved word and could lead to issues. You should change that if possible and if not then you should bracket it with []
-
Aug 18th, 2012, 01:08 PM
#10
Thread Starter
Junior Member
Re: Insert into sql
thanks, whats the advantage of using parameters?
-
Aug 18th, 2012, 03:52 PM
#11
Thread Starter
Junior Member
Re: Insert into sql
Also,
I tried like you said using paremeters and still the same error;
Code:
Private Sub SaveButton_Click(sender As System.Object, e As System.EventArgs) Handles SaveButton.Click
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim SQLString As String = "INSERT INTO Landlord "
Dim AID As String = AgentIDNum.AgentIDNumber
Dim GDate As String = GetDateModule.GetDate
Try
con.ConnectionString = ("Data Source=ANDY-PC\LETTINGS;Initial Catalog=easylet_database;User Id=sa;Password=g3m1n1;")
con.Open()
SQLString += "(Title,FirstName,LastName,Email,Telephone,Telephone2,Fax,Source,Address1,Address2,Address3,Address4,PostCode,Occupation,Bank_Number,Bank_Sortcode,Bank_AccountName,Bank_Company,Notes,Inactive,Date)"
SQLString += " VALUES (@Title,@FirstName,@LastName,@Email,@Telephone,@Telephone2,@Fax,@Source,@Address1,@Address2,@Address3,@Address4,@PostCode,@Occupation,@Bank_Number,@Bank_Sortcode,@Bank_AccountName,@Bank_Company,@Notes,@Inactive,@Date"
cmd.CommandText = SQLString
cmd.Parameters.AddWithValue("@Title", Title.Text)
cmd.Parameters.AddWithValue("@FirstName", FirstName.Text)
cmd.Parameters.AddWithValue("@LastName", LastName.Text)
cmd.Parameters.AddWithValue("@Email", Email.Text)
cmd.Parameters.AddWithValue("@Telephone", Telephone.Text)
cmd.Parameters.AddWithValue("@Telephone2", Telephone2.Text)
cmd.Parameters.AddWithValue("@Fax", Fax.Text)
cmd.Parameters.AddWithValue("@Source", Source.Text)
cmd.Parameters.AddWithValue("@Address1", Address1.Text)
cmd.Parameters.AddWithValue("@Address2", Address2.Text)
cmd.Parameters.AddWithValue("@Address3", Address3.Text)
cmd.Parameters.AddWithValue("@Address4", Address4.Text)
cmd.Parameters.AddWithValue("@Postcode", Postcode.Text)
cmd.Parameters.AddWithValue("@Occupation", Occupation.Text)
cmd.Parameters.AddWithValue("@Bank_Number", Bank_Number.Text)
cmd.Parameters.AddWithValue("@Bank_Sortcode", Bank_Sortcode.Text)
cmd.Parameters.AddWithValue("@Bank_Accountname", Bank_Accountname.Text)
cmd.Parameters.AddWithValue("@Bank_Company", Bank_Company.Text)
cmd.Parameters.AddWithValue("@Notes", Notes.Text)
cmd.Parameters.AddWithValue("@Inactive", 0)
cmd.Parameters.AddWithValue("@AgentID", AID)
cmd.Parameters.AddWithValue("@Date", GDate)
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error creating Landlord - Please contact Support")
Finally
con.Close()
Me.Close()
End Try
End Sub
-
Aug 19th, 2012, 08:45 AM
#12
Thread Starter
Junior Member
-
Aug 19th, 2012, 08:56 AM
#13
Re: Insert into sql
Include ex.Message in MessageBox.Show to tell you what exactly the error is.
-
Aug 19th, 2012, 09:12 AM
#14
Thread Starter
Junior Member
Re: Insert into sql
connection property not instalized?
-
Aug 19th, 2012, 10:39 AM
#15
Hyperactive Member
Re: Insert into sql
you need to initialized the connection in your sql command.
Code:
SQLString += "(Title,FirstName,LastName,Email,Telephone,Telephone2,Fax,Source,Address1,Address2,Address3,Address4,PostCode,Occupation,Bank_Number,Bank_Sortcode,Bank_AccountName,Bank_Company,Notes,Inactive,Date)"
SQLString += " VALUES (@Title,@FirstName,@LastName,@Email,@Telephone,@Telephone2,@Fax,@Source,@Address1,@Address2,@Address3,@Address4,@PostCode,@Occupation,@Bank_Number,@Bank_Sortcode,@Bank_AccountName,@Bank_Company,@Notes,@Inactive,@Date"
you must put " con " after the end of the statement like this ("insert into ( table1) values (@table1)" ,con)
also can you just put your SQL string in one just put _ if you want to extend the sentence into nextline
-
Aug 19th, 2012, 10:48 AM
#16
Thread Starter
Junior Member
Re: Insert into sql
So where do I put the con in my statement, sorry I just had a go but couldnt get it in the right place :/
-
Aug 19th, 2012, 10:49 AM
#17
Re: Insert into sql
I thought the _ was no longer necessary under .Net?
Last edited by Españolita; Aug 19th, 2012 at 11:02 AM.
-
Aug 19th, 2012, 10:56 AM
#18
Hyperactive Member
Re: Insert into sql
you must put con at the end of the sql string
Last edited by marniel647; Aug 19th, 2012 at 10:59 AM.
-
Aug 19th, 2012, 11:07 AM
#19
Thread Starter
Junior Member
Re: Insert into sql
thanks for your reply.
It is saying the syntax is wrong. End of statement required.
Code:
SQLString += " VALUES (@Title,@FirstName,@LastName,@Email,@Telephone,@Telephone2,@Username,@Password,@Commission,@Notes,@Date",con)
-
Aug 19th, 2012, 07:23 PM
#20
Hyperactive Member
Re: Insert into sql
 Originally Posted by andybonse2012
thanks for your reply.
It is saying the syntax is wrong. End of statement required.
Code:
SQLString += " VALUES (@Title,@FirstName,@LastName,@Email,@Telephone,@Telephone2,@Username,@Password,@Commission,@Notes,@Date",con)
that's why i want you to combine the 2 Sqlstring just make it one SQLstring statement.
By the way i rewrite your code try this.
Code:
Private Sub SaveButton_Click(sender As System.Object, e As System.EventArgs) Handles SaveButton.Click
Dim con As New SqlConnection
Dim AID As String = AgentIDNum.AgentIDNumber
Dim GDate As String = GetDateModule.GetDate
Try
con.ConnectionString = ("Data Source=ANDY-PC\LETTINGS;Initial Catalog=easylet_database;User Id=sa;Password=g3m1n1;")
con.Open()
Dim cmd As New SqlCommand("INSERT INTO Landlord(Title,FirstName,LastName,Email,Telephone,Telephone2,Fax,Source,Address1,Address2,Address3,Address4,PostCode,Occupation,Bank_Number,Bank_Sortcode,Bank_AccountName,Bank_Company,Notes,Inactive,Date) VALUES (@Title,@FirstName,@LastName,@Email,@Telephone,@Telephone2,@Fax,@Source,@Address1,@Address2,@Address3,@Address4,@PostCode,@Occupation,@Bank_Number,@Bank_Sortcode,@Bank_AccountName,@Bank_Company,@Notes,@Inactive,@Date)",con)
cmd.Parameters.AddWithValue("@Title", Title.Text)
cmd.Parameters.AddWithValue("@FirstName", FirstName.Text)
cmd.Parameters.AddWithValue("@LastName", LastName.Text)
cmd.Parameters.AddWithValue("@Email", Email.Text)
cmd.Parameters.AddWithValue("@Telephone", Telephone.Text)
cmd.Parameters.AddWithValue("@Telephone2", Telephone2.Text)
cmd.Parameters.AddWithValue("@Fax", Fax.Text)
cmd.Parameters.AddWithValue("@Source", Source.Text)
cmd.Parameters.AddWithValue("@Address1", Address1.Text)
cmd.Parameters.AddWithValue("@Address2", Address2.Text)
cmd.Parameters.AddWithValue("@Address3", Address3.Text)
cmd.Parameters.AddWithValue("@Address4", Address4.Text)
cmd.Parameters.AddWithValue("@Postcode", Postcode.Text)
cmd.Parameters.AddWithValue("@Occupation", Occupation.Text)
cmd.Parameters.AddWithValue("@Bank_Number", Bank_Number.Text)
cmd.Parameters.AddWithValue("@Bank_Sortcode", Bank_Sortcode.Text)
cmd.Parameters.AddWithValue("@Bank_Accountname", Bank_Accountname.Text)
cmd.Parameters.AddWithValue("@Bank_Company", Bank_Company.Text)
cmd.Parameters.AddWithValue("@Notes", Notes.Text)
cmd.Parameters.AddWithValue("@Inactive", 0)
cmd.Parameters.AddWithValue("@AgentID", AID)
cmd.Parameters.AddWithValue("@Date", GDate)
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error creating Landlord - Please contact Support")
Finally
con.Close()
Me.Close()
End Try
End Sub
-
Aug 22nd, 2012, 09:59 AM
#21
Re: Insert into sql
You don't need to make the SQL string one long string like that, just makes it hard to read.
You can easily set a Variable using multiple lines to concantonate the values as was done above then when setting up the command use
Code:
Dim cmd As New SqlCommand (SqlString,Con)
Makes for much more readable code
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|