Results 1 to 3 of 3

Thread: [RESOLVED] Inserting to SQL database from VB App - three rows instead of one.

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2013
    Posts
    11

    Resolved [RESOLVED] Inserting to SQL database from VB App - three rows instead of one.

    I am new to VB, and am trying to create a VB app wherby one of the features enables an operator to insert an SQL row.
    The below code is working, although, instead of one row being inserted, three rows are being inserted. Any ideas are appreciated. TIA

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    If TextBox1.Text() = "" Or TextBox2.Text = "" Then
    MessageBox.Show("Textbox cannot be empty. Please Check")
    Else
    Try
    Dim connectionString As String = "data source=[ip address];initial catalog=Software_Licenses;persist security info=True;user id=id;password=password"
    Dim SqlConnection As New SqlConnection(connectionString)
    Dim sqlcommand As New SqlCommand("INSERT INTO Software_Licenses.dbo.MasterList (Software,License) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "')", SqlConnection)
    Dim adapter As New SqlDataAdapter(sqlcommand)
    Dim ds As New DataSet("MasterList")
    Dim dataTable As New DataTable("MasterList")
    ds.Tables.Add("MasterList")
    adapter.Fill(ds.Tables.Item("MasterList"))
    adapter.Fill(dataTable)
    SqlConnection.Open()
    sqlcommand.ExecuteNonQuery()
    SqlConnection.Close()
    Catch ex As Exception

    End Try
    MessageBox.Show("You have successfully updated the License Master List", "Success")
    Close()

    End If
    End Sub

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,834

    Re: Inserting to SQL database from VB App - three rows instead of one.

    All three of these are adding rows:

    ds.Tables.Add("MasterList")
    adapter.Fill(ds.Tables.Item("MasterList"))

    adapter.Fill(dataTable)

    SqlConnection.Open()

    Take your pick and keep one

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2013
    Posts
    11

    Re: Inserting to SQL database from VB App - three rows instead of one.

    Quote Originally Posted by TysonLPrice View Post
    All three of these are adding rows:

    ds.Tables.Add("MasterList")
    adapter.Fill(ds.Tables.Item("MasterList"))

    adapter.Fill(dataTable)

    SqlConnection.Open()

    Take your pick and keep one
    Thank you very much... you are the man!

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