0

別のページで値を表示するように設定した後SqlDataSource、コメントページにテスト値を2回入力すると、2つの空白として表示されます。

SQL Serverデータベース値のテーブルにそれらを取り込む際に何かが欠けていると思いますか?

ここで必要な情報がわかりませんので、お知らせください。

前もって感謝します

CODEのユーザーリクエストの編集#1

protected void btnSend_Click(object sender, EventArgs e)
{
    Page.Validate("vld2");
    SendMail();
    lblMsgSend.Visible = true;

    //SQL Server Database
    SqlConnection conn; //manages connection to database
    SqlCommand cmd; //manages the SQL statements

    string strInsert; //SQL INSERT Statement

    try
    {
            //create a connection object
            conn = new SqlConnection("Data Source=localhost\\sqlexpress;" +
                                     "Initial Catalog=RionServer;" +
                                     "Integrated Security=True;");
            //Build the SQL INSERT Document
            strInsert = "INSERT INTO CommentsAdmin (Name,Phone,Email,Comments)"
                + "VALUES(@Name,@Phone,@Email,@Comments);";

            //associate the INSERT statement with the connection
            cmd = new SqlCommand(strInsert, conn);

            //TELL the SqlCommand WHERE to get the data from
            cmd.Parameters.AddWithValue("Name", txtName.Text);
            cmd.Parameters.AddWithValue("Phone", txtPhone.Text);
            cmd.Parameters.AddWithValue("Email", txtEmail.Text);
            cmd.Parameters.AddWithValue("Comments", txtComment.Text);

            //open the connection
            cmd.Connection.Open();

            //run the SQL statement
            cmd.ExecuteNonQuery();

            //close connection
            cmd.Connection.Close();

            //display status message on the webpage
            lblMsgSend.Text = "Thank you for the comment! Please hit the 'Return to Main Page' to return to the Main Page!";
        }
        catch (Exception ex)
        {
            lblMsgSend.Text = ex.Message;
        }

    txtPhone.Text = "";
    txtEmail.Text = "";
    txtName.Text = "";
    txtComment.Text = "";
}

編集#2

データベース内のName、Phone、Email、Commentsの値は空のようですが、クエリをテストすると、SQLに値を取得するのではなく、エントリを登録していると思いますか?

編集#3

coderとrsの提案により、私は彼らの言ったことを実行しました。そして今、私はこのエラーを受け取ります。「文字列またはバイナリデータは切り捨てられます。ステートメントは終了しました。」コードも更新されました。

編集#4

この質問は、SQLServerエラーのフォローアップです。 「キーワードはサポートされていません」「データソース」

4

1 に答える 1

1

サーバーにnull値を入力しているので、SQLに値を入力する前に""、これに似たものをすべて削除してください。txtPhone.Text = "";したがって、テキストボックスにいくつかの値を指定しても、事前定義された NULL 値が使用され、いずれも入力されません。

于 2013-02-18T03:58:26.360 に答える