2016-04-05 23 views
0

これは私のRegistration.aspxファイルのコーディングです。これは、コードの一部であるキーワード「テーブル」の近くに不正な構文System.Data.SqlClient.SqlException:キーワード 'テーブル'の近くの構文が正しくありません

:私は

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は、この2つのクエリにブラケットを使用し、予約語である:

"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

@スティーブあなたは正しいです、固定: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もう一度同じエラーが発生します。 –

関連する問題