Results 1 to 3 of 3

Thread: SQL Insert Into

  1. #1

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    52

    SQL Insert Into

    Hi,

    I have tested the command in SQL Managment studio and i can add a company easily with the insert into command.

    But my code picks up this error:

    Name:  ErrorAddSQL.JPG
Views: 188
Size:  16.6 KB

    Here is my code:

    Code:
            Dim Sqlcon As SqlConnection = New SqlConnection("Data Source=JAMESPC\SUPPORTDB;Initial Catalog=Support_DB;Persist Security Info=True;User ID=user;Password=password")
            Dim SQLcmd As SqlCommand
            Dim SQLcmdFind As SqlCommand
            Dim QueryOpen As String = "insert into Customers (CompanyName, Address, City, County, PostCode, PhoneNumber, Extension, FaxNumber, EmailAddress, Notes, Passwords, Alert, Alerttxt) values ('" & txtnewcust.Text & "','" & txtnewadd.Text & "','" & txtnewcity.Text & "','" & txtnewcou.Text & "','" & txtnewpost.Text & "','" & txtnewphon.Text & "','" & txtnewext.Text & "','" & txtnewfax.Text & "','" & txtnewemail.Text & "','" & chknewalert.CheckState & "','" & txtnewalert.Text & "','" & txtnewnote.Text & "','" & txtnewpass.Text & "')"
    
            Dim QueryFind As String = "select count(Customer) from CompanyName where CompanyName ='" & txtnewcust.Text & "'"
            Dim count As Integer = -1
            'Dim msgblank As DialogResult
            'If txtadd.Text = "" Then
            'msgblank = MessageBox.Show("Please enter an engineers name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            'If msgblank = DialogResult.OK Then
            'Exit Sub
            'End If
            'End If
            Try
                SQLcmd = New SqlCommand(QueryOpen, Sqlcon)
                SQLcmdFind = New SqlCommand(QueryFind, Sqlcon)
                Sqlcon.Open()
                count = Convert.ToInt32(SQLcmdFind.ExecuteScalar())
                If count > 0 Then
                    MessageBox.Show("The customer" & " " & txtnewcust.Text & " " & "already exists in the database", "Duplicate Record", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    Me.Show()
                Else
                    SQLcmd.ExecuteNonQuery()
                    MessageBox.Show("The customer" & " " & txtnewcust.Text & " " & "has been created.", "Customer Created", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Sqlcon.Close()
                End If
            Catch ex As Exception
                MsgBox("Error while creating the new customers record..." & " " & ex.Message)
                Me.Show()
            Finally
                Sqlcon.Close()
    
            End Try
    
        End Sub
    Thanks James

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: SQL Insert Into

    This line looks wrong:

    Dim QueryFind As String = "select count(Customer) from CompanyName where CompanyName ='" & txtnewcust.Text & "'"

    It looks like you have the wrong table name.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    52

    Re: SQL Insert Into

    Quote Originally Posted by Shaggy Hiker View Post
    This line looks wrong:

    Dim QueryFind As String = "select count(Customer) from CompanyName where CompanyName ='" & txtnewcust.Text & "'"

    It looks like you have the wrong table name.
    Again didnt see this got to learn from your mistakes thanks Shaggy Hiker

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width