程序代码如下:using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;//连接数据库配置web.configusing System.Data.SqlClient;//你用的是SQL数据库
namespace login1
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Labusr;
protected System.Web.UI.WebControls.Label Labpwd;
protected System.Web.UI.WebControls.Button Btnok;
protected System.Web.UI.WebControls.Button Btnclear;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox Txtpwd;
protected System.Web.UI.WebControls.TextBox Txtusr;

       private SqlConnection connsql =  null;

private void Page_Load(object sender, System.EventArgs e)
{
/*if (!IsPostBack) 
{

sqlCon = new SqlConnection();
}*/
// 在此处放置用户代码以初始化页面 }
/// <summary>

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.Btnclear.Click += new System.EventHandler(this.Btnclear_Click);
this.Btnok.Click += new System.EventHandler(this.Btnok_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Btnok_Click(object sender, System.EventArgs e)
{


//SqlCommand cmdOrder=new SqlCommand();
string strTxt1 = Txtusr.Text;
string strTxt2= Txtpwd.Text;
string strWhere="";
if(strTxt1!="" && strTxt2!="")
{
strWhere=" AccountId = '"+strTxt1+"' AND Usrpwd='"+strTxt2+"'";



connsql = new SqlConnection();
try
{
connsql.Open();
SqlCommand cmdOrder=new SqlCommand();
cmdOrder.CommandText="SELECT accountid FROM sUser  "+strWhere+"    ";
cmdOrder.Connection=connsql;
SqlDataReader readerOrder=cmdOrder.ExecuteReader();



if(readerOrder.Read())
{
Response.Write("Frame.aspx");

connsql.Close();
}
//捕获异常
catch(Exception ex)
{
Response.Write(ex.Message);
}
//断开数据库连接
finally
{
if(connsql.State!=ConnectionState.Closed)
connsql.Close();
}

} private void Btnclear_Click(object sender, System.EventArgs e)
{
Txtusr.Text="";
Txtpwd.Text="";
}
}
}web.config 里的内容为:<appSettings>
<add key="ConnectString" value="data source=.;initial catalog=cheshi;persist security info=False;user id=sa;password=jerrysun;packet size=8192;connect timeout=60" />
</appSettings>

解决方案 »

  1.   

    sqlCon = new SqlConnection();
    这里你没有实例化,当然也可以后面实例化要读取Web.config文件中的可以这样写
    sqlCon=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectString"].ToString());
      

  2.   

    connsql = new SqlConnection();
    这一行改为
    connsql = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectString"].ToString());
      

  3.   

    呵呵
    以前做asp的最近看,net