473,503 Members | 12,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's wrong with this DataReader????

11 New Member
I am using Microsoft Visual Studio 2003 and Microsoft Access.

Everytime I run this, it keeps throwing the exception "specified cast is not valid". I don't know what's wrong. Here is the code.

//variables for Employees
string strLName = "";
string strFName = "";
string strMName = "";
string strSSN = "";

//database location
string conString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=C:\\TimeClock\\TimeClock.mdb";
OleDbConnection empConnection = new OleDbConnection(conString);

//select data
string selectStatement = "SELECT FirstName, MiddleName, LastName, SSN "
+ "FROM Employee";
OleDbCommand selectCommand = new OleDbCommand(selectStatement, empConnection);
empConnection.Open();
//activate reader
OleDbDataReader myReader = selectCommand.ExecuteReader();

//populate the list view with employees
try
{
if (myReader.HasRows)
{
while (myReader.Read())
{
strSSN = myReader.GetString(0);
strLName = myReader.GetString(1);
strFName = myReader.GetString(2);
strMName = myReader.GetString(3);

//there is a reason its checking to see if there is no SSN
//I would have to explain the whole program for you to know why
if (strSSN != "")
{
ListViewItem lvi = new ListViewItem();
lvi.Text = strSSN;
lvi.SubItems.Add(strLName);
lvi.SubItems.Add(strFName);
lvi.SubItems.Add(strMName);
lvwEmployees.Items.Add(lvi);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

myReader.Close();
empConnection.Close();
Jul 29 '06 #1
3 1418
themoonisdown09
11 New Member
I failed to mention that it's in C#.
Jul 29 '06 #2
dna5122
12 New Member
You also failed to mention which line throws the exception. That would help alot.
Jul 29 '06 #3
themoonisdown09
11 New Member
I figured it out now. It's all good.
Jul 31 '06 #4

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

Similar topics

2
6501
by: Islam Elkhayat | last post by:
In my C# Web Application I need to fill a textbox with DataReader and use a Next Button to view next value... I created the Datareader in the Page_Load but everytime it get result of the First...
5
5748
by: Jason Huang | last post by:
Hi, Is it possible to bind DataReader to a DataGrid in C# windows form? And how? And can we update data in a DataSet by using the DataReader? Thanks for help. Jason
3
1310
by: shin | last post by:
hi i am trying to output an image which i have in a database. but this gives me errors byte Picture; Picture = (dr); //cannot convert from object to byte Response.Buffer=true;
1
1548
by: BC | last post by:
Hi, Been searching the newgroups but haven't found an answer. I'm used to binding disconnected ADO recordsets to Datagrid controls and tweaking the Datagrid to suit my needs (columnheader,...
5
1192
by: Tim::.. | last post by:
Can someone please tell me what is wrong with the following code! I have a record in the datareader but it isn't showing up and niether is the label... I presume there is a problem with this...
14
2220
by: Bihn | last post by:
I was reading about datareader which is said to be slimmer & faster then dataset. Since the datareader have to go fetching the dat from the database every time it need it, the data it gets then...
3
2338
by: tshad | last post by:
I am trying to set up a class to handle my database accesses. I can't seem to figure out how to get the return value from my dataReader from these routines (most of which I got elsewhere). They...
6
1934
by: Kalim Julia | last post by:
Is it possible to open more than 1 datareader / dataadapter on one connection ? Is it possible to duplicate / clone connection ?
0
2872
by: Teo | last post by:
Hi!! I have been trying to fix an error I keep getting on my VB.NET code. For some reason, I get an error saying "Invalid attempt to access a field before calling Read()" everytime. As you can see...
4
6182
by: =?Utf-8?B?VG9kZCBKYXNwZXJz?= | last post by:
Here is what I have: private int NationalCount() { Int32 numRecords = 0; using (SqlConnection dataConnection = new SqlConnection(GlobalVars.sqlConnString)) { SqlCommand dataCommand = new...
0
7212
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
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7364
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
7017
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
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
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
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1524
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.