我被困在一个asp.net登录表单

本文关键字:asp 一个 net 登录 表单 | 更新日期: 2023-09-27 18:09:23

我已经创建了asp.net登录表单。问题是,我想检查用户名是否存在于SQL数据库中,但我甚至不能连接到数据库。错误信息是:

Conversion failed when converting the varchar value 'System.Web.UI.WebControls.TextBox' to data type int.

这是我的代码:

protected void Page_Load(object sender, EventArgs e)
{
    lblmessage.Visible = false;
    if (!IsPostBack)
    {
        lblsec.Text = Userutility.generateSecurity();
    }
}
protected void registerButton_Click(object sender, EventArgs e)
{
    if (txtusername.Text =="" || txtpassword.Text=="" || TextBox2.Text ==""|| sec_code.Text == "" )
    {
        lblmessage.Text = "لطفا فیلد هارا پر کنید";
        lblmessage.Visible = true;
    }
    else if (!Regex.IsMatch(txtusername.Text,@"^[a-zA-Z]{5,50}$"))
    {
        lblmessage.Text = "نام کاربری باید حداقل 5 کاراکتر و از کاراکتر های مجاز استفاده شود";
        lblmessage.Visible = true;
    }
    else if (!Regex.IsMatch(txtmail.Text, @"'w+([-+.]'w+)*@'w+([-.]'w+)*'.'w+([-.]'w+)*"))
    {
        lblmessage.Text = "ایمیل وارد شده معتبر نمی باشد";
        lblmessage.Visible = true;
    }
    else if (txtpassword.Text != TextBox2.Text)
    {
        lblmessage.Text = "کلمه عبور شما تطابق ندارد";
        lblmessage.Visible = true;
    }
    else if (Userutility.user_login_exist(txtusername.Text))
    {
        lblmessage.Text = "این نام کاربری موجود میباشد";
        lblmessage.Visible = true;
    }
    else if (Convert.ToInt16(sec_code.Text) != Convert.ToInt16(Session["sec_code"].ToString()))
    {
        lblmessage.Text = "عبارت امنیتی صحیح نمی باشد";
        lblsec.Text = Userutility.generateSecurity();
        lblmessage.Visible = true;
    }
    else 
    {
        SqlConnection cnn = new SqlConnection("Data Source=.;Initial Catalog=EnglishDB;Integrated Security=true");
        SqlCommand cmd = new SqlCommand();
        cmd.Connection=cnn;
        string sql = @"Insert into student (Username,FirstName,LastName,Age,Major,City,Country,Password,Email,Cellphone)";
        sql += " VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')";
        sql = string.Format(sql, txtusername, txtlastname, txtlastname, txtage, txtmajor, txtcity, txtcountry, txtpassword, txtmail, txtcellphone);
        cnn.Open();
        cmd.CommandText = sql;
        cmd.ExecuteNonQuery();
        cnn.Close();
    }
}

我被困在一个asp.net登录表单

您没有提到sql查询中的文本框的.Text属性。相反,你提到了控件名。

string.Format(sql, txtusername.Text ... 
更好的方法是使用参数化查询
 using( SqlConnection cnn = new SqlConnection("Data Source=.;Initial Catalog=EnglishDB;Integrated Security=true"))
 {
    cnn.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection=cnn;
    string sql = @"Insert into student (Username,FirstName,LastName,Age,Major,City,Country,Password,Email,Cellphone)";
    sql += " VALUES(@Username,@FirstName,@LastName,@Age,@Major,@City,@Country,@Password,@Email,@Cellphone)";

    cmd.CommandText = sql;
    cmd.Parameters.AddWithValue(@Username, txtusername.Text);
    cmd.Parameters.AddWithValue(@FirstName, txtfirstname.Text);
    cmd.Parameters.AddWithValue(@LastName, txtlastname.Text);
    cmd.Parameters.AddWithValue(@Age, txtage.Text);
    cmd.Parameters.AddWithValue(@Major, txtmajor.Text);
    cmd.Parameters.AddWithValue(@City, txtcity.Text);
    cmd.Parameters.AddWithValue(@Country, txtcountry.Text);
    cmd.Parameters.AddWithValue(@Password, txtpassword.Text);
    cmd.Parameters.AddWithValue(@Email, txtmail.Text);
    cmd.Parameters.AddWithValue(@Cellphone, txtcellphone.Text);
    cmd.ExecuteNonQuery();        
 }

似乎你在单引号中也给出了int的价值。SqlCommand参数应该可以解决这里的所有问题。

您的年龄字段显然是数据类型int。你得到它的值从一个文本框,因此一个字符串,所以你正试图插入一个字符串到一个int字段

使用:

int age=int.Parse(txtage);

,然后作为参数传递:

string sql = @"Insert into student(Username,FirstName,LastName,Age,Major,City,Country,Password,Email,Cellphone)  VALUES(@Usr,@Fname,@LastName,@Age,@Major,@City,@Country,@Pass,@Email,@Cell)";
cmd.Parameters.AddWithValue("@Usr",txtusername);
//...
cmd.Parameters.AddWithValue("@age",age);

尝试用下面的代码替换您的那部分代码。

string sql = @"Insert into student (Username,FirstName,LastName,Age,Major,City,Country,Password,Email,Cellphone)";
            sql += " VALUES('{0}','{1}','{2}',{3},'{4}','{5}','{6}','{7}','{8}','{9}')";
            sql = string.Format(sql, txtusername.Text, txtlastname.Text, txtlastname.Text, txtage.Text, txtmajor.Text, txtcity.Text, txtcountry.Text, txtpassword.Text, txtmail.Text, txtcellphone.Text);