InvalidOperationException was unhandled by user code.

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 7
Reputation: msamir is an unknown quantity at this point 
Solved Threads: 0
msamir msamir is offline Offline
Newbie Poster

InvalidOperationException was unhandled by user code.

 
0
  #1
Feb 7th, 2007
I done login form in c# using login control.It gives runtime error near while loop .
error-'Invalid attempt to Read when reader is closed.'
Please give me solution bcoz i tried many options here.

using
System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
publicpartialclass_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
bool Autheticated = false;
Autheticated = SiteLevelCustomAutheticationMethod(Login1.UserName, Login1.Password);
e.Authenticated = Autheticated;
if (Autheticated == true)
{
Response.Write("Home.aspx");
}

}
private bool SiteLevelCustomAutheticationMethod (string UserName,string Password)
{
bool boolReturnValue = false;
string scon = "SERVER=.; INITIAL CATALOG=Villaplus; UID=sa; PWD=ash; ";
SqlConnection con = new SqlConnection(scon);
con.Open();
string strSQL = "Select * From Login";
SqlCommand com = new SqlCommand(strSQL, con);
SqlDataReader Dr;
Dr = com.ExecuteReader();
while (Dr.Read())
{
if ((UserName == Dr["Uname"].ToString()) & (Password == Dr["Pwd"].ToString()))
{
boolReturnValue = true;
}

Dr.Close();
}
return boolReturnValue;

}
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 802
Reputation: Phaelax is on a distinguished road 
Solved Threads: 40
Phaelax Phaelax is offline Offline
Practically a Posting Shark

Re: InvalidOperationException was unhandled by user code.

 
0
  #2
Feb 12th, 2007
My guess is that perhaps the information used to connect to the SQL server may be incorrect, and so its not actually establishing a connection. Also, to prevent the application from quitting, wrap the while loop in a try-catch block.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 79
Reputation: nikkiH is an unknown quantity at this point 
Solved Threads: 4
nikkiH's Avatar
nikkiH nikkiH is offline Offline
Junior Poster in Training

Re: InvalidOperationException was unhandled by user code.

 
0
  #3
Feb 13th, 2007
You have the close inside the read loop. Move it outside the loop.
Google is your friend. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
Bored? Visit http://www.kaelisspace.com/
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4660 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC