问题对象引用未设置为对象的实例。 [英] problem Object reference not set to an instance of an object.

查看:40
本文介绍了问题对象引用未设置为对象的实例。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我的程序中的情节是否已经存在然后它应该显示记录已存在的消息,如果没有则插入记录,我在文本框plotno中使用了丢失焦点事件中的程序。



代码:

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:

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

推荐答案

替换下面的行

Replace the below line
Using cmd As SqlClient.SqlCommand



这个


with this

Using cmd As New SqlClient.SqlCommand


这篇关于问题对象引用未设置为对象的实例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆