2016-08-31 41 views
1

我創建一個簡單的註冊頁面我得到我相信有不能夠找到我所創建的表做一個錯誤,但我在當地做到了。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) 
    { 

    } 
} 
+0

你的表名是什麼? – SilentCoder

+0

dbo.Table是我的表名 – bootle

+0

當你得到這個例外?在userchecker()? – Forlani

回答

1

試試這個:

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

[]周圍Table,這是因爲表是保留字中的所有SQL變種,你應該逃避它

+0

總是有一些小.... – bootle

+0

謝謝!!!!!! – bootle

+0

總是很小..很高興幫助! – Forlani

1

更改表的名稱,表是在SQL

的保留字
+0

wooooooooow感謝傢伙 - 總是有一些小.... – bootle