Results 1 to 5 of 5

Thread: Record Repeat(HELP)

  1. #1

    Thread Starter
    Member bestex's Avatar
    Join Date
    Jan 2010
    Posts
    52

    Record Repeat(HELP)

    Hi i build a code that can insert my records to mysql database.. the codes are correct it can save records to the mysql database. Now i want to know is what code will i used that will show an error if the records already exist, so it will not make the same records again. here is my code for my INSERT code for mysql database
    Code:
    Imports MySql.Data.MySqlClient
    Imports System.Data
    
    Public Class Form2
        Dim connection As MySqlConnection
    
    
        Private Sub btnlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
            connection = New MySqlConnection()
    
            connection.ConnectionString = "server=localhost; user id=root; password=phoenix0931; database=accounts;"
            Try
                connection.Open()
    
    
            Catch MyError As MySqlException
                MessageBox.Show("Error in Connection to database; " & MyError.Message)
                Textbox1.Text = ""
                Textbox2.Text = ""
                Textbox1.Focus()
            End Try
            Dim myAdapter As New MySqlDataAdapter
    
            Dim sqlquery = "INSERT INTO users (Username, Password) Values('" & Textbox1.Text & "', '" & Textbox2.Text & "' )"
    
            Dim command As New MySqlCommand
            command.Connection = connection
            command.CommandText = sqlquery
    
            myAdapter.InsertCommand = command
            Dim mydata As MySqlDataReader
            mydata = command.ExecuteReader()
    Need help please..

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Record Repeat(HELP)

    There is no magic. If you want to know whether specific data already exists in the database then you execute a query to retrieve it. If the query returns any data then it exists and if it doesn't then it doesn't. It's just like if you were keeping a list on paper: you look for the value in the list and if you find it then you don't add it again but if you don't find it then you add it. Programming doesn't exist in a vacuum. Programming generally models real-world processes. Whatever you would do in the real world is pretty much what you do in programming.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member bestex's Avatar
    Join Date
    Jan 2010
    Posts
    52

    Re: Record Repeat(HELP)

    im only a beginner can you give me a code and explain to me so i can understand it well? please if you dont mind?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Record Repeat(HELP)

    There are lots and lots of examples of querying databases using VB.NET. There are many on this forum and many more besides. Look first, ask questions later. You should start with the Database FAQ link in my signature.
    Last edited by jmcilhinney; Oct 3rd, 2010 at 09:46 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member bestex's Avatar
    Join Date
    Jan 2010
    Posts
    52

    Re: Record Repeat(HELP)

    sir please help me with this code. i will review the code you will give so i will understand,,

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