2016-10-15 2 views
-1

私は、私は次のエラーを取得していますマイクロソフトのVisual Studio 2015で作成されたasp.netのフォームを使用してデータベースにユーザー・データを挿入しようとしています:C#を使用して、データベースasp.netに挿入することができません - 未処理の例外

Exception Details: System.Web.HttpException: Unable to find control id 'txtName' referenced by the 'ControlToValidate' property of 'reqName'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[HttpException (0x80004005): Unable to find control id 'txtName' referenced by the 'ControlToValidate' property of 'reqName'.]
System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(String name, String propertyName) +183
System.Web.UI.WebControls.BaseValidator.ControlPropertiesValid() +36
System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) +36
System.Web.UI.Control.PreRenderRecursiveInternal() +166
System.Web.UI.Control.PreRenderRecursiveInternal() +236
System.Web.UI.Control.PreRenderRecursiveInternal() +236
System.Web.UI.Control.PreRenderRecursiveInternal() +236
System.Web.UI.Control.PreRenderRecursiveInternal() +236
System.Web.UI.Control.PreRenderRecursiveInternal() +236
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4713

C#のコードは、データベースにユーザー・データを挿入するには:

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 _Default : System.Web.UI.Page 
{ 
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C: \Users\P\Docs\Visual Studio 2015\WebSites\WebSite2\App_Data\Database.mdf;Integrated Security=True"); 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void Unnamed1_Click(object sender, EventArgs e) 
    { 
     con.Open(); 
     SqlCommand cmd = con.CreateCommand(); 
     cmd.CommandType = System.Data.CommandType.Text; 
     cmd.CommandText = "insert into Table values('" + pName.Text + "','" + pEmail.Text + "')"; 
     cmd.ExecuteNonQuery(); 

     con.Close(); 
    } 
} 

私のデータベースファイル2列名(はpName)および電子メールのために他に1つ(pEmail)が

+0

txtNameとして定義されたコントロールはありますか? –

+2

このエラーはあなたの挿入とは関係ありません。 – Mahdi

+0

私はtxtNameとして定義された何かを見つけることができませんでした... pName.Textを除いて、私はそれに近づくでしょう。問題の原因となっているものについての考えは何ですか? – penmas

答えて

1

[HttpException (0x80004005): Unable to find control id 'txtName' referenced by the 'ControlToValidate' property of 'reqName'.]

メッセージは、検証のためにページ上!reqName制御上、ControlToValidateパラメータに存在していないコントロール(txtName)を定義しているあなたのaspxページ上の(ではないの背後にあるコードで)、明らかである....

+0

ControlToValidateを解決するコードを更新しましたが、スタックの空のエラーが発生しました。どのように私は正常にフォームにデータを挿入するために電流を微調整することができますか? – penmas

+0

@penmas新しい質問をしてください。あなたは実際に完全に質問を変えるので... – Aristos

関連する問題