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

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello everyone,
I kind of new to C# object oriented programming and I recently started working for a company that their previous IT guy had wrote some code for a module in the website. It had an error when he left but he did not document it so I have no idea what it could
be :( . So if anyone could please help me with this error that would be great.
Thank you in advance.

<br/>
Source Error:
<table bgcolor="#ffffcc" width="100%
<tbody>
<tr>
<td>
Code:
<pre> Line 31:         // rows represent privileges (AddModify, Delete, View)
Line 32:         // stored in database as a bit (0 or 1); boolean value; user has right or doesnt
<span style="color:red Line 33:         userpriv[0, 0] = Convert.ToBoolean(reader[0]);
Line 34:         //userpriv[0, 1] = Convert.ToBoolean(reader[1]);
Line 35:         //userpriv[0, 2] = Convert.ToBoolean(reader[2]);
[/code]</td>
</tr>
</tbody>
</table>

The error is located in the masters page and the whole module seems to have been created from Visual Studio 2010 also it is an ASP.NET website written in C# with a SQL Server 2008 backend.
Rest of the pages code:
<%@ Master Language="C#" %><br/>
<?xml version="1.0" encoding="utf-8"?><br/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"<br/>
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd <br/>
<br/>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en <br/>
<head runat="server <br/>
<title>Concepts West Training Database</title><br/>
</head><br/>
<body alink="blue" vlink="blue" link="blue" bgcolor="white <br/>
<table cellpadding="30px" cellspacing="0" align="left" cols="2 <br/>
<tr valign="top <br/>
<td align="left <br/>
<!-- navigation panel --><br/>
<!-- main list of categories --><br/>
<% <br/>
// get username stored in session variable and look it up in [TR_Privileges] table<br/>
string username = Session["uname"].ToString().Trim();<br/>
string usergroup = Session["ugroup"].ToString().Trim().ToUpper();<br/>
<br/>
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();<br/>
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["CWIS"].ConnectionString;<br/>
conn.Open();<br/>
string commandString = "SELECT [Employee_AddModify], [Employee_Delete], [Employee_View], [ECC_AddModify], [ECC_Delete], [ECC_View], [Procedures_AddModify], [Procedures_Delete], [Procedures_View], [Revs_AddModify], [Revs_Delete], [Revs_View],
[Reqs_AddModify], [Reqs_Delete], [Reqs_View], [Sessions_AddModify], [Sessions_Delete], [Sessions_View] FROM dbo.[TR_Privileges] WHERE [Username] = " + username + ";";<br/>
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(commandString, conn);<br/>
System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader();<br/>
reader.Read();<br/>
<br/>
bool[,] userpriv = new bool[6, 3]; // array to hold result of query<br/>
// array is 6x3; 6 columns and 3 rows; columns represent categories (Employee, ECC, Procedures, Revs, Reqs, Sessions)<br/>
// rows represent privileges (AddModify, Delete, View)<br/>
// stored in database as a bit (0 or 1); boolean value; user has right or doesnt<br/>
userpriv[0, 0] = Convert.ToBoolean(reader[0]);<br/>
//userpriv[0, 1] = Convert.ToBoolean(reader[1]);<br/>
//userpriv[0, 2] = Convert.ToBoolean(reader[2]);<br/>
//userpriv[1, 0] = Convert.ToBoolean(reader[3]);<br/>
//userpriv[1, 1] = Convert.ToBoolean(reader[4]);<br/>
//userpriv[1, 2] = Convert.ToBoolean(reader[5]);<br/>
//userpriv[2, 0] = Convert.ToBoolean(reader[6]);<br/>
//userpriv[2, 1] = Convert.ToBoolean(reader[7]);<br/>
//userpriv[2, 2] = Convert.ToBoolean(reader[8]);<br/>
//userpriv[3, 0] = Convert.ToBoolean(reader[9]);<br/>
//userpriv[3, 1] = Convert.ToBoolean(reader[10]);<br/>
//userpriv[3, 2] = Convert.ToBoolean(reader[11]);<br/>
//userpriv[4, 0] = Convert.ToBoolean(reader[12]);<br/>
//userpriv[4, 1] = Convert.ToBoolean(reader[13]);<br/>
//userpriv[4, 2] = Convert.ToBoolean(reader[14]);<br/>
//userpriv[5, 0] = Convert.ToBoolean(reader[15]);<br/>
//userpriv[5, 1] = Convert.ToBoolean(reader[16]);<br/>
//userpriv[5, 2] = Convert.ToBoolean(reader[17]);<br/>
<br/>
reader.Close();<br/>
conn.Close();<br/>
<br/>
if (usergroup == "ADMIN")<br/>
{<br/>
string menu;<br/>
menu = "User Privileges";<br/>
menu += "";<br/>
menu += "./training_userpriv_view.aspx>View User Rights ";<br/>
menu += "./training_userpriv_modify.aspx>Modify User Rights ";<br/>
menu += "";<br/>
Response.Write(menu);<br/>
}<br/>
%><br/>
Employees<br/>
<!-- sublist for employees category --><br/>
./training_addemployee.aspx Add a New Employee <br/>
./training_modifyemployee.aspx Update Employee Info <br/>
./training_viewemployee.aspx View Employees <br/>
./training_deleteemployee.aspx Delete an Employee <br/>
<br/>
Classification Codes<br/>
<!-- sublist of ECCs --><br/>
./training_addecc.aspx Add a New ECC <br/>
./training_viewecc.aspx View ECC List <br/>
./training_deleteecc.aspx Delete an ECC <br/>
<br/>
Procedures/Forms/Aids<br/>
<!-- sublist for procedures --><br/>
./training_addprocedures.aspx Add a New Procedure <br/>
./training_modifyprocedures.aspx Update Procedure Info <br/>
./training_viewprocedures.aspx View Procedures <br/>
./training_deleteprocedure.aspx Delete a Procedure <br/>
<br/>
Revs<br/>
<!-- sublist for revs --><br/>
./training_addrev.aspx Add a New Rev <br/>
./training_modifyrevs.aspx Update Rev Info <br/>
./training_viewrev.aspx View Revs <br/>
./training_deleterev.aspx Delete a Rev <br/>
<br/>
Requirements<br/>
<!-- sublist for requirements --><br/>
./training_setrequirements.aspx Set Requirements <br/>
./training_viewrequirements.aspx View Reqs. By ECC <br/>
./training_viewrequirementsbyprocedure.aspx View Reqs. By Procedure <br/>
Delete a Requirement<br/>
<br/>
Training Sessions<br/>
<!-- sublist for training sessions --><br/>
./training_createsession.aspx Create a Session <br/>
Modify a Session<br/>
View Sessions<br/>
Delete a Session<br/>
<br/>
Miscellaneous Reports<br/>
<br/>
</td><br/>
<td align="center <br/>
<!-- dynamic content panel --><br/>
<asp:ContentPlaceHolder ID="mainContent" runat="server <br/>
</asp:ContentPlaceHolder><br/>
</td><br/>
</tr><br/>
</table><br/>
</body><br/>
</html>

View the full article
 
Back
Top