执行插入操作后,Asp.net Listview不会绑定

时间:2017-06-19 03:44:55

标签: c# asp.net listview

我的页面中有listview控件。我在onPage加载以及在表中输入新记录时填充。问题是在执行InsertOperation后我再次调用它的绑定方法但它没有刷新其数据。因此我调试了它&在SQL&中执行实际查询查询返回添加的行。

protected void insertBulkPricing()
{
    try {
        MySqlConnection con = new MySqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings("conio2").ConnectionString();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "INSERT INTO bulk_pricing (productID, rangeFrom, rangeTo, price, time, discount, status) VALUES(@productID, @rangeFrom, @rangeTo, @price, @time, @discount, @status)";
        cmd.Parameters.AddWithValue("@productID", productID.Text);
        cmd.Parameters.AddWithValue("@rangeFrom", bulkRangeFrom.Text);
        cmd.Parameters.AddWithValue("@rangeTo", bulkRangeTo.Text);
        cmd.Parameters.AddWithValue("@price", bulkPrice.Text);
        cmd.Parameters.AddWithValue("@time", bulkTime.Text);
        cmd.Parameters.AddWithValue("@discount", bulkDiscount);
        cmd.Parameters.AddWithValue("@status", "active");
        cmd.Connection = con;
        con.Open();
        this.bindBulkPricing();
        cmd.ExecuteNonQuery();
        con.Close();
    } catch (Exception ex) {
        Response.Write(ex);
    }
}

private void bindBulkPricing()
{
    string action = Request.QueryString("action").ToString;
    if (action == "new") {
        productID.Text = rowNumber;
    }

    try {
        string str = "select * from bulk_pricing where productID = @productID";
        string conString = ConfigurationManager.ConnectionStrings("conio2").ConnectionString;
        MySqlConnection con = new MySqlConnection(conString);
        MySqlCommand cmd = new MySqlCommand(str);
        cmd.Parameters.AddWithValue("@productID", productID.Text);
        con.Open();
        MySqlDataAdapter da = new MySqlDataAdapter();
        cmd.Connection = con;
        da.SelectCommand = cmd;
        DataTable dt = new DataTable();
        da.Fill(dt);
        bulkPriceList.DataSource = dt;
        bulkPriceList.DataBind();
        con.Close();
    } catch (Exception ex) {
        Response.Write(ex);
    }
}

1 个答案:

答案 0 :(得分:0)

protected void insertBulkPricing()
{
    try {
        MySqlConnection con = new MySqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings("conio2").ConnectionString();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "INSERT INTO bulk_pricing (productID, rangeFrom, rangeTo, price, time, discount, status) VALUES(@productID, @rangeFrom, @rangeTo, @price, @time, @discount, @status)";
        cmd.Parameters.AddWithValue("@productID", productID.Text);
        cmd.Parameters.AddWithValue("@rangeFrom", bulkRangeFrom.Text);
        cmd.Parameters.AddWithValue("@rangeTo", bulkRangeTo.Text);
        cmd.Parameters.AddWithValue("@price", bulkPrice.Text);
        cmd.Parameters.AddWithValue("@time", bulkTime.Text);
        cmd.Parameters.AddWithValue("@discount", bulkDiscount);
        cmd.Parameters.AddWithValue("@status", "active");
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        this.bindBulkPricing();
    } catch (Exception ex) {
        Response.Write(ex);
    }
}

您只能在运行cmd.ExecuteNonQuery()后运行this.bindBulkPricing()。我希望它对你有用。