数据库更新

时间:2011-03-31 10:22:36

标签: sql-server sql-server-2008 database-update

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

但是,通过单击按钮进行数据提取时,同样可以正常工作。

这是代码:

    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;

    }

请帮忙。

1 个答案:

答案 0 :(得分:0)

我认为您已注释掉了global / employeeid作业?

// global = Session["ID"].ToString();

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

相关问题