主题:[讨论]ExecuteScalar: Connection 属性尚未初始化问题
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string username = Request.Form["username"].ToString();
string userpwd = Request.Form["userpwd"].ToString();
SqlConnection con = new SqlConnection("server=(local);database=login;uid=sa;pwd=;");
con.Open();
SqlCommand cmd = new SqlCommand("select count(*) from login where username='" + username + "and userpwd='" + userpwd + ",con");
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
Response.Redirect("main.aspx");
}
else
{
Response.Redirect("loginfail.htm");
}
}
}
程序运行出现错误,
错误提示:ExecuteScalar: Connection 属性尚未初始化
请教此问题如何解决
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string username = Request.Form["username"].ToString();
string userpwd = Request.Form["userpwd"].ToString();
SqlConnection con = new SqlConnection("server=(local);database=login;uid=sa;pwd=;");
con.Open();
SqlCommand cmd = new SqlCommand("select count(*) from login where username='" + username + "and userpwd='" + userpwd + ",con");
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
Response.Redirect("main.aspx");
}
else
{
Response.Redirect("loginfail.htm");
}
}
}
程序运行出现错误,
错误提示:ExecuteScalar: Connection 属性尚未初始化
请教此问题如何解决