从文本字段插入表格 - 空字段

时间:2013-02-05 10:26:57

标签: c# asp.net sql-server-express

我遇到了一些问题:下面的代码用新行更新了表格,但此行中的所有字段都是空的。我想将文本字段中的输入数据放入表Members中的新行。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Default4 : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
    {
        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtAdress.Text = "";
        txtResidence.Text = "";
        txtEmail.Text = "";
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(
            WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("INSERT INTO Members(FirstName, LastName, Adress, Residence, Email) VALUES (@txtFirstName, @txtLastName, @txtAdress, @txtResidence, @txtEmail)", con);
        cmd.Parameters.AddWithValue("@txtFirstName", txtFirstName.Text);
        cmd.Parameters.AddWithValue("@txtLastName", txtLastName.Text);
        cmd.Parameters.AddWithValue("@txtAdress", txtAdress.Text);
        cmd.Parameters.AddWithValue("@txtResidence", txtResidence.Text);
        cmd.Parameters.AddWithValue("@txtEmail", txtEmail.Text);

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();

            lblConfirm.Text = "Database updated, insert confirmed!";

            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtAdress.Text = "";
            txtResidence.Text = "";
            txtEmail.Text = "";
        }
        finally
        {
            con.Close();
        }
    }
}

如果我更改此行:

SqlCommand cmd = new SqlCommand("INSERT INTO Members(FirstName, LastName, Adress, Residence, Email) VALUES (@txtFirstName, @txtLastName, @txtAdress, @txtResidence, @txtEmail)", con);

对于这样的事情:

SqlCommand cmd = new SqlCommand("INSERT INTO Members(FirstName, LastName, Adress, Residence, Email) VALUES ('@txtFirstName', '@txtLastName', @txtAdress, @txtResidence, @txtEmail)", con);

它将打印出一行包含以下字段: | @txtFirstName | @txtLastName | [空] | [空] | [空] |

任何帮助都会很棒,谢谢!

2 个答案:

答案 0 :(得分:1)

每次按下注册按钮,Page_Load执行,清空文本框,然后执行btnRegister_Click。此时您的文本框是空白的。

您需要在Page_Load事件中添加一项检查

   if(!IsPostBack)
   {
      ... // code to empty the textboxes
   }

Page.IsPostBack property on MSDN

答案 1 :(得分:0)

继承问题......

    try
    {
        con.Open();
        cmd.ExecuteNonQuery();

        lblConfirm.Text = "Database updated, insert confirmed!";

        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtAdress.Text = "";
        txtResidence.Text = "";
        txtEmail.Text = "";
    }

^^^^^^这里重置所有字段没有区别。每次回发后都会发生这种情况。

试试这个

if(!IsPostBack)
   {
            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtAdress.Text = "";
            txtResidence.Text = "";
            txtEmail.Text = "";
   }

因此,点击按钮时用户输入不会重置为空。

在这里有更好的方法来实现你的目标......