Click here to Skip to main content
15,921,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is my coding,

protected void Button1_Click(object sender, EventArgs e)
        {
            

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["aspdbConnectionString"].ToString());
            SqlCommand cmd = new SqlCommand("select * from lgtab where Username='"+usr.Value+"' and Password='"+pwd.Value+"'", con);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Login Successfully')", true);
                usr.Value = "";
                pwd.Value = "";
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Login Failed')", true);
                usr.Value = "";
                pwd.Value = "";
            }
            con.Close();
        }


I am use local db for login page, but it can't read. Please Help Something...

What I have tried:

I try above code but its shows following exception,

An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code

Additional information: ExecuteReader requires an open and available Connection. The connection's current state is closed.
Posted
Updated 20-Dec-20 22:43pm

1 solution

Entire error is quite self explanatory.

You are using SQLReader instance to read the data but have not opened up the connection with database to do so. Would recommend you to go through ADO.NET basics and then try again.

Regarding the data communication can be read here:
Accessing data with ADO.NET[^]
MSDN: Retrieving Data Using a DataReader (ADO.NET)[^]
MSDN: DataReader Class[^]
Beginners guide to accessing SQL Server through C#[^]

Sample:
C#
using (connection)
{
    SqlCommand command = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories;", connection);
    connection.Open();

    SqlDataReader reader = command.ExecuteReader();

    if (reader.HasRows)
    {
        while (reader.Read())
        {
            Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
                reader.GetString(1));
        }
    }
    else
    {
        Console.WriteLine("No rows found.");
    }
    reader.Close();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900