Hi Everyone,
I am in the process of doing a new registration for my website and I getting a error, which is as follows:
Server Error in '/' Application.
ExecuteScalar requires an open and available Connection. The connection's current state is closed.
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: ExecuteScalar requires an open and available Connection. The connection's current state is closed.
Source Error:
Line 34: String cmdStr2 = "Select Password from aspnet_UserMembership where UserName = '" + UserName.Text + "'";
Line 35: SqlCommand pass = new SqlCommand(cmdStr2, con);
Line 36: String Password = pass.ExecuteScalar().ToString(); Line 37:
Line 38:
|
Source File: c:\Genealogy\Login.aspx.cs Line: 36
Stack Trace:
[InvalidOperationException: ExecuteScalar requires an open and available Connection. The connection's current state is closed.]
System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +5288094
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +82
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
System.Data.SqlClient.SqlCommand.ExecuteScalar() +149
Login.Button1_Click(Object sender, EventArgs e) in c:\Genealogy\Login.aspx.cs:36
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9552874
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
|
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18055
I have created a new registration page, Register.aspx, and a new Table, aspnet_UserMembership, in the database. These are all working with no problem. It is my login page, Login.aspx, and the code page, Login.aspx.cs, which I am getting the error. The code for these pages is as follows below:
Page Login.aspx,
<%@ Page Title="" Language="C#" MasterPageFile="~/Register.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<br />
<link href="Styles/Content.css" rel="stylesheet" />
<div id="content">
<div style="text-align: center; height: 53px">
<span style="font-family: Arial, Helvetica, sans-serif; font-size: large"><strong>Welcome to Genealogy-Biography.com Login Page </strong></span>
</div>
<div style="text-align: justify">
If you are already a member, you can just login. If not a member yet, please press the register link below to register it is FREE!:</div>
<br />
<table cellpadding="0" cellspacing="0" style="width: 159%">
<tr>
<td style="background-color: #990000"> </td>
<td style="text-align: left; font-family: Arial, Helvetica, sans-serif; font-size: large; color: #9F9F6F; width: 163px; background-color: #990000"><strong>Login<br />
<br />
</strong></td>
<td style="background-color: #FFFFFF">
</td>
</tr>
<tr>
<td style="text-align: right"> </td>
<td style="width: 163px"> </td>
<td> </td>
</tr>
<tr>
<td style="text-align: right; font-family: Arial, Helvetica, sans-serif; font-size: medium; height: 22px; border-left-color: #A0A0A0; border-right-color: #C0C0C0; border-top-color: #A0A0A0; border-bottom-color: #C0C0C0"><b>User Name:</b></td>
<td style="width: 163px; height: 22px">
<asp:TextBox ID="UserName" runat="server" style="margin-left: 0px" Width="200px"></asp:TextBox>
</td>
<td style="height: 22px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="UserName" ErrorMessage="User Name Required......" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="text-align: right; font-family: Arial, Helvetica, sans-serif; font-size: medium; border-left-color: #A0A0A0; border-right-color: #C0C0C0; border-top-color: #A0A0A0; border-bottom-color: #C0C0C0"><b>Password:</b></td>
<td style="width: 163px">
<asp:TextBox ID="Password1" runat="server" TextMode="Password" Width="200px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="Password1" ErrorMessage="Password Required......." ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="text-align: right"> </td>
<td style="width: 163px"> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td style="width: 163px; text-align: right">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Login" />
</td>
<td>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Register.aspx">Register</asp:HyperLink>
</td>
</tr>
</table>
<br />
<br />
<br />
<br />
</div>
<div id ="content1"></div>
<div id="ad"></div>
</asp:Content>
Login.aspx.cs,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["GenealogyConnectionString1"].ConnectionString);
con.Open();
String cmdStr = "Select count (*) from aspnet_UserMembership where UserName = '" + UserName.Text + "'";
SqlCommand Checkuser = new SqlCommand(cmdStr, con);
int temp = Convert.ToInt32(Checkuser.ExecuteScalar().ToString());
con.Close();
if (temp == 1)
{
String cmdStr2 = "Select Password from aspnet_UserMembership where UserName = '" + UserName.Text + "'";
SqlCommand pass = new SqlCommand(cmdStr2, con);
String Password = pass.ExecuteScalar().ToString();
if (Password == Password1.Text)
{
Session["New"] = UserName.Text;
Response.Redirect("Default.aspx");
}
else
{
Response.Redirect("ErrorExist.aspx");
}
}
else
{
Response.Redirect("ErrorExist.aspx");
}
}
}
I have tried opening the connection and that did not work, can anyone help with this error?
Thanks, your help would be much appreciated, and look forward to hearing from you in the near future.
Thanks
ASW