Invalid attempt to read when no data is present.

J

Jerry

Below is ALL the code for all the databases...

Here's the problem:

I callup the aspx file in IE and the form comes up just fine. When I
select a person to update, I get the subject error. Aparently, when I
select a person, it's not selecting anyone and returning this error.
Here's the full error:

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

------------------------------------------------------------------------
------------------------------

Exception Details: System.InvalidOperationException: Invalid attempt to
read when no data is present.

Source Error:


Line 60: objRdr = objCmd.ExecuteReader();
Line 61: while (objRdr.Read()); {
Line 62: txtLast.Text = (string) objRdr["Lname"];
Line 63: txtFirst.Text = (string) objRdr["Fname"];
Line 64: txtRank.Text = (string) objRdr["Rank"];

------------------------------------------------------------------------
-----------------------------

I've compared the code in this application with one that works and I
can't see the problem. I guess I need a second pair of eyes (can't see
the trees thru the forest).

Here's all the code:
(there's three databases and one view)
------------------------------------------------------------------------
-----------------------------

------------tblWGA Script------------

CREATE TABLE [tblWGA] (
[wgaID] [int] IDENTITY (1, 1) NOT NULL ,
[unitID] [int] NULL ,
[typeID] [int] NULL ,
[wgaLast] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[wgaFirst] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[wgaRank] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[wgaType] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[wgaExt] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
CONSTRAINT [PK_tblWGA] PRIMARY KEY CLUSTERED
(
[wgaID]
) ON [PRIMARY] ,
CONSTRAINT [Tid_FK] FOREIGN KEY
(
[typeID]
) REFERENCES [tblType] (
[typeID]
),
CONSTRAINT [Uid_FK] FOREIGN KEY
(
[unitID]
) REFERENCES [tblUnit] (
[unitID]
)
) ON [PRIMARY]
GO

-----------tblUnit Script------------

CREATE TABLE [tblUnit] (
[unitID] [int] IDENTITY (1, 1) NOT NULL ,
[unitNumber] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
,
[unitName] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[unitDOL] [datetime] NULL ,
CONSTRAINT [PK_tblUnit] PRIMARY KEY CLUSTERED
(
[unitID]
) ON [PRIMARY]
) ON [PRIMARY]
GO

-----------tblType Script------------

CREATE TABLE [tblType] (
[typeID] [int] IDENTITY (1, 1) NOT NULL ,
[wgaType] [nvarchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
CONSTRAINT [PK_tblType] PRIMARY KEY CLUSTERED
(
[typeID]
) ON [PRIMARY]
) ON [PRIMARY]
GO

----------WGA_Update Script----------

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO



CREATE VIEW nelsonj.WGA_Update
AS
SELECT dbo.tblWGA.wgaLast AS Lname, dbo.tblWGA.wgaFirst AS Fname,
dbo.tblWGA.wgaRank AS Rank, dbo.tblType.wgaType AS Type,
dbo.tblUnit.unitNumber AS Unit,
dbo.tblUnit.unitName AS Designator, dbo.tblUnit.unitDOL AS Letter,
dbo.tblType.typeID AS TtypeID,
dbo.tblUnit.unitID AS TunitID, dbo.tblWGA.typeID
AS WtypeID, dbo.tblWGA.unitID AS WunitID, dbo.tblWGA.wgaID AS WwgaID
FROM dbo.tblType INNER JOIN
dbo.tblWGA ON dbo.tblType.typeID =
dbo.tblWGA.typeID INNER JOIN
dbo.tblUnit ON dbo.tblWGA.unitID =
dbo.tblUnit.unitID

GO


SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO



----------wgaUpdate.aspx Code-------------

<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Page Language="C#" Debug="true" %>
<html>
<head>
<title>WGA Lookup</title>
<script runat="server" language="C#">
SqlConnection objConn = new SqlConnection("Server=CACHAN07;" +
"Database='WGA list';" + "User ID=WGAadmin;Password=");
SqlDataReader objRdr;
SqlCommand objCmd;

void Page_Load() {
if (!IsPostBack) {
BindData();
}
}

void BindData() {
objCmd = new SqlCommand("SELECT * FROM WGA_Update",objConn);
//,WwgaID,Lname

objConn.Open();

objRdr = objCmd.ExecuteReader();
ddlLast.DataSource = objRdr;
ddlLast.DataValueField = "WwgaID";
ddlLast.DataTextField = "Lname";
ddlLast.DataBind();
objRdr.Close();

objCmd = new SqlCommand("SELECT TunitID,Designator FROM
WGA_Update",objConn);
objRdr = objCmd.ExecuteReader();
ddlUnit.DataSource = objRdr;
ddlUnit.DataValueField = "TunitID";
ddlUnit.DataTextField = "Designator";
ddlUnit.DataBind();
objRdr.Close();

objCmd = new SqlCommand("SELECT TunitID,Unit FROM
WGA_Update",objConn);
objRdr = objCmd.ExecuteReader();
ddlDesig.DataSource = objRdr;
ddlDesig.DataValueField = "TunitID";
ddlDesig.DataTextField = "Unit";
ddlDesig.DataBind();
objRdr.Close();

objCmd = new SqlCommand("SELECT TtypeID,Type FROM
WGA_Update",objConn);
objRdr = objCmd.ExecuteReader();
ddlType.DataSource = objRdr;
ddlType.DataValueField = "TtypeID";
ddlType.DataTextField = "Type";
ddlType.DataBind();
objRdr.Close();
objConn.Close();
}

void selectWGA(Object s, EventArgs e) {
objCmd= new SqlCommand("SELECT * FROM WGA_Update WHERE WwgaID =
@wgaID", objConn);
objCmd.Parameters.Add("@wgaID", ddlLast.SelectedItem.Value);
objConn.Open();

objRdr = objCmd.ExecuteReader();
while (objRdr.Read()); {
txtLast.Text = (string) objRdr["Lname"];
txtFirst.Text = (string) objRdr["Fname"];
txtRank.Text = (string) objRdr["Rank"];
ddlType.SelectedItem.Value = (string) objRdr["Type"];
ddlUnit.SelectedItem.Value = (string) objRdr["Designator"];
ddlDesig.SelectedItem.Value = (string) objRdr["Unit"];
txtDOL.Text = (string) objRdr["Letter"];
}
objRdr.Close();
objConn.Close();
BindData();
btnUpdate.Enabled = true;
}

void UpdateWGA(Object s, EventArgs e) {
objCmd = new SqlCommand(
"UPDATE WGA_Update SET " +
"Lname = @Last, Fname = @First, Rank = @Rank," +
"Type = @Type, Unit = @Unit, Designator = @Desig, Letter = @DOL" +
"WHERE wgaID = @wgaID", objConn);
objCmd.Parameters.Add("@Last", txtLast.Text);
objCmd.Parameters.Add("@First", txtFirst.Text);
objCmd.Parameters.Add("@Rank", txtRank.Text);
objCmd.Parameters.Add("@Type", ddlType.SelectedItem.Value);
objCmd.Parameters.Add("@Unit", ddlUnit.SelectedItem.Value);
objCmd.Parameters.Add("@Desig", ddlDesig.SelectedItem.Value);
objCmd.Parameters.Add("@DOL", txtDOL.Text);
objConn.Open();
objCmd.ExecuteNonQuery();
objConn.Close();
}

</script>
</head>
<body bgColor="#ffffcc">
<center>
<h1>WGA Administration Page</h1>
<hr>
<form runat="server">
<asp:DropDownList id="ddlLast" runat="server" />
<asp:Button id="btnSubmit" onClick="selectWGA" text="Get WGA Data"
runat="server"/>&nbsp;Select WGA Last Name
<p>
<table border="1" align="center" width="50%">
<tr>
<td>Last Name:</td><td><asp:TextBox id="txtLast" runat="server"
/></td>
</tr>
<tr>
<td>First Name:</td><td><asp:TextBox id="txtFirst" runat="server"
/></td>
</tr>
<tr>
<td>Rank:</td><td><asp:TextBox id="txtRank" runat="server" /></td>
</tr>
<tr>
<td>WGA Type:</td><td><asp:DropDownList id="ddlType" runat="server"
/></td>
</tr>
<tr>
<td>Unit Name:</td><td><asp:DropDownList id="ddlUnit" runat="server"
/></td>
</tr>
<tr>
<td>Unit Designator:</td><td><asp:DropDownList id="ddlDesig"
runat="server" /></td>
</tr>
<tr>
<td>Date of Letter</td><td><asp:TextBox id="txtDOL" runat="server"
/></td>
</tr>
</table>
<asp:Button id="btnUpdate" Text="Update WGA Data" Enabled="False"
runat="server" />
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top