| | |
InvalidOperationException was unhandled by user code.
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 7
Reputation:
Solved Threads: 0
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;
}
}
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;
}
}
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/
Bored? Visit http://www.kaelisspace.com/
![]() |
Similar Threads
- Database Connectivity in C (C)
- ASP.Net YAF Application Error (ASP.NET)
- YAF Application Run time error (ASP.NET)
- Beginner: Insert, Update, Delete & Search records from/to sql database (ASP.NET)
- Prblem with sql sever connection (ASP.NET)
- Using passwd in a perl file to automate user creation (Perl)
Other Threads in the C# Forum
- Previous Thread: C# .NET Gridview
- Next Thread: Reset Font in RTB
Views: 4660 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap broadcast button buttons c# check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption event excel file files form format forms ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





