Click here to Skip to main content
Rate this: bad
good
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:
 
  1  Imports System.Data.SqlClient
  2  
  3  Public Class Form3
  4      Dim con As New SqlConnection
  5      Dim myconstring As String = "Data Source=ETA-TESTSRVR;Initial Catalog=Procurement;Integrated Security=True"
  6  
  7      Private Sub E2BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles E2BindingNavigatorSaveItem.Click
  8  
  9          Me.Validate()
 10          Me.E2BindingSource.EndEdit()
 11          Me.TableAdapterManager.UpdateAll(Me.ProcurementDataSet)
 12  
 13      End Sub
 14  
 15      Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 16  
 17          'TODO: This line of code loads data into the 'ProcurementDataSet.E1' table. You can move, or remove it, as needed.
 18          Me.E1TableAdapter.FillBy1(Me.ProcurementDataSet.E1)
 19  
 20          'TODO: This line of code loads data into the 'ProcurementDataSet.E2' table. You can move, or remove it, as needed.
 21          Me.E2TableAdapter.Fill(Me.ProcurementDataSet.E2)
 22  
 23      End Sub
 24  
 25      Private Sub Mat_Sub_NoComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Mat_Sub_NoComboBox.SelectedIndexChanged
 26  
 27          con.ConnectionString = myconstring
 28  
 29          'Automated Serial Number
 30          Dim da As New SqlDataAdapter("SELECT max(sl_no)+1 as max FROM e1 where project_id='" + Project_IDTextBox.Text + "'", con)
 31          Dim dt As New DataTable
 32          da.Fill(dt)
 33          Sl_NoTextBox.Text = dt.Rows(0).Item("max").ToString
 34          If Sl_NoTextBox.Text = "" Then
 35              Sl_NoTextBox.Text = 1
 36          End If
 37  
 38          'Fetching Data to Textbox
 39          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)
 40          Dim dt1 As New DataTable
 41          da1.Fill(dt1)
 42          Project_IDTextBox.Text = dt1.Rows(0).Item("Project_ID").ToString
 43          TradeTextBox.Text = dt1.Rows(0).Item("Trade").ToString
 44          FixTextBox.Text = dt1.Rows(0).Item("fix").ToString
 45          DescriptionTextBox.Text = dt1.Rows(0).Item("Material_Description").ToString
 46          
 47      End Sub
 48  
 49  End Class
Posted 17-Nov-12 4:21am
Edited 17-Nov-12 4:52am
v2
Comments
OriginalGriff at 17-Nov-12 9:36am
   
Where are you getting the error? Which line?
armarzook at 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 at 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 at 18-Nov-12 3:12am
   
5+
armarzook at 18-Nov-12 7:39am
   
Now it is showing a different error...
"There is no row at position 0."
OriginalGriff at 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 at 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
0 DamithSL 425
1 OriginalGriff 184
2 BillWoodruff 170
3 Zoltán Zörgő 165
4 Maciej Los 163
0 OriginalGriff 7,854
1 DamithSL 5,949
2 Sergey Alexandrovich Kryukov 5,449
3 Maciej Los 5,084
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 18 Nov 2012
Copyright © CodeProject, 1999-2014
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