这是我第一次设计网站。我在连接数据库时遇到问题。没有按钮在页面上工作。最重要的是注册按钮。我正确地填写表格,但是当我按下注册按钮时,它不会将新用户注册到数据库中。它甚至没有显示我考虑过的任何错误消息。例如,它没有显示您之前已注册或您的注册未成功。我的数据库中没有错误消息和新记录。我删除了验证码,因为我认为这可能会导致问题。这是我的代码:
using System;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
public partial class SignUp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strname = Cache["TF"] as string;
if (strname != null)
{
(Master.FindControl("Lozv") as Label).Text = strname;
(Master.FindControl("LinkButton1") as LinkButton).Visible = true;
}
else
{
(Master.FindControl("Lozv") as Label).Text = "Guest";
(Master.FindControl("LinkButton1") as LinkButton).Visible = false;
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
string username = txtboxUser.Text;
SqlConnection sqlc = new SqlConnection("Data Source=.; Database=LDatabase; Integrated Security=True");
SqlCommand cmd = new SqlCommand("SELECT dbo.CheckUserName(@UN)");
cmd.Parameters.AddWithValue("@UN", txtboxUser.Text);
sqlc.Open();
Boolean User = Convert.ToBoolean(cmd.ExecuteScalar());
sqlc.Close();
if (User == false) ////////////// if user name is not in DB//////////////
{
SqlConnection sqlca = new SqlConnection();
sqlca.ConnectionString = "data source=. ; database=LDatabase ; integrated security=true";
SqlCommand cmda = new SqlCommand();
cmda.Connection = sqlca;
cmda.CommandText = "INSERT INTO User_Pass values(@UserName,@Pass,@Name,@LastName,@Email,@Date,@Sex,'0')";
cmda.Parameters.AddWithValue("@UserName", txtboxUser.Text);
cmda.Parameters.AddWithValue("@Pass", txtboxPass.Text);
cmda.Parameters.AddWithValue("@Name", txtboxName.Text);
cmda.Parameters.AddWithValue("@LastName", txtboxSurname.Text);
cmda.Parameters.AddWithValue("@Email", txtboxEmail.Text);
cmda.Parameters.AddWithValue("@Date", DateTime.Now);
cmda.Parameters.AddWithValue("@Sex", rbtnGender.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@manager", "No");
sqlca.Open();
int n= cmda.ExecuteNonQuery();
if (n <= 0)
LMsg.Text = "Your registration wasn't successful";
else
{
txtboxName.Text = "";
txtboxSurname.Text = "";
txtboxUser.Text = "";
txtboxPass.Text = "";
txtboxRePass.Text = "";
txtboxEmail.Text = "";
rbtnGender.SelectedIndex = -1;
LMsg.Text = "You registered successfully.";
}
sqlca.Close();
}
else //////////////if user name is in db//////////////
{
LMsg.Text = "This username has already registered.";
}
}
}
Captcha是否与此类问题有关?任何帮助将不胜感激。
答案 0 :(得分:0)
将这样的按钮放在aspx-markup中:
<asp:Button ID="btnRegister" runat="server" Click="Button1_Click1" Height="26px" Text="register" Width="88px"/>
它应该触发该方法。
编辑:或者在Page_Load方法中绑定事件(首先从按钮中删除Click-attribute - 从上面的例子开始)。
protected void Page_Load(object sender, EventArgs e)
{
btnRegister.Click += new EventHandler(Button1_Click1);
string strname = Cache["TF"] as string;
[...]