none
ex.message= object reference not set to an instance of an object RRS feed

  • Question

  • hi all,

     i am new to developing so my question may sound stupid, well i am working with vs 2010 language vb, developing windows application. coming to code i want to retrieve data from my database using dataread but i am getting ex.message=  object reference not set to an instance of an object 

    my code is,

    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Text.RegularExpressions
    Public Class Forgotpassword
        Dim cn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\maddy\Documents\Visual Studio 2010\Projects\vijaya lakshmi chit fund\vijaya lakshmi chit fund\vlcf.mdf;Integrated Security=True;User Instance=True")
        Dim cmd As New SqlCommand
        Dim dr As SqlDataReader


        Private Sub Forgotpassword_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.EnabledChanged
            Try
                cmd.Connection = cn
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If tbFPEMAIL.Text = Nothing Then
                Me.erorproFPEMAIL.SetError(Me.tbFPEMAIL, "Please fill the required feild")
            Else
                Dim emailvalidator As Boolean
                Try
                    emailvalidator = Regex.IsMatch(tbFPEMAIL.Text, "\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", RegexOptions.IgnoreCase)
                Catch ex As ArgumentException
                End Try
                If emailvalidator = False Then
                    Me.erorproFPEMAIL.SetError(Me.tbFPEMAIL, "INVALID E-MAIL ID")
                End If
            End If

            cmd.CommandText = "slect password from login where emailid='" & tbFPEMAIL.Text & "'"
            cn.Open()
            Try
                dr = cmd.ExecuteReader()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            Try
                If dr.Read() = True Then
                    Label2.Text = dr.Read
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            cn.Close()
        End Sub
    End Class  
    Friday, January 11, 2013 8:39 AM

Answers

  • That is probably because you have not put this in the button click event.

        

        cmd.Connection = cn

    But please don't create follow up questions, it took me 24 hours before I understood that it was like that.

    If a problem is solved as far as the question goes than mark all the replies which have led to the answer as answer and vote all the helpful posts like that.


    Success
    Cor

    • Marked as answer by Alexander Sun Wednesday, January 30, 2013 8:05 AM
    Wednesday, January 16, 2013 9:59 AM

All replies

  • Hi,

    Use ex.ToString(), It will works

    example:

      MessageBox.Show(ex.ToString())


    PS.Shakeer Hussain

    Friday, January 11, 2013 10:15 AM
  • tan q but it's still  not working
    Friday, January 11, 2013 1:25 PM
  • With this kind of code which you use,

    Catch ex As ArgumentException
    End Try

    you can expect any kind of message which is completely unrelated to its place and its content.


    Success
    Cor

    Saturday, January 12, 2013 2:15 PM
  • Hi rahul,

    In which line your are facing error


    PS.Shakeer Hussain

    Monday, January 14, 2013 6:39 AM
  • Where you able to connect to the DB file properly? put a breakpoint and chenck

    If yes is there some data returned via the query ""slect password from login where emailid='" & tbFPEMAIL.Text & ", please ensure that tbFPEMAIL you have trimmed the data before comparison and same case also something like tbFPEMAIL.Text.Trim.Upper()


    Abhinav
    http://bishtabhinav.wordpress.com/

    Monday, January 14, 2013 7:19 AM
  • That can be completely unpredictable. Because of the Resume on error which I've showed in the code the error can appear on any place of the program because that the dr can contain all kind of rubish


    Success
    Cor

    Tuesday, January 15, 2013 9:57 AM
  • hi syed,

    at  dr = cmd.ExecuteReader()

    i am getting connection property has not been initialized and at

    If dr.Read() = True Then
      Label2.Text = dr.Read

    object reference has not  set to a instance of an object 

    Tuesday, January 15, 2013 10:36 AM
  • hi cor ligthert,

    i am also getting this ex message

    connection property has not been initialized 

    can u plz tell me y iam getting this message

    am i missing somthing here......can u plz tell me what all should i see before retrieve a data from data base using data reader

       

    Tuesday, January 15, 2013 10:48 AM
  • Hi Rahul,

    You can try with the below code by passing table "Column name with dr

     If dr.Read() Then
         Label2.Text = dr("ColumnName").ToSTring()
      End If                   
    or
    If dr.Read() Then
         Label2.Text = dr(0).ToSTring()
      End If    
    --------------------------------------------------------------------------------

    PS.Shakeer Hussain


    Tuesday, January 15, 2013 10:49 AM
  • That is probably because you have not put this in the button click event.

        

        cmd.Connection = cn

    But please don't create follow up questions, it took me 24 hours before I understood that it was like that.

    If a problem is solved as far as the question goes than mark all the replies which have led to the answer as answer and vote all the helpful posts like that.


    Success
    Cor

    • Marked as answer by Alexander Sun Wednesday, January 30, 2013 8:05 AM
    Wednesday, January 16, 2013 9:59 AM