2016-08-31 13 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

ですか? in 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

関連する問題