-
.Net Database Issue In VB
I am trying to create a program that retrieves info. from a database when
someone types in FirstName, LastName, or Badge # and performs a search.
I get the error - An unhandled exception of type 'System NullReferenceException'
occured in MossSecurity.exe
Additional Info: Object Reference not set to an instance of an object
I also tried an example straight out of a book and got the same error
Could there be a bug or something and if not then could someone help me change
the code
Here is my code
Public Class Form1
Inherits System.Windows.Forms.Form
'This program is designed to return employee data by badge number or
employee name
'Employee data will consist of Badge Number, First Name, Last Name, Department
'and Photo (if available).
Dim Id As String
Dim FName, LName, Dept As String
Private Sub BadgeBtn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BadgeBtn.Click
Dim id As String
id = Badgetxt.Text
BadgeNumber(id)
End Sub
Private Sub Namebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Namebtn.Click
Dim FName As String
Dim LName As String
FName = Fnametxt.Text
LName = Lnametxt.Text
EmployeeName(FName, LName)
End Sub
Private Sub BadgeNumber(ByVal ID As String)
'BadgeNumber subroutine retrieves data by Badge Number.
Dim FirstName, LastName, Dept As String
Dim BadgeNum As String
If ID <> "" Then
Form_Activate()
End If
On Error GoTo IDHandler
IDHandler: MsgBox("You have entered an invalid Badge Number.", vbInformation,
"ATTENTION")
Badgetxt.Text = ""
End Sub
Private Sub EmployeeName(ByVal FName As String, ByVal LName As String)
'EmployeeName subroutine retrieves data by Employee Name.
Dim FirstName, LastName, Dept As String
Dim BadgeNum As String
Dim Photo As Image
If FName <> "" Or LName <> "" Then
Form_Activate2()
End If
On Error GoTo NameHandler
NameHandler: MsgBox("You have entered an invalid Employee Name.", vbInformation,
"ATTENTION")
Fnametxt.Text = ""
Lnametxt.Text = ""
End Sub
Public Function Form_Activate()
Dim MyConnection As OleDb.OleDbConnection
Dim objadapter As OleDb.OleDbDataAdapter
Dim objRow As DataRow
Dim objDs As New DataTable()
Dim strQuery As String
MyConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"
_
& "Data Source=C:\Program Files\Microsoft
Visual Studio\VB98\MossSecurityDB\MossSecurity.mdb"
strQuery = "SELECT BadgeNum, FirstName, LastName, Dept, Photo FROM
Employees WHERE BadgeNum = '" _
& Id
objadapter.SelectCommand.CommandText = strQuery
objadapter.Fill(objDs)
Form_Activate = objDs.Copy()
LstDisplay.Items.Add("Badge #: " & Id)
LstDisplay.Items.Add("First Name: " & FName)
LstDisplay.Items.Add("Last Name: " & LName)
LstDisplay.Items.Add("Department: " & Dept)
End Function
Public Function Form_Activate2()
Dim MyConnection As OleDb.OleDbConnection
Dim objadapter As OleDb.OleDbDataAdapter
Dim objRow As DataRow
Dim objDs As New DataTable()
Dim strQuery As String
MyConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"
_
& "Data Source=C:\Program Files\Microsoft
Visual Studio\VB98\MossSecurityDB\MossSecurity.mdb"
strQuery = "SELECT BadgeNum, FirstName, LastName, Dept, Photo FROM
Employees WHERE FirstName = '" _
& FName & "' or LastName = '" & LName & "'"
objadapter.SelectCommand.CommandText = strQuery
objadapter.Fill(objDs)
Form_Activate2 = objDs.Copy()
LstDisplay.Items.Add("Badge #: " & Id)
LstDisplay.Items.Add("First Name: " & FName)
LstDisplay.Items.Add("Last Name: " & LName)
LstDisplay.Items.Add("Department: " & Dept)
End Function
Private Sub Exitbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Exitbtn.Click
End
End Sub
Private Sub Clearbtn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Clearbtn.Click
LstDisplay.Items.Clear()
Badgetxt.Text = ""
Fnametxt.Text = ""
Lnametxt.Text = ""
End Sub
End Class
-
Re: .Net Database Issue In VB
> I get the error - An unhandled exception of type 'System
> NullReferenceException' occured in MossSecurity.exe
> Additional Info: Object Reference not set to an instance
> of an object
Jeff: On which line does the error occur?
--
Phil Weber
-
Re: .Net Database Issue In VB
This line here:
MyConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=C:\Program Files\Microsoft
Visual Studio\VB98\MossSecurityDB\MossSecurity.mdb"
"Phil Weber" <philweber@hotmail.com> wrote:
> > I get the error - An unhandled exception of type 'System
> > NullReferenceException' occured in MossSecurity.exe
> > Additional Info: Object Reference not set to an instance
> > of an object
>
>Jeff: On which line does the error occur?
>--
>Phil Weber
>
-
Re: .Net Database Issue In VB
> This line here:
> MyConnection.ConnectionString = ...
Jeff: Try changing the line:
Dim MyConnection As OleDb.OleDbConnection
to:
Dim MyConnection As New OleDb.OleDbConnection
(And please stop posting this question to the vb.dotnet.discussion group!
;-)
--
Phil Weber
-
Re: .Net Database Issue In VB
i already stopped doing that
i sent it a second time because i thought it didnt go through before u told
me to only post on one
sorry
"Phil Weber" <philweber@hotmail.com> wrote:
> > This line here:
> > MyConnection.ConnectionString = ...
>
>Jeff: Try changing the line:
>
> Dim MyConnection As OleDb.OleDbConnection
>
>to:
>
> Dim MyConnection As New OleDb.OleDbConnection
>
>(And please stop posting this question to the vb.dotnet.discussion group!
>;-)
>--
>Phil Weber
>
-
Re: .Net Database Issue In VB
I added new
and now i get the same error
on this line:
objadapter.SelectCommand.CommandText = strQuery
"Jeff" <vb.@127.0.0.1> wrote:
>
>i already stopped doing that
>i sent it a second time because i thought it didnt go through before u told
>me to only post on one
>sorry
>
>"Phil Weber" <philweber@hotmail.com> wrote:
>> > This line here:
>> > MyConnection.ConnectionString = ...
>>
>>Jeff: Try changing the line:
>>
>> Dim MyConnection As OleDb.OleDbConnection
>>
>>to:
>>
>> Dim MyConnection As New OleDb.OleDbConnection
>>
>>(And please stop posting this question to the vb.dotnet.discussion group!
>>;-)
>>--
>>Phil Weber
>>
>
-
Re: .Net Database Issue In VB
I have no clue
Im pretty new to this
First time Ive used a non-text file based database
"Phil Weber" <philweber@hotmail.com> wrote:
> > I added new and now I get the same error on this line:
> > objAdapter.SelectCommand.CommandText = strQuery
>
>OK, let's analyze: We corrected the first error -- "Object Reference not
set
>to an instance of an object" on a line that referenced MyConnection -- by
>adding a "New" to the declaration of MyConnection. Now you're getting the
>error on a line that references objAdapter, so how do you think you should
>fix it?
>--
>Phil Weber
>
-
Re: .Net Database Issue In VB
> I added new and now I get the same error on this line:
> objAdapter.SelectCommand.CommandText = strQuery
OK, let's analyze: We corrected the first error -- "Object Reference not set
to an instance of an object" on a line that referenced MyConnection -- by
adding a "New" to the declaration of MyConnection. Now you're getting the
error on a line that references objAdapter, so how do you think you should
fix it?
--
Phil Weber
-
Re: .Net Database Issue In VB
> I have no clue
Well, we solved the first error by adding "New" to the declaration of the
object in question (MyConnection). So try adding "New" to the declaration of
objAdapter. The issue is that the Dim statement doesn't actually create an
instance of an object; you need to use New for that.
--
Phil Weber
-
Re: .Net Database Issue In VB
I still get the same error in the same spot
"Phil Weber" <philweber@hotmail.com> wrote:
> > I have no clue
>
>Well, we solved the first error by adding "New" to the declaration of the
>object in question (MyConnection). So try adding "New" to the declaration
of
>objAdapter. The issue is that the Dim statement doesn't actually create
an
>instance of an object; you need to use New for that.
>--
>Phil Weber
>
-
Re: .Net Database Issue In VB
Still Havent solved this if anyone could help
"Jeff" <JFord@mossberg.com> wrote:
>
>I still get the same error in the same spot
>
>"Phil Weber" <philweber@hotmail.com> wrote:
>> > I have no clue
>>
>>Well, we solved the first error by adding "New" to the declaration of the
>>object in question (MyConnection). So try adding "New" to the declaration
>of
>>objAdapter. The issue is that the Dim statement doesn't actually create
>an
>>instance of an object; you need to use New for that.
>>--
>>Phil Weber
>>
>
-
Re: .Net Database Issue In VB
Jeff, try something like this:
Public Class Form1
Inherits System.Windows.Forms.Form
' this is the connection info
Private mySelectText As String = "SELECT * FROM Daylilys ORDER BY Name"
Private mySelectConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=D:\Daylily.mdb"
Private da As New OleDbDataAdapter(mySelectText, mySelectConn)
Private ds As New DataSet("daylilys")
Private myrow As DataRow
Dim bmb As BindingManagerBase
This is where I load my data:
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnLoad.Click
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
da.Fill(ds, "daylilys")
DataGrid1.DataSource = ds.Tables(0).DefaultView
ds.Tables(0).DefaultView.AllowEdit = True
ds.Tables(0).DefaultView().AllowNew = True
bmb = Me.BindingContext(Me.DataGrid1.DataSource, Me.DataGrid1.DataMember)
End Sub
Maybe this will give you some ideas. This will populate a datagrid. In
this case information from a database called"Daylily.mdb" and the table name
is:"daylilys".
james
"Jeff" <vb.@127.0.0.1> wrote in message
news:3f007b7e$1@tnews.web.devx.com...
>
> Still Havent solved this if anyone could help
>
>
> "Jeff" <JFord@mossberg.com> wrote:
> >
> >I still get the same error in the same spot
> >
> >"Phil Weber" <philweber@hotmail.com> wrote:
> >> > I have no clue
> >>
> >>Well, we solved the first error by adding "New" to the declaration of
the
> >>object in question (MyConnection). So try adding "New" to the
declaration
> >of
> >>objAdapter. The issue is that the Dim statement doesn't actually create
> >an
> >>instance of an object; you need to use New for that.
> >>--
> >>Phil Weber
> >>
> >
>
-
Re: VB
Nikz: This group is for questions about VB.NET. I have moved your question
to the vb.general group, which is for questions about VB6 and earlier
versions. Thanks!
--
Phil Weber
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks