在列表框中提交产品时出现问题 [英] problem in submitting the products in listbox

查看:51
本文介绍了在列表框中提交产品时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表框.一个列表框显示所有产品,另一个列表框用于将列表框1中的产品交换给管理员想要为其分配在购物车中显示的权限的列表框1.但是管理员有权添加仅5个产品.但是,当我添加2种产品然后按提交时,就会出现问题,但是当我下次再添加3种产品时,则我之前提交的2种产品也添加到了列表中.

这是我的代码

I have two list boxes . One list box showing all products and other list box is used to swap the products from listbox1 to whom admin wants to assign the permission to show in cart . But admin have right to add the only 5 product . But the problem occurs when i add the 2 products and then press submit they will be submitted but when i go next time and add 3 more produtcts then the 2 products which i submitted previously also added into the list .

here is my code

public partial class admin_UserPermission : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {   
        if (!IsPostBack)
        {
            DataManager myData = new DataManager(DbFunctions.GetConnectionString());
            MySqlConnection myPConn = myData.GetPooledConnection(new MySqlConnection());
            MySqlCommand cmd = new MySqlCommand("SELECT adminuserid,adminusername FROM adminusermast ", myPConn);
            DataTable dt = new DataTable();
            myPConn.Open();
            MySqlDataAdapter adptr = new MySqlDataAdapter(cmd);
            adptr.Fill(dt);
            myPConn.Close();
            if (dt.Rows.Count > 0)
            {
                ddlAdmin.Items.Clear();
                ddlAdmin.Items.Add("SELECT");
                foreach (DataRow drow in dt.Rows)
                {
                    ListItem lst = new ListItem();
                    lst.Value = drow[0].ToString();
                    lst.Text = drow[1].ToString();
                    ddlAdmin.Items.Add(lst);
                }
            }
        }
    }

    protected void FillProductList()
    {
           
        DataManager myData = new DataManager(DbFunctions.GetConnectionString());
        MySqlConnection myPConn = myData.GetPooledConnection(new MySqlConnection());
        MySqlCommand com = new MySqlCommand("select * from productmast where productid NOT IN (select productid  from userpermission where adminuserid =" + ddlAdmin.SelectedIndex.ToString() + ")", myPConn);
        MySqlDataReader Reader = null;
        myPConn.Open();
        Reader = com.ExecuteReader();
        while (Reader.Read())
        {
            ListItem newItem = new ListItem();
            newItem.Text = Reader["productname"].ToString();
            newItem.Value = Reader["productid"].ToString();
            ProductListBox.Items.Add(newItem);
        }
        Reader.Close();
        myPConn.Close();  
    }

    protected void FillProuductPermissionList()
    {
        DataManager myData = new DataManager(DbFunctions.GetConnectionString());
        MySqlConnection myPConn = myData.GetPooledConnection(new MySqlConnection());
        MySqlCommand com = new MySqlCommand("select * from userpermission u inner join productmast p on u.productid = p.productid where adminuserid =" + ddlAdmin.SelectedIndex.ToString() + "", myPConn);
        MySqlDataReader Reader = null;
        myPConn.Open();
        Reader = com.ExecuteReader();
        while (Reader.Read())
        {

            ListItem newItem = new ListItem();

            newItem.Text = Reader["productname"].ToString();
            newItem.Value = Reader["productid"].ToString();
            PermissionListBox.Items.Add(newItem);
        }

        Reader.Close();
        myPConn.Close();

    }
    protected void AdminDropDown_Selected(object sender, EventArgs e)
    {
        FillProductList();
        ProductListBox.Visible = true;
        BtnMoveLeft.Visible = true;
        ButtonMoveRight.Visible = true;
        PermissionListBox.Visible = true;
        FillProuductPermissionList();
        BtnPermission.Visible = true;
    }
    protected void BtnMoveleft_Click(object sender, EventArgs e)
    {
        if (ProductListBox.SelectedIndex == -1)
        {
            LabelError.InnerHtml = "No Data Selected";
        }
        for (int i = ProductListBox.Items.Count - 1; i >= 0; i--)
        {
            if (ProductListBox.Items[i].Selected == true && Convert.ToInt32(PermissionListBox.Items.Count) < 5)
            {
                PermissionListBox.Items.Add(ProductListBox.Items[i]);
                ListItem li = ProductListBox.Items[i];
                ProductListBox.Items.Remove(li);
            }
            else if (Convert.ToInt32(PermissionListBox.Items.Count) >= 5)
            {
                LabelError.InnerText = " You have right to add only 5 products";
            }
            
        }
        
        BtnPermission.Enabled = true;

    }
    protected void Permission_Accepted(object sender, EventArgs e)
    {
        DataManager myData = new DataManager(DbFunctions.GetConnectionString());
        MySqlConnection myPConn = myData.GetPooledConnection(new MySqlConnection());
        for(int i=PermissionListBox.Items.Count-1;i>=0;i--)
        {
            myData.InsertTable("userpermission", "productid,adminuserid", TextUtilities.ToSql(PermissionListBox.Items[i].Value.ToString() , "S") + "," + TextUtilities.ToSql(ddlAdmin.SelectedIndex.ToString(),"S"), myPConn);
        }

        Response.Redirect("MyAccount.aspx");
    } 
    protected void BtnMoveRight_Click(object sender, EventArgs e)
    {
        for (int i = PermissionListBox.Items.Count - 1; i >= 0; i--)
        {
            if (PermissionListBox.Items[i].Selected == true)
            {
                ProductListBox.Items.Add(PermissionListBox.Items[i]);
                ListItem li = PermissionListBox.Items[i];
                PermissionListBox.Items.Remove(li);
            }
        }            
    }
}

推荐答案

导致该错误的根本原因是未清除旧数据. Itz通过在FillProductList方法的while循环之前添加Clear方法来修复.

The root cause for the error is due to the non clearance of old data. Itz fixed by adding Clear method just before while loop in FillProductList method.

ProductListBox.Items.Clear();
while (Reader.Read())
{
    ListItem newItem = new ListItem();
    newItem.Text = Reader["productname"].ToString();
    newItem.Value = Reader["productid"].ToString();
    ProductListBox.Items.Add(newItem);

}


这篇关于在列表框中提交产品时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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