Click here to Skip to main content
12,633,990 members (22,898 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB Visual-Studio VB.NET
Dear Coders,
Please Help me regarding the error "Object reference not set to an instance of an object"
The Source code is:

Imports System.Data.SqlClient

Public Class Form3
    Dim con As New SqlConnection
    Dim myconstring As String = "Data Source=ETA-TESTSRVR;Initial Catalog=Procurement;Integrated Security=True"

    Private Sub E2BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles E2BindingNavigatorSaveItem.Click

        Me.Validate()
        Me.E2BindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.ProcurementDataSet)

    End Sub

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

        'TODO: This line of code loads data into the 'ProcurementDataSet.E1' table. You can move, or remove it, as needed.
        Me.E1TableAdapter.FillBy1(Me.ProcurementDataSet.E1)

        'TODO: This line of code loads data into the 'ProcurementDataSet.E2' table. You can move, or remove it, as needed.
        Me.E2TableAdapter.Fill(Me.ProcurementDataSet.E2)

    End Sub

    Private Sub Mat_Sub_NoComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Mat_Sub_NoComboBox.SelectedIndexChanged

        con.ConnectionString = myconstring

        'Automated Serial Number
        Dim da As New SqlDataAdapter("SELECT max(sl_no)+1 as max FROM e1 where project_id='" + Project_IDTextBox.Text + "'", con)
        Dim dt As New DataTable
        da.Fill(dt)
        Sl_NoTextBox.Text = dt.Rows(0).Item("max").ToString
        If Sl_NoTextBox.Text = "" Then
            Sl_NoTextBox.Text = 1
        End If

        'Fetching Data to Textbox
        Dim da1 As New SqlDataAdapter("SELECT Project_ID,Trade,Fix,Material_Description FROM Mat_e1_prj where mat_sub_no='" + Mat_Sub_NoComboBox.SelectedValue.ToString + "'", con)
        Dim dt1 As New DataTable
        da1.Fill(dt1)
        Project_IDTextBox.Text = dt1.Rows(0).Item("Project_ID").ToString
        TradeTextBox.Text = dt1.Rows(0).Item("Trade").ToString
        FixTextBox.Text = dt1.Rows(0).Item("fix").ToString
        DescriptionTextBox.Text = dt1.Rows(0).Item("Material_Description").ToString
        
    End Sub

End Class
Posted 17-Nov-12 4:21am
Updated 17-Nov-12 4:52am
v2
Comments
OriginalGriff 17-Nov-12 9:36am
   
Where are you getting the error? Which line?
armarzook 18-Nov-12 0:39am
   
In this line
Dim da1 As New SqlDataAdapter("SELECT Project_ID,Trade,Fix,Material_Description FROM Mat_e1_prj where mat_sub_no='" + Mat_Sub_NoComboBox.SelectedValue.ToString + "'", con)
Dim dt1 As New DataTable
da1.Fill(dt1)
chandanadhikari 17-Nov-12 10:43am
   
debug the code by placing break points and find which line gives error

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Ah!
then you need to look at Mat_Sub_NoComboBox.SelectedValue.ToString - since it is in the SelectedIndexChanged methoid, the most likely reason is that there is nothing selected. Check the value of Mat_Sub_NoComboBox.SelectedValue at the top of your method - if it is Nothing then you either need to ignore the event, or do something different!
  Permalink  
Comments
@AmitGajjar 18-Nov-12 3:12am
   
5+
armarzook 18-Nov-12 7:39am
   
Now it is showing a different error...
"There is no row at position 0."
OriginalGriff 18-Nov-12 7:58am
   
Then there are no matching rows where project_id exactly matches the content of Project_IDTextBox.Text (or similar for your other table)
You could check for that as well - DataTable.Rows.Count will tell you.
armarzook 18-Nov-12 8:28am
   
Ok. Actually i tried what u told already the result is 1

MsgBox(Prjdt.Rows.Count)
Output is 1

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161208.2 | Last Updated 18 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100