Print Page | Close Window

Retrieving a Value from a DataRow Object

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=13597
Printed Date: 29 March 2026 at 3:11am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Retrieving a Value from a DataRow Object
Posted By: Misty
Subject: Retrieving a Value from a DataRow Object
Date Posted: 01 February 2005 at 1:44pm
I have a guest book application that uses ASP.Net and Microsoft Access. I have a table named EmailNotify that stores email addresses that will be sent email notifications every time someone signs the guest book. I am having some problems with the code. I think I'm missing something in the DataRow.
 
Here's the code that is significant:
 
                 Dim strConnect As String
                 Dim strSQL as String
                 Dim objConnect As New OleDbConnection()
                 Dim objCommand As New OleDbCommand()
                 Dim objDataAdapter As New OleDbDataAdapter()
                 Dim dtsGBook As New DataSet()
                 dim objDataRow as DataRow
                
                
               Dim databaseName as String
               Dim path as String
        databaseName = "MyDatabase.mdb"
        path = Server.MapPath(".")
        path = Replace(path,"html","database")
        'Create Connection object
        strConnect    = "Provider=Microsoft.Jet.OLEDB.4.0;Data " _
                & ;nbs p;                &a mp;n bsp;              & "Source=" & path & "\" & databaseName  
        
        
                 objConnect.ConnectionString = strConnect
                 objConnect.Open()

                 'Build sql string
                 strSQL = "Select * From EmailNotify"
                
                 'Set the Command Object properties
                 objCommand.Connection = objConnect
                 objCommand.CommandText = strSQL
                 'Fill the DataSet based on the SQL command
                 objDataAdapter.SelectCommand = objCommand
                 objDataAdapter.Fill(dtsGBook, "dttGBook")
 
                 'Create a DataRow object
                 'objDataRow = dtsGBook.Tables("dttGBook") - I tried doing this, but it would not work. 
               
                 Dim GetEMail As String
                &nbs p;   GetEmail = objDataRow("Email").ToString()
 
 

I am getting the following error message:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 162:
Line 163: Dim GetEMail As String
Line 164: GetEmail = objDataRow("Email").ToString()
 

I tried adding 'Create a DataRow object

'objDataRow = dtsGBook.Tables("dttGBook") before line 162, but it would not work. I think that I am supposed to create a datarow object before line 164 will work. Can someone please help me with this?

 



Replies:
Posted By: michael
Date Posted: 01 February 2005 at 4:20pm
Originally posted by Misty Misty wrote:

 
                 'Create a DataRow object
                 'objDataRow = dtsGBook.Tables("dttGBook") - I tried doing this, but it would not work. 
               
                 Dim GetEMail As String
                &nbs p;  GetEmail = objDataRow("Email").ToString()
 
 
As for the first problem you encoutered
objDataRow=dtsGBook.Tables(...
that cannot work as you have a row on the left and a table on the right. Row <> Table.
You could do objDataRow=dsGBook.Tables(0).Rows(0)
to get the first row in the collection. But why so complicated? You have them all in your dataset so why not just go like
For i as Integer=0 to ds.Tables(0).Rows.Count-1
   Dim GetEmail as String
   GetEmail = ds.Tables(0).Rows(i).Item("Email").ToString
   'Do your think like sending email or whatever
Next i
Now I assume a loop is what you want to do but no matter what you should get the idea.


-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Misty
Date Posted: 03 February 2005 at 1:25am
Michael,
 
Thank you! I did what you suggested. It worked very well.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net