Click here to Skip to main content
15,742,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to check in my program that plot no already exist then it should display message that"Record already exist" if not then insert the record, i used procedure in lost focus event in textbox plotno.

Code:
VB
Private Sub validateplotno()
        Dim plotnumber As String = ""
        Using cnn As New SqlClient.SqlConnection("Data Source=toshiba-pc;Initial Catalog=VRINDAVAN;User ID=sa;password=zion123")
            Using cmd As SqlClient.SqlCommand = cnn.CreateCommand()

                cmd.CommandText = "SELECT 1 FROM plotmaster WHERE plotno = @plotno"
                cmd.Parameters.AddWithValue("@plotno", plotnumber)
                cnn.Open()
                Dim exists As String = cmd.ExecuteScalar().ToString()'here shows Object reference not set to an instance of an object."}

                If exists = "1" Then
                    MessageBox.Show(plotnumber & " exists already.")
                End If
            End Using
        End Using

    End Sub
Posted
Updated 19-Oct-13 2:03am
v2

1 solution

Replace the below line
VB
Using cmd As SqlClient.SqlCommand

with this
VB
Using cmd As New SqlClient.SqlCommand
 
Share this answer
 
Comments
Member 10192835 20-Oct-13 4:22am    
Dear sir,

now i have changed the code , in this coding it's check that data is already exist, but when it's get the new data it's doesn't add the record and showing error.object reference not set to an instance of object.
the Code:-
Private Sub validateplotno()
Dim plotnumber As String = ""
Dim cnn As New SqlConnection("Data Source=toshiba-pc;Initial Catalog=VRINDAVAN;User ID=sa;password=zion123")
cnn.Open()
Dim command As New SqlClient.SqlCommand()
command.Connection = cnn
command.CommandText = "SELECT plotno FROM plotmaster WHERE plotno = " & txtplno.Text & ""
plotnumber = command.ExecuteScalar().ToString
MessageBox.Show(plotnumber & " exists already.")
txtplno.Text = ""
txtplno.Focus()
cnn.Close()
End Sub

for inserting the new data i added the following line in the code:--

Private Sub validateplotno()
Dim plotnumber As String = ""
Dim cnn As New SqlConnection("Data Source=toshiba-pc;Initial Catalog=VRINDAVAN;User ID=sa;password=zion123")
cnn.Open()
Dim command As New SqlClient.SqlCommand()
command.Connection = cnn
command.CommandText = "SELECT plotno FROM plotmaster WHERE plotno = " & txtplno.Text & ""
If plotnumber = command.ExecuteScalar().ToString Then
MessageBox.Show(plotnumber & " exists already.")
txtplno.Text = ""
txtplno.Focus()
cnn.Close()
Else
dim command As New SqlClient.SqlCommand()
command.connection=cnn
command.commandText="Select plotno from plotmaster where plotno="& txtplno.Text &""
command.parameters.Addwith Value(@plotno",plotnumber)
endSub

now when i insert the data in plotno , if the same plot no exist then it doesn't cheque and throwing the error message that duplicate key (primary) not allowed.
thatraja 22-Oct-13 5:53am    
You have to check the ID exists before insert, otherwise you'll have store new id in that column

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