数据不写出来数据库 [英] Data not writing out to Database

查看:100
本文介绍了数据不写出来数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个位值在CheckboxProcess_CheckedChanged事件的数据库。但是,没有实际写入的。我曾在那里填写适配器一些code,但我把它。任何人都可以看到这是怎么回事了?

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        如果(!的IsPostBack)
        {
            使用(SqlConnection的连接=新的SqlConnection(connectionString.ToString()))
            {
                connection.Open();
                DataAdapter的=新的SqlDataAdapter(SELECT * FROM SecureOrders连接);
                数据=新的DataSet();
                DataAdapter.Fill方法(数据集SecureOrders);
                数据视图源=新的数据视图(dataSet.Tables [0]);
                DefaultGrid.DataSource =来源;
                DefaultGrid.DataBind();
                connection.close()时;
            }
        }
    }    保护无效CheckBoxProcess_CheckedChanged(对象发件人,EventArgs的发送)
    {
        布尔更新;
        字符串checkedString =UPDATE SecureOrders SET处理= 1,其中FNAME LIKE'%+ DefaultGrid.SelectedRow.Cells [2]。文本+%'AND LNAME LIKE'%+ DefaultGrid.SelectedRow.Cells [3]。文本+ %';
        字符串uncheckedString =UPDATE SecureOrders SET处理= 0 WHERE FNAME LIKE'%+ DefaultGrid.SelectedRow.Cells [2]。文本+%'AND LNAME LIKE'%+ DefaultGrid.SelectedRow.Cells [3]。文本+ %';
        复选框CB =(复选框)发送;
        GridViewRow GVR =(GridViewRow)cb.Parent.Parent;
        DefaultGrid.SelectedIndex = gvr.RowIndex;
        更新= Convert.ToBoolean(DefaultGrid.SelectedValue);        orderByString = orderByList.SelectedItem.Value;
        fieldString = searchTextBox.Text;        的connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings [secureodb];        使用(SqlConnection的连接=新的SqlConnection(connectionString.ToString()))
        {
            connection.Open();
            的SqlCommand checkedCmd =新的SqlCommand(checkedString,连接);
            的SqlCommand uncheckedCmd =新的SqlCommand(uncheckedString,连接);            如果(cb.Checked ==真)
            {
                checkedCmd.ExecuteNonQuery();            }
            其他
            {
                uncheckedCmd.ExecuteNonQuery();
            }            connection.close()时;
        }


解决方案

有关您的更新语句有:<​​/ P>

 字符串checkedString =UPDATE SecureOrders SET处理= 1,其中FNAME LIKE'%+ DefaultGrid.SelectedRow.Cells [2]。文本+%'AND LNAME LIKE'%+ DefaultGrid.SelectedRow.Cells [3]。文本+%';

我想知道,如果你需要的最后%大关后删除空间:

 字符串checkedString =UPDATE SecureOrders SET处理= 1,其中FNAME LIKE'%+ DefaultGrid.SelectedRow.Cells [2]。文本+%'AND LNAME LIKE'%+ DefaultGrid.SelectedRow.Cells [3]。文本+%';

I'm writing a bit value to a database in the CheckboxProcess_CheckedChanged event. However, nothing is actually being written out. I had some code in there filling an adapter, but I took it out. Can anybody see where this is going wrong?

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
            {
                connection.Open();
                dataAdapter = new SqlDataAdapter("SELECT * FROM SecureOrders", connection);
                dataSet = new DataSet();             
                dataAdapter.Fill(dataSet, "SecureOrders");
                DataView source = new DataView(dataSet.Tables[0]);
                DefaultGrid.DataSource = source;
                DefaultGrid.DataBind();
                connection.Close();
            }
        }
    }

    protected void CheckBoxProcess_CheckedChanged(object sender, EventArgs e)
    {
        bool update;
        string checkedString = "UPDATE SecureOrders SET processed = 1 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '% " + DefaultGrid.SelectedRow.Cells[3].Text + "%'";
        string uncheckedString = "UPDATE SecureOrders SET processed = 0 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '% " + DefaultGrid.SelectedRow.Cells[3].Text + "%'";
        CheckBox cb = (CheckBox)sender;
        GridViewRow gvr = (GridViewRow)cb.Parent.Parent;
        DefaultGrid.SelectedIndex = gvr.RowIndex;
        update = Convert.ToBoolean(DefaultGrid.SelectedValue);

        orderByString = orderByList.SelectedItem.Value;
        fieldString = searchTextBox.Text;



        connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"];

        using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
        {
            connection.Open();
            SqlCommand checkedCmd = new SqlCommand(checkedString, connection);
            SqlCommand uncheckedCmd = new SqlCommand(uncheckedString, connection);

            if (cb.Checked == true)
            {
                checkedCmd.ExecuteNonQuery();

            }
            else
            {
                uncheckedCmd.ExecuteNonQuery();
            }

            connection.Close();
        }

解决方案

For your update statements you have:

string checkedString = "UPDATE SecureOrders SET processed = 1 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '% " + DefaultGrid.SelectedRow.Cells[3].Text + "%'";

I'm wondering if you need to remove the space after the last % mark:

string checkedString = "UPDATE SecureOrders SET processed = 1 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '%" + DefaultGrid.SelectedRow.Cells[3].Text + "%'";

这篇关于数据不写出来数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆