Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to insert data in tables using vb.net,but before inserting checks duplication
Posted

Please i would ask you to post in your efforts first and then ask for assistance as connectivity is the basic along with DML which is thought in colleges or class as part of .NET....

Now coming to your query

use a primary key constraint with your table for the value which you want to keep unique
now coming to the connectivity part hers the code
Imports System.Data.SqlClient 'use this namespace

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' it will first check whether the textbox contains data or not
        If TextBox1.TextLength = 0 Then
            'if textbox does not contain data then prompt the user to enter data
            'with the help of messagebox
            MessageBox.Show("value missing")
        Else
            'creating the object of sqlconnection class and setting its
            'connectionstring property
            Dim con As SqlConnection = New SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True")
            'open the connection
            con.Open()
            'set the command by creating the object of sqlcommand class
            Dim cmd As SqlCommand = New SqlCommand("insert into mytable values('" + TextBox1.Text + "')", con)
            'execute the query
            Dim x As Integer = cmd.ExecuteNonQuery()
            If (x <> 0) Then
                'once the data is added display this messagebox to user
                MessageBox.Show("data added")
            End If
            'close the connection in the end
            con.Close()
        End If
    End Sub
End Class

Do rate my answer once you find it useful
Thanks & Regards
Radix :rose:
 
Share this answer
 
v2
I M USING ACCESS DATABASE. showing error on cmd.eecutenonquery().
 
Share this answer
 
Comments
Kschuler 23-Feb-11 17:05pm    
what is the error?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900