Click here to Skip to main content
15,921,210 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
'I have a modified my code as Grif told me, i removed code block from load event and I call a function auto at the row enter event,Still problem Persists.

'I have declared a variable index to hold the current row index of the gridview

public Class RecuritmentManagement

Dim con As New SqlConnection("Integrated Security=SSPI;Initial catalog=Equation;server=localhost\SQLEXPRESS")

public index as Integer

Private Sub RecruitmentManagement_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim sCommand As SqlCommand
    Dim sAdapter As SqlDataAdapter
    Dim sBuilder As SqlCommandBuilder
    Dim sDs As DataSet
    Dim sTable As DataTable

    Try
        
        Dim sql As String = "select Id,SrNo,Name,DOB,ContactNumber,ResNumber, Location,Qualification,CurrentOrganizatn,WorkExp,CalledBy,EQId,Date,SentTo,HRStatus,Process,Department,Designation from BasicInfo,CompanyInfo where BasicInfo.CId=CompanyInfo.CIds"

        sCommand = New SqlCommand(sql, con)
        sAdapter = New SqlDataAdapter(sCommand)  
        sDs = New DataSet()
        sAdapter.Fill(sDs)
        sTable = sDs.Tables(0)

        BasicDetailGrid.DataSource = sDs.Tables(0)


    Catch ex As Exception
        MsgBox(ex.ToString())

    End Try

End Sub

Private Sub BasicDetailGrid_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles BasicDetailGrid.RowEnter

       auto()

   End Sub


Public Sub auto()
       Try
           
           Dim cmd1 As New SqlCommand
           Dim f2
           con.Open()
           Dim cmd2 As New SqlCommand("select COUNT(*) from BasicInfo", con)
           f2 = cmd2.ExecuteScalar
           con.Close()

           index = BasicDetailGrid.CurrentRow.Index '******* ERROR HERE *******

           If (CInt(f2) = 0) Then
               Me.BasicDetailGrid.Rows(index).Cells(0).Value = "1";
               
           Else
           ...

           End If
       Catch ex As Exception
           MsgBox(ex.ToString())
       End Try
End Sub

End Class


'Thanks In Advance.

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 23-Jun-11 3:04am
v4
Comments
OriginalGriff 23-Jun-11 7:08am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.

Yes. So?
BasicDetailGrid.DataSource = sDs.Tables(0)
index = BasicDetailGrid.CurrentRow.Index
If you are getting a "object not set to an instance" error on the second of these two lines I am not surprised. There is no current row. And won't be until the user has selected one. Which, since you haven't shown it to him / her yet, will be a while...

I would suggest that wherever you use index it would be better to both load it there (so that you get the latest version after any user input) and check it for validity before you use it, in case the user hasn't selected a row yet...
 
Share this answer
 
Adding to other answer:

" System.NullReferenceException: Object reference not set to an instance of an object."
This simply means that you are trying to access a property of an object that is null.

Using Visual Studio DEBUGGER will tell you exact line and the object that is null. Find it and handle the same.
 
Share this answer
 

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