Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有载事件问题_C#_Asp.net_Sql_Onload Event - Fatal编程技术网

C# 有载事件问题

C# 有载事件问题,c#,asp.net,sql,onload-event,C#,Asp.net,Sql,Onload Event,当我想更新数据库中的一行时,我遇到了一个问题。更新的页面还添加了一个客户端,但问题是当页面加载检测到更新按钮被按下时,它似乎一直在加载变量,我无法更新我的数据库 public partial class CustomerInput : System.Web.UI.Page { string update, Id, Name, Address, Suburb, Postcode, Age, Email; protected void Page_Load(object sender

当我想更新数据库中的一行时,我遇到了一个问题。更新的页面还添加了一个客户端,但问题是当页面加载检测到更新按钮被按下时,它似乎一直在加载变量,我无法更新我的数据库

public partial class CustomerInput : System.Web.UI.Page
{
    string update, Id, Name, Address, Suburb, Postcode, Age, Email;

    protected void Page_Load(object sender, EventArgs e)
    {
        update = Request.QueryString["Update"];
        if (update == "true")
        {
            SqlConnection connection = new SqlConnection("server=localhost; uid=xxxx; pwd=xxxx; database=Customer");
            Button1.Text = "Update";

            Id = Request.QueryString["Id"];
            connection.Open();

            SqlCommand command = new SqlCommand("Select * from Customer where Id = " + Id, connection);

            SqlDataReader read = command.ExecuteReader();
            read.Read();
            TextBox1.Text = read[1].ToString();
            TextBox2.Text = read[2].ToString();
            TextBox3.Text = read[3].ToString();
            TextBox4.Text = read[4].ToString();
            TextBox5.Text = read[5].ToString();
            TextBox6.Text = read[6].ToString();

            connection.Close();
            update = string.Empty;
        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection connection = new SqlConnection("server=localhost; uid=xxxx; pwd=xxxx; database=Customer");

        if (Button1.Text == "Update")
        {
            connection.Open();
            SqlCommand command;

            Name = TextBox1.Text;
            Address = TextBox2.Text;
            Suburb = TextBox3.Text;
            Postcode = TextBox4.Text;
            Age = TextBox5.Text;
            Email = TextBox6.Text;


            command = new SqlCommand("UPDATE Customer SET Name = " + "'" + Name + "', " + "Address = " + "'" + Address + "', " + "Suburb = " + "'" + Suburb + "', "
                    + "Postcode = " + "'" + Postcode + "', " + "Age = " + "'" + Age + "', " + "Email = " + "'" + Email + "' " + "Where Id =" + Id, connection);

            command.ExecuteNonQuery();
            connection.Close();

        }
        if (Button1.Text == "New Client")
        {
            Name = TextBox1.Text;
            Address = TextBox2.Text;
            Suburb = TextBox3.Text;
            Postcode = TextBox4.Text;
            Age = TextBox5.Text;
            Email = TextBox6.Text;

            Response.Write("Blah");

            SqlCommand command = new SqlCommand("INSERT INTO Customer VALUES (" + "'" + Name + "'" + ", " +  "'" + Address + "'"  + ", " +  "'" + Suburb + "'"  + ", " 
                +  "'" + Postcode + "'"  + ", " +  "'" + Age + "'"  + ", " +  "'" + Email + "'"  + ")", connection);

            command.ExecuteNonQuery();
        }

        Button1.Text = "New Client";
    }
}

}

在页面加载事件开始时,您需要添加一条if语句,以检查这是否是页面第一次加载:

例如:

if (!IsPostBack)
{
    ... add your code here
}

在页面加载事件开始时,您需要添加if语句,以检查这是否是页面首次加载:

例如:

if (!IsPostBack)
{
    ... add your code here
}

我想您需要使用
页面。IsPostBack

if (Page.IsPostBack)
{
     // Do Something ..
{
else
{
     // Do something else ..

}

我想您需要使用
页面。IsPostBack

if (Page.IsPostBack)
{
     // Do Something ..
{
else
{
     // Do something else ..

}

似乎一直在加载变量
什么变量?为什么无法更新数据库?请检查页面中的加载。
似乎一直在加载变量
什么变量?为什么无法更新数据库?请检查页面中的加载。