Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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
Sql server 数据库更新_Sql Server_Sql Server 2008_Database Update - Fatal编程技术网

Sql server 数据库更新

Sql server 数据库更新,sql-server,sql-server-2008,database-update,Sql Server,Sql Server 2008,Database Update,我有一个web应用程序,当页面加载时,地址详细信息从数据库中提取并显示在相应的文本字段中。但是,当我尝试更新和保存数据时,数据不会得到更新 但是,当通过单击按钮提取数据时,同样的方法也可以很好地工作 代码如下: public partial class Address : System.Web.UI.Page { string global; protected void Page_Load(object sender, EventArgs e) {

我有一个web应用程序,当页面加载时,地址详细信息从数据库中提取并显示在相应的文本字段中。但是,当我尝试更新和保存数据时,数据不会得到更新

但是,当通过单击按钮提取数据时,同样的方法也可以很好地工作

代码如下:

    public partial class Address : System.Web.UI.Page
{
    string global;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            global = Session["ID"].ToString();

            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Server = INLD50045747A\\SQLEXPRESS; Database = MyDatabase;User ID = sa; Password = Welcome1; Trusted_Connection = False;");
            //SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename=|DataDirectory|\\MyDatabase.mdf;Integrated Security=True;User Instance=True");
            con.Open();


            SqlCommand cmd = new SqlCommand("SELECT PermanentAdd,PermanentAdd2, HomePlace, HomeState, HomePin FROM EMPLOYEE_FULLADDRESS_TABLE WHERE EmployeeID = '" + global + "'", con);

            SqlDataReader x = cmd.ExecuteReader();
            while (x.Read())
            {
                TextBox1.Text = (string)x["PermanentAdd"];
                TextBox1.Enabled = false;

                TextBox5.Text = (string)x["PermanentAdd2"];
                TextBox5.Enabled = false;

                TextBox2.Text = (string)x["HomePlace"];
                TextBox2.Enabled = false;

                TextBox3.Text = (string)x["HomeState"];
                TextBox3.Enabled = false;

                State.Items.FindByText(State.SelectedItem.Text).Selected = false;
                State.Items.FindByText(TextBox3.Text).Selected = true;
                State.Enabled = false;

                TextBox4.Text = (string)x["HomePin"];
                TextBox4.Enabled = false;
            }
            x.Close();
            con.Close();
        }


    }

    protected void UpdateButton_Click(object sender, EventArgs e)
    {
        try
        {
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Server = INLD50045747A\\SQLEXPRESS; Database = MyDatabase;User ID = sa; Password = Welcome1; Trusted_Connection = False;");
            //System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename=|DataDirectory|\\MyDatabase.mdf;Integrated Security=True;User Instance=True");
            con.Open();
           // global = Session["ID"].ToString();


            //string insert = "UPDATE EMPLOYEE_FULLADDRESS_TABLE SET PermanentAdd = @PermanentAdd, PermanentAdd2 = @PermanentAdd2, HomePlace = @HomePlace, HomeState= @HomeState, HomePin= @HomePin where EmployeeID = '" + global + "'";
            SqlCommand cmd1 = new SqlCommand("UPDATE EMPLOYEE_FULLADDRESS_TABLE SET PermanentAdd = @PermanentAdd, PermanentAdd2 = @PermanentAdd2, HomePlace = @HomePlace, HomeState= @HomeState, HomePin= @HomePin where EmployeeID = '" + global + "'", con);
            cmd1.Parameters.AddWithValue("@PermanentAdd", TextBox1.Text);

            cmd1.Parameters.AddWithValue("@PermanentAdd2", TextBox5.Text);
            cmd1.Parameters.AddWithValue("@HomePlace", TextBox2.Text);
            if (State.SelectedItem.Text == "--Select--")
            {
                State.SelectedItem.Text = TextBox3.Text;
            }
            cmd1.Parameters.AddWithValue("@HomeState", State.SelectedItem.Text);
            cmd1.Parameters.AddWithValue("@HomePin", TextBox4.Text);
            cmd1.ExecuteNonQuery();
            con.Close();

            lblmsg.Text = "DATA Updated Successfully";
            lblmsg.ForeColor = System.Drawing.Color.Green;
        }
        catch (Exception exp)
        {
            lblmsg.Text = exp.Message;
            lblmsg.ForeColor = System.Drawing.Color.Red;
        }



    }


   // static int count = 0;
    protected void EditButton_Click(object sender, EventArgs e)
    {

                TextBox1.Enabled = true;

                TextBox2.Enabled = true;
                //TextBox3.Enabled = true;
                TextBox4.Enabled = true;
                TextBox5.Enabled = true;
                State.Enabled = true;

    }

请帮忙。

我想你已经注释掉了你的
全球
/
员工ID
作业了吗

//global=Session[“ID”].ToString()


您还应该将其更改为SQL中的参数。

恐怕您需要显示一些代码。没有“通用”解决方案。听起来你没有提交更新。当我在页面加载函数中不包含select命令时,更新数据库的代码工作正常。但是,一旦我在页面加载函数中包含select命令,更新按钮代码工作正常,但在数据库中看不到数据的变化。哪部分代码是必需的,请告诉我。嘿,克里斯,多谢了。这就解开了lil之谜。字符串global位于if(!IsPostBack)块内。因此,在页面加载期间,它只获取一次值。因此,在更新过程中,它只接受一个空值。问题已解决。:)