2017-07-25 5 views
1

これはWebフォームアプリケーションであり、正確に何が問題になっているかを見つけるのに苦労しています。webformsのこの挿入クエリはエラーを投げていますが、なぜ私はわかりません

loginsテーブルに情報を挿入しようとしています。

私は[User]を追加したときとは別にすべてが動作します。これはその時にログインしたユーザーのユーザー名です。 usernameには別のエントリがありますが、うまくいきます。背後

Image Of The Logins Table

Aspx.csコード:

protected void add_Click(object sender, EventArgs e)  
{ 
     string currentUser = User.Identity.Name; 

     int webid = Convert.ToInt32(ddlWebsite.SelectedItem.Value); 
     string username = username.Text; 
     string _email = email.Text; 
     string pass = password.Text; 
     string Ainfo = info.Text; 
     string loguser = User.Identity.Name; 

     //string connectionstring = "Data Source=(LocalDb)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebProg-20170720092452.mdf;Initial Catalog=aspnet-WebProg-20170720092452;Integrated Security=true"; 
     SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()); 

     string query = "Insert into Logins(WebsiteID, Username, Password, AdditionalInfo, User, Email) Values (@webid, @username, @pass, @Ainfo, @loguser, @_email)"; 

     SqlCommand insertQuery = new SqlCommand(query,connection); 

     insertQuery.Parameters.AddWithValue("@webid", webid); 
     insertQuery.Parameters.AddWithValue("@username", username); 
     insertQuery.Parameters.AddWithValue("@pass", pass); 
     insertQuery.Parameters.AddWithValue("@Ainfo", Ainfo); 
     insertQuery.Parameters.AddWithValue("@loguser", User.Identity.Name); 
     insertQuery.Parameters.AddWithValue("@_email", _email); 

     connection.Open(); 
     insertQuery.ExecuteNonQuery(); 
     connection.Close(); 
    } 

ASPXマークアップ:

ログイン

<div id="websiteDetails" class="col-md-4"> 

     <label id="Label1" for="websiteName"> 
      Website Name:    

      <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [Id], [Name] FROM [WebsiteList]"></asp:SqlDataSource> 
      <asp:DropDownList ID="ddlWebsite" runat="server" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Id"></asp:DropDownList> 

      <asp:Button ID="Button1" runat="server" Text="AddAWebsite" /> 
     </label> 
     <label id="Label3" for="username"> 
      Username/Email: 
      <asp:TextBox ID="username" runat="server"></asp:TextBox> 
     </label> 
     <label id="Label4" for="password"> 
      Password: 
      <asp:TextBox ID="password" runat="server"></asp:TextBox> 
     </label> 
     <label id="Label6" for="email"> 
      Email Address: 
      <asp:TextBox ID="email" runat="server"></asp:TextBox> 
     </label> 
     <label id="Label5" for="info"> 
      Additional Info: 
      <asp:TextBox ID="info" runat="server"></asp:TextBox> 
     </label> 
    </div> 
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"></asp:SqlDataSource> 
    <div id="loginDetails" class="col-md-4"> 
     <p>Website not showing up in the list? Add a Website to the list.</p> 
     <label id="Label7" for="websiteName"> 
      Website Name: 
      <asp:TextBox ID="Wname" runat="server"></asp:TextBox> 
     </label> 
     <label id="Label8" for="link"> 
      Website Address: 
      <asp:TextBox ID="link" runat="server"></asp:TextBox> 
     </label> 
    </div> 
    <asp:Button ID="AddWebsite" runat="server" Text="Add Website" OnClick="AddWebsite_Click" /> 



</div> 
<asp:Button ID="add" runat="server" Text="Add Login" class="btn btn-default" OnClick="add_Click" /> 

This is the error message that doesn't tell me much at all

を追加
+0

データベース内の列の種類は何を使ってエスケープANSIスタイルを使用する場合は[User]のようにエスケープする必要がありますか? –

+0

パラメータとして追加する変数の値は何ですか? –

+0

エラーは実際に何が間違っているかに関して非常に明確です。 Rahulの答えを参照してください。 – BugFinder

答えて

4

井戸Userは予約語で、投稿されたINSERTの声明に示されているように、これが問題です。それはSQL Serverのですか二重引用符に

Insert into Logins(WebsiteID, Username, Password, AdditionalInfo, [User], Email) 
+0

はい、これは問題のおかげです。 – Seangol1

+0

@ Seangol1の場合は、チェックボタンをクリックして回答を受け入れることを検討してください – Rahul

関連する問題