asp.net注册页面无法正常工作

时间:2016-08-31 13:30:31

标签: c# sql asp.net

我正在创建一个简单的注册页面,我得到一个错误,我认为这与我无法找到我创建的表有关,但我在本地创建了它。

这是我得到的错误:

  

发生了'System.Data.SqlClient.SqlException'类型的异常   System.Data.dll但未在用户代码中处理

     

其他信息:关键字“表格”附近的语法不正确。

任何帮助都会很棒。

下面我发布了迄今为止的代码:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Net.Mail;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection conn =
                new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            conn.Open();
            string checkuser = userchecker();
            SqlCommand com = new SqlCommand(checkuser, conn);
            int temp = changehere(com);
            conn.Close();
            if (temp == 1)
            {
                Response.Write("User Already Exists");
            }

        }
    }

    private string userchecker()
    {
        return "select count(*) from Table where UserName='" + TextBoxUN.Text + "'";
    }

    private static int changehere(SqlCommand com)
    {
        return Convert.ToInt32(com.ExecuteScalar().ToString());
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            Guid NEWguid = Guid.NewGuid();

            SqlConnection conn =
                new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            conn.Open();
            string insertQuery = "insert into Table (ID, UserName, Email, Password) values (@ID, @Uname , @email, @password)";
            SqlCommand com = new SqlCommand(insertQuery, conn);
            com.Parameters.AddWithValue("@ID", NEWguid.ToString());
            com.Parameters.AddWithValue("@Uname", TextBoxUN.Text);
            com.Parameters.AddWithValue("@email", TextBoxEmail.Text);
            com.Parameters.AddWithValue("@password", TextBoxPass.Text);
            com.ExecuteNonQuery();
            Response.Redirect("manager.aspx");
            Response.Write("Registration successful");
            conn.Close();
        }
        catch (Exception)
        {
            Response.Write("Error:");
        }
    }

    protected void TextBoxEmail_TextChanged(object sender, EventArgs e)
    {

    }
}

2 个答案:

答案 0 :(得分:1)

更改表的名称,表是SQL中的保留字

答案 1 :(得分:1)

试试这个:

private string userchecker()
{
    return "select count(*) from [Table] where UserName='" + TextBoxUN.Text + "'";
}

请参阅[]周围的Table,这是因为Table是所有SQL变体中的保留字,您应该将其转义

相关问题