Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Dear Coders,
Please Help me regarding the error "Object reference not set to an instance of an object"
The Source code is:

VB
  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
Updated 17-Nov-12 3: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

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!
 
Share this answer
 
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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900