ConnectionString 属性尚未初始化
using System.Web.UI.WebControls;using System.Data.SqlClient;
using System.Configuration;
public partial class Default2 : System.Web.UI.Page
{
protected bool fangfa(string k)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["con"]);
con.Open();
string sqlsel = "select count(*) from tb_User where Name='" + k + "'";
SqlCommand com = new SqlCommand(sqlsel, con);
if (Convert.ToInt32(com.ExecuteScalar()) > 0)
{
return true;
}
else
{
return false;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
if (fangfa(TextBox1.Text))
{
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('用户名已存在')</script>");
}
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('可以注册')</script>");
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('用户名不能为空')</script>");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string A = TextBox1.Text;
string B = TextBox2.Text;
string C = TextBox3.Text;
string D = TextBox4.Text;
string E = TextBox5.Text;
string F = TextBox6.Text;
if (!fangfa(TextBox1.Text))
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["con"]);
con.Open();
string sql = "insert into tb_User values('" + A + "','" + B + "','" + C + "','" + D + "','" + E + "','" + F + "','" + DateTime.Now.ToString() + "')";
SqlCommand com = new SqlCommand(sql, con);
if (Convert.ToInt32(com.ExecuteScalar()) > 0)
{
ClientScript.RegisterStartupScript(this.GetType(), "true", "<script>alert('注册成功')</script>");
fangfa2(A, B, F);
TextBox1.Text = TextBox2.Text = TextBox1.Text = TextBox3.Text = TextBox4.Text = TextBox5.Text = TextBox6.Text = "";
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "false", "<script>alert('注册失败')</script>");
}
con.Open();
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('用户名已存在')</script>");
}
}
protected bool fangfa2(string A, string B,string F)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["con"]);
con.Open();
string sqls = "select count(*) from tb_User where Name='" + A + "'and Pass='" + B + "'and Email='"+F+"'";
SqlCommand com = new SqlCommand(sqls, con);
if (Convert.ToInt32(com.ExecuteScalar()) > 0)
{
return true;
}
else
{
return false;
}
}
}