473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

ADO.NET: How to tell if a DataSet or DataRow is empty

After much convincing by other people, I have finally started trying to use ADO.NET instead of ADODB. Unfortunately, my lack of knowledge regarding these objects has left me broken in one area. I tried converting the area where I am checking for duplicate records to use ADO.NET by checking if a student filling out a form enters the same first name, last name, and program or major as another record in the database as per my instructions. Unfortunately, in the following code snippet, it is not behaving how I thought it should and I am unable to diagnose exactly where the problem might be just yet. I would appreciate any helpful information or pointers in the right direction on this issue that you might be able to give.

Here is the code in question:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         Try
  3.             Dim orion As New SqlConnection(strConn)
  4.             Dim sqlCmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(query, orion)
  5.  
  6.             sqlCmd.Parameters.Add("@fName", System.Data.SqlDbType.NVarChar).Value = firstName.Text
  7.             sqlCmd.Parameters.Add("@lName", System.Data.SqlDbType.NVarChar).Value = lastName.Text
  8.             sqlCmd.Parameters.Add("@progMaj", System.Data.SqlDbType.NVarChar).Value = progMajor.SelectedValue.ToString
  9.  
  10.             Dim dataAdapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(sqlCmd)
  11.             Dim dataSet As System.Data.DataSet = New System.Data.DataSet
  12.  
  13.             dataAdapter.Fill(dataSet)
  14.  
  15.             If (dataSet.Tables(0).CreateDataReader.HasRows) Then
  16.                 'we already have this record in the database
  17.                 errBlurb.Text &= "Already have this record." & vbCrLf
  18.                 success = False
  19.             End If
  20.         Catch
  21.             errBlurb.Text &= "Error checking for duplicate records." & vbCrLf
  22.             success = False
  23.         End Try
  24.  
I probably should have mentioned also that it never flags an error despite there being 'duplicate' records in the database at this point. Thank you for any pointers in the right direction or information/hints you might be able to give me in this matter!
Feb 23 '12 #1
3 4207
yarbrough40
320 100+
please show your sql. It's difficult to see exactly what you are doing without it
Feb 26 '12 #2
Sorry that it took me so long to get back to you. I ended up with a serious case of pneumonia that put me down for 4 weeks and I'm just now getting back to working on this project. Here is the whole code excerpt you requested, including the SQL. Please let me know if you need anything more, and thanks for your time taking a peek at this.

Expand|Select|Wrap|Line Numbers
  1.         'now check to see if this is a duplicate record
  2.         Dim strConn, query As String
  3.  
  4.         strConn = "SERVER=BSC-Orion\HomeApps; DATABASE=BSC-Survey; " & _
  5.                 "UID=nope; PWD=yeah_right;"
  6.         'removing PROVIDER=SQLOLEDB; from the start of the string
  7.         query = "SELECT Fname, Lname, ProgramMajor FROM [Student_Employment] WHERE " & _
  8.                 "Fname = @fName AND fName = @lName AND ProgramMajor = @progMaj"
  9.  
  10.         Try
  11.             Dim orion As New SqlConnection(strConn)
  12.             Dim sqlCmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(query, orion)
  13.  
  14.             sqlCmd.Parameters.Add("@fName", System.Data.SqlDbType.NVarChar).Value = firstName.Text
  15.             sqlCmd.Parameters.Add("@lName", System.Data.SqlDbType.NVarChar).Value = lastName.Text
  16.             sqlCmd.Parameters.Add("@progMaj", System.Data.SqlDbType.NVarChar).Value = progMajor.SelectedValue.ToString
  17.  
  18.             Dim dataAdapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(sqlCmd)
  19.             Dim dataSet As System.Data.DataSet = New System.Data.DataSet
  20.  
  21.             dataAdapter.Fill(dataSet)
  22.  
  23.             'If (dataSet.Tables(0).CreateDataReader().HasRows) Then
  24.             'we already have this record in the database
  25.             '    errBlurb.Text &= "Already have this record." & vbCrLf
  26.             '    success = False
  27.             'End If
  28.  
  29.             Dim count As Integer = dataSet.Tables(0).Rows.Count
  30.             'For Each dr As DataRow In dataSet.Tables(0).Rows.
  31.             ' For Each it As Object In dr.ItemArray
  32.             'count += 1
  33.             'Next
  34.             'Next
  35.  
  36.             If (count > 0) Then
  37.                 'duplicate record
  38.                 errBlurb.Text &= "Already have this record x" & count.ToString() & vbCrLf
  39.                 success = vbFalse
  40.             End If
  41.         Catch
  42.             errBlurb.Text &= "Error checking for duplicate records." & vbCrLf
  43.             success = False
  44.         End Try
Mar 27 '12 #3
Alright, I found my mistake in that whole mess. Turns out I was accessing the ADO stuff correctly, I just wasn't familiar enough with VB to know that I had to ReDim the array that I was loading with the information. Whoops!
Mar 29 '12 #4

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

Similar topics

3
by: Alpha | last post by:
Hi, I have a window C# application. I update the dataset when user finished entering in a text box. But when I want to update the database when OK is pressed, the...
3
by: martin | last post by:
Hi, I am having trouble determining if my dataset is empty. I have function that populates a dataset from a database and then retuens it. If the database contains no data then the dataset will...
1
by: Shapper | last post by:
Hello, I have am connecting a datagrid to a dataset: dgProducts.DataSource = dsProducts() dsProducts is a function which returns a dataset. How can I detect if the dataset which is being...
3
by: Tim Frawley | last post by:
I have a situtation where I am adding a Row to a Dataset that was filled with an Access table. The column in the Access table shows a default value of 0 (zero) but when I create a new row on the...
3
by: Newbie | last post by:
Could someone please tell me how or if the following is possible. I have created a small test database and have opened one connection, one DataAdapter, and one Dataset for it in my vb.net...
1
by: theWizard1 | last post by:
The following sends my email, but the attachment is empty. The attachment should contain the data that is in the string that was created from the xmlReader. I have a stored procedure written...
3
by: nashak | last post by:
Hello, ASP.NET pages are disconnected html pages i.e once page is sent to the browser, that is it. Let's say that I have a datagrid on my page and I have used a dataset to fill this grid only...
0
by: Jim | last post by:
Hi, ..Net 2003, Language: VB Problem: Populating a Dataset with a datatable, the columns of which are bound to controls on a form. I'm assigning the value directly in 2 of the fields of the...
2
by: ccbryan | last post by:
Hi folks... .NET newbie I admit. I'm running VS 2005 version 8 on .NET platform 2.0.50727 SP1. How can you tell if a query has returned an empty dataset to a SQLReader or a DataTable? I am running...
0
by: rclarke | last post by:
I am writing an application which needs to save data to a new Jet (MS Access) database. What I would like to clarify is that there is no straightforward way of simply saving an existing DataSet as an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.