473,472 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Newbie Wants to get row from database based on combobox selection

9 New Member
I get the error message "object reference not set to the instance if an object". The code that I have is :

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.  
  3.         LoadName()
  4.  
  5.         Dim con As New OleDbConnection
  6.         con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\CCS.mdb"
  7.  
  8.         Try
  9.             con.Open()
  10.  
  11.             Dim daGuard As OleDbDataAdapter = New OleDbDataAdapter("select gID, fullName from tblGuardInfo", con)
  12.             Dim dt As New DataSet
  13.             daGuard.Fill(dt, "tblGuardInfo")
  14.             Me.cboGetGuard.DataSource = dt.Tables("tblGuardInfo").DefaultView
  15.             Me.cboGetGuard.DisplayMember = "fullName"
  16.             Me.cboGetGuard.ValueMember = "fullName"
  17.  
  18.             dt = Nothing
  19.             daGuard = Nothing
  20.  
  21.         Catch ex As Exception
  22.             MessageBox.Show(ex.Message)
  23.         Finally
  24.             con.Close()
  25.         End Try
  26.  
  27.     End Sub
  28.  
  29.     Private Sub btnGetInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetInfo.Click
  30.  
  31.         Dim con As New OleDbConnection
  32.         con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\CCS.mdb"
  33.         Dim strSQL As String = "SELECT gID,gFName,gLName from tblGuardInfo WHERE fullName = '" & Me.cboGetGuard.Text & "'"
  34.  
  35.         Try
  36.             con.Open()
  37.  
  38.             Dim daGuard As OleDbDataAdapter = New OleDbDataAdapter(strSQL, con)
  39.             Dim dsGuard As New DataSet
  40.             daGuard.Fill(dsGuard)
  41.             Me.cboGetGuard.DataSource = dsGuard.Tables("tblGuardInfo")
  42.  
  43.             Me.txtgID.Text = dsGuard.Tables("tblGuardInfo").Rows(0).Item("gID")
  44.             Me.txtgFName.Text = dsGuard.Tables("tblGuardInfo").Rows(0).Item("gFName")
  45.             Me.txtgLName.Text = dsGuard.Tables("tblGuardInfo").Rows(0).Item("gLName")
  46.  
  47.         Catch ex As Exception
  48.             MessageBox.Show(ex.Message)
  49.         End Try
  50.     End Sub
  51.  
  52.     Private Sub LoadName()
  53.         Me.lblFullName.Text = Me.cboGetGuard.Text
  54.     End Sub
Feb 6 '08 #1
4 951
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
And which one of those lines throws that exception?
Feb 6 '08 #2
kunal pawar
297 Contributor
I think error is in
LoadName() function
coz cboGetGuard is combo obx or dropdown and u used property
cboGetGuard.text
Feb 7 '08 #3
Bomac8
9 New Member
the exception iss thrown after executing this line:

Me.txtgID.Text = dsGuard.Tables("tblGuardInfo").Rows(0).Item("gID")

However, during debugging, i dont think there is anything in the dataset.

Why am i getting the "object reference not set to the instance if an object" error?
Me.cboGetGuard.DataSource = dsGuard.Tables("tblGuardInfo")
Feb 7 '08 #4
Plater
7,872 Recognized Expert Expert
If the query does not return any rows, then your table will be empty and calling Rows(0) will throw that exception.
Feb 7 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Wayne Wengert | last post by:
I am trying to populate a combobox with a lisy of items. I want the 1st item to be "Make a Selection" and the following items will be names from a table in my DB (See code below). When I run the...
6
by: Noozer | last post by:
I'm developing a database using MS Access and have come across a problem. The majority of my database is pretty straightforward "many to one" relationships. I have one relationship that is...
0
by: George Hartas | last post by:
I am using Visual C# .NET 2003 to make a ComboBox accept both mouse and keyboard selection. For mouse selection code, I double-clicked ComboBox to get the default "comboBox1_SelectedIndexChanged"...
3
by: Alonso | last post by:
Hi, I have two drop downs and the content of the second one must changed everytime the user changes the selection in the first one. Is any easy way of doing this? Thanks in advance. Alonso
6
by: ProteusGak | last post by:
Hi, new membership and first post. I am working in VB 2005, windows forms and would like to know how to open form2 based on the user's selection from a combobox or listbox from say, form1. Thank...
7
by: grummanf6f | last post by:
Hello Gurus, this probably is real simple for you but for me it's a bummer. I have one table in which I have basic data collected of schools. I have another data that is detailed info on the...
4
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
I am trying to populate a DataGridView Combobox column with a different data binding source (dataset) depending upon a selection made in a previous column. I can handle the previous column but how...
2
by: jujubean | last post by:
I have a form with five comboboxes. In the first, the user can select one of four items. The other four comboxes are hidden. Based on the selection made in combobox one, I would like one of the...
4
by: =?Utf-8?B?SlA=?= | last post by:
I am beginning C# programming and am trying to open frmOrders from a button in frmCustomer (in Northwind database) with CustomerID as the link between the two forms. What is the easiest and...
0
by: Brandon | last post by:
Hi there... I got a WPF project that I am trying to select a ComboBoxItem in a ComboBox based on a string from the selected ListView item in the project... This ComboBox is unbound now and the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.