2016-04-05 101 views
0

這是我的Registration.aspx文件的編碼。每當我點擊提交,我得到System.Data.SqlClient.SqlException:關鍵字'Table'附近的語法不正確

System.Data.sqlclient.sqlexception:關鍵字「表」

附近的語法不正確這是代碼的一部分。

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

public partial class Registration : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (IsPostBack) 
     { 
      SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString); 
      conn.Open(); 
      // This is the part im getting error in. 
      string checkuser = "select count(*) from Table where UserName ='" + uname.Text + "'"; 
      SqlCommand com = new SqlCommand(checkuser, conn); 
      int temp = Convert.ToInt32(com.ExecuteScalar().ToString()); 
      if (temp == 1) 
      { 
       Response.Write("User Already Exists"); 
      } 

      conn.Close(); 
     } 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString); 
      conn.Open(); 
      string insertQuery = "insert into Table (UserName,Email,Password,Country) values (@uname ,@email ,@pass ,@country)"; 
      SqlCommand com = new SqlCommand(insertQuery, conn); 
      com.Parameters.AddWithValue("@uname", uname.Text); 
      com.Parameters.AddWithValue("@email", email.Text); 
      com.Parameters.AddWithValue("@pass", pwd.Text); 
      com.Parameters.AddWithValue("@country", DropDownList1.SelectedItem.ToString()); 
      com.ExecuteNonQuery(); 
      Response.Redirect("Manager.aspx"); 
      Response.Write("Registration Successful"); 
      conn.Close(); 
     } 
     catch (Exception ex) 
     { 
      Response.Write("Error:" + ex.ToString()); 
     } 
    } 

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
    } 
} 
+0

這個問題與jquery和visual studio無關。請注意適當地標記您的問題。 –

+0

你確定你的表名是'Table'嗎? –

+0

是@KartikeyaKhosla –

回答

1

Table是一個保留字,在這兩個查詢使用括號:

"select count(*) from [Table] where UserName ='" + uname.Text + "'"; 

"insert into [Table] (UserName,Email,Password,Country) values (@uname ,@email ,@pass ,@country)"; 
+0

不是你的錯,但這不是MySql(SqlConnection) – Steve

+0

@Steve你是對的,修正了:P – sagi

0

表是一個關鍵字,所以我不就可以在這種情況下使用,因此請確保您的SQL查詢是正確

和MySQL不使用

using System.Data.SqlClient; 
0

更改查詢如下,

string insertQuery = "insert into [Table] (`UserName`,`Email`,`Password`,`Country`) values (@uname ,@email ,@pass ,@country)"; 
+0

否@Piyush再次出現相同的錯誤。 –

相關問題