Results 1 to 21 of 21

Thread: Insert into sql

  1. #1
    Junior Member
    Join Date
    Aug 12
    Posts
    31

    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.

  2. #2
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,957

    Re: Insert into sql

    Yes you must use 's around text fields
    Code:
    ('" & Title.Text & "',"

  3. #3
    Junior Member
    Join Date
    Aug 12
    Posts
    31

    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.

  4. #4
    Junior Member
    Join Date
    Aug 12
    Posts
    31

    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

  5. #5
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,957

    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

  6. #6
    Junior Member
    Join Date
    Aug 12
    Posts
    31

    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())"

  7. #7
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,957

    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.

  8. #8
    Junior Member
    Join Date
    Aug 12
    Posts
    31

    Re: Insert into sql

    I know,

    I dont know how to create a SQL stored procedure tho.

    MANNNNN lol.

  9. #9
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,957

    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 []

  10. #10
    Junior Member
    Join Date
    Aug 12
    Posts
    31

    Re: Insert into sql

    thanks, whats the advantage of using parameters?

  11. #11
    Junior Member
    Join Date
    Aug 12
    Posts
    31

    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

  12. #12
    Junior Member
    Join Date
    Aug 12
    Posts
    31

    Re: Insert into sql

    Anyone?

  13. #13
    Fanatic Member
    Join Date
    Dec 07
    Location
    Albacete, españa
    Posts
    579

    Re: Insert into sql

    Include ex.Message in MessageBox.Show to tell you what exactly the error is.
    A fun card game written in VBA within Excel Tri Peaks

  14. #14
    Junior Member
    Join Date
    Aug 12
    Posts
    31

    Re: Insert into sql

    connection property not instalized?

  15. #15
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 10
    Location
    MSDN Library
    Posts
    256

    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

  16. #16
    Junior Member
    Join Date
    Aug 12
    Posts
    31

    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 :/

  17. #17
    Fanatic Member
    Join Date
    Dec 07
    Location
    Albacete, españa
    Posts
    579

    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.
    A fun card game written in VBA within Excel Tri Peaks

  18. #18
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 10
    Location
    MSDN Library
    Posts
    256

    Re: Insert into sql

    you must put con at the end of the sql string

  19. #19
    Junior Member
    Join Date
    Aug 12
    Posts
    31

    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)

  20. #20
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 10
    Location
    MSDN Library
    Posts
    256

    Re: Insert into sql

    Quote Originally Posted by andybonse2012 View Post
    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

  21. #21
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,957

    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
  •