Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ListBox ID="LstLeft"     runat="server" SelectionMode="multiple" Width="200px" Rows="5"></asp:ListBox>
            <asp:Button  ID="BtnRight"    runat="server" Text=">"  onclick="BtnRight_Click"/>
            <asp:Button  ID="BtnRightAll" runat="server" Text=">>" onclick="BtnRightAll_Click"/>
            <asp:Button  ID="BtnLeftAll"  runat="server" Text="<<" onclick="BtnLeftAll_Click"/>
            <asp:Button  ID="BtnLeft"     runat="server" Text="<"  onclick="BtnLeft_Click"/>
            <asp:ListBox ID="LstRight"    runat="server" SelectionMode="multiple" Width="200px" Rows="5"></asp:ListBox>

        </div>
    </form>
</body>
</html>






code in cs_page


C#
using Microsoft.VisualBasic;

using System;

using System.Collections;

using System.Collections.Generic;

using System.Data;

using System.Diagnostics;

using System.Data.SqlClient;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class rough : System.Web.UI.Page
{

    public static ArrayList Files = new ArrayList();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Left_List();
            Right_List();
        }
    }
    protected void Left_List()
    {
        try
        {
            string str = (@"Data Source=.;Initial Catalog=new;Integrated Security=True");

            SqlConnection con = new SqlConnection(str);

            string com = "Select username from Ttable";

            SqlDataAdapter adpt = new SqlDataAdapter(com, con);

            DataSet myDataSet = new DataSet();

            adpt.Fill(myDataSet, "Ttable");

            DataTable myDataTable = myDataSet.Tables[0];

            DataRow tempRow = null;



            foreach (DataRow tempRow_Variable in myDataTable.Rows)
            {

                tempRow = tempRow_Variable;

                LstLeft.Items.Add((tempRow["username"] + " "));

            }
        }
        catch
        {
            throw;
        }
    }
    protected void Right_List()
    {
        try
        {
            string str = (@"Data Source=.;Initial Catalog=new;Integrated Security=True");

            SqlConnection con = new SqlConnection(str);

            string com = "Select city from dTable";

            SqlDataAdapter adpt = new SqlDataAdapter(com, con);

            DataSet myDataSet = new DataSet();

            adpt.Fill(myDataSet, "dTable");

            DataTable myDataTable = myDataSet.Tables[0];

            DataRow tempRow = null;



            foreach (DataRow tempRow_Variable in myDataTable.Rows)
            {

                tempRow = tempRow_Variable;

                LstRight.Items.Add((tempRow["city"] + " "));

            }
        }
        catch
        {
            throw;
        }
    }
    protected void BtnRight_Click(object sender, EventArgs e)
    {
          for (int i = LstLeft.Items.Count - 1; i >= 0; i--)
            {

                if (LstLeft.Items[i].Selected)
                {

                    LstRight.Items.Add(LstLeft.Items[i]);

                    LstRight.ClearSelection();

                    LstLeft.Items.Remove(LstLeft.Items[i]);

                }

            }
        string str = (@"Data Source=.;Initial Catalog=new;Integrated Security=True");
        SqlConnection con = new SqlConnection(str);
        SqlCommand cmd_check = con.CreateCommand();
        cmd_check.CommandType = CommandType.Text;
        SqlCommand cmd_insert = con.CreateCommand();
        cmd_insert.CommandType = CommandType.Text;
        try
        {
            con.Open();
        }
        catch (SqlException ex)
        {

        }

        //Inserts into dTable and deltes from TTable according to the final RIGHT list

        for (int i = 0; i < LstRight.Items.Count; i++)
        {
            cmd_check.CommandText = "select count(*) from dTable where city='" + LstRight.Items[i] + "'";
            if ((int)cmd_check.ExecuteScalar() == 0)
            {
                cmd_insert.CommandText = "insert into dTable values('" + LstRight.Items[i] + "')";
                cmd_insert.ExecuteNonQuery();
            }
        }
        for (int i = 0; i < LstRight.Items.Count; i++)
        {
            cmd_check.CommandText = "select * from Ttable where username='" + LstRight.Items[i] + "'";
            if (cmd_check.ExecuteNonQuery() != 0)
            {
                
            }
        }

      
    }

        
    
    protected void BtnRightAll_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < LstLeft.Items.Count; i++)
        {
            LstRight.Items.Add(LstLeft.Items[i]);
        }
        LstLeft.Items.Clear();
        string str = (@"Data Source=.;Initial Catalog=new;Integrated Security=True");
        SqlConnection con = new SqlConnection(str);
        SqlCommand cmd_check = con.CreateCommand();
        cmd_check.CommandType = CommandType.Text;
        SqlCommand cmd_insert = con.CreateCommand();
        cmd_insert.CommandType = CommandType.Text;
        try
        {
            con.Open();
        }
        catch (SqlException ex)
        {

        }

        //Inserts into dTable and deltes from TTable according to the final RIGHT list

        for (int i = 0; i < LstRight.Items.Count; i++)
        {
            cmd_check.CommandText = "select count(*) from dTable where city='" + LstRight.Items[i] + "'";
            if ((int)cmd_check.ExecuteScalar() == 0)
            {
                cmd_insert.CommandText = "insert into dTable values('" + LstRight.Items[i] + "')";
                cmd_insert.ExecuteNonQuery();
            }
        }
        for (int i = 0; i < LstRight.Items.Count; i++)
        {
            cmd_check.CommandText = "select * from Ttable where username='" + LstRight.Items[i] + "'";
            if (cmd_check.ExecuteNonQuery() != 0)
            {
                
            }
        }
    }
    protected void BtnLeft_Click(object sender, EventArgs e)
    {
        for (int i = LstRight.Items.Count - 1; i >= 0; i--)
        {

            if (LstRight.Items[i].Selected)
            {

                LstLeft.Items.Add(LstRight.Items[i]);

                LstLeft.ClearSelection();

                LstRight.Items.Remove(LstRight.Items[i]);

            }

        }
        string str = (@"Data Source=.;Initial Catalog=new;Integrated Security=True");
        SqlConnection con = new SqlConnection(str);
        SqlCommand cmd_check = con.CreateCommand();
        cmd_check.CommandType = CommandType.Text;
        SqlCommand cmd_insert = con.CreateCommand();
        cmd_insert.CommandType = CommandType.Text;
        try
        {
            con.Open();
        }
        catch (SqlException ex)
        {

        }

        for (int i = 0; i < LstLeft.Items.Count; i++)
        {
            cmd_check.CommandText = "select count(*) from TTable where username='" + LstLeft.Items[i] + "'";
            if ((int)cmd_check.ExecuteScalar() == 0)
            {
                cmd_insert.CommandText = "insert into TTable values('" + LstLeft.Items[i] + "')";
                cmd_insert.ExecuteNonQuery();
            }
        }
        for (int i = 0; i < LstLeft.Items.Count; i++)
        {
            cmd_check.CommandText = "select * from dTable where city='" + LstLeft.Items[i] + "'";
            if (cmd_check.ExecuteNonQuery() != 0)
            {
                cmd_insert.CommandText = "delete from dTable where city='" + LstLeft.Items[i] + "'";
                cmd_insert.ExecuteNonQuery();
            }
        }
    }

  
    protected void BtnLeftAll_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < LstRight.Items.Count; i++)
        {
            LstLeft.Items.Add(LstRight.Items[i]);
        }
        LstRight.Items.Clear();
        string str = (@"Data Source=.;Initial Catalog=new;Integrated Security=True");
        SqlConnection con = new SqlConnection(str);
        SqlCommand cmd_check = con.CreateCommand();
        cmd_check.CommandType = CommandType.Text;
        SqlCommand cmd_insert = con.CreateCommand();
        cmd_insert.CommandType = CommandType.Text;
        try
        {
            con.Open();
        }
        catch (SqlException ex)
        {

        }

        for (int i = 0; i < LstLeft.Items.Count; i++)
        {
            cmd_check.CommandText = "select count(*) from TTable where username='" + LstLeft.Items[i] + "'";
            if ((int)cmd_check.ExecuteScalar() == 0)
            {
                cmd_insert.CommandText = "insert into TTable values('" + LstLeft.Items[i] + "')";
                cmd_insert.ExecuteNonQuery();
            }
        }
        for (int i = 0; i < LstLeft.Items.Count; i++)
        {
            cmd_check.CommandText = "select * from dTable where city='" + LstLeft.Items[i] + "'";
            if (cmd_check.ExecuteNonQuery() != 0)
            {
                cmd_insert.CommandText = "delete from dTable where city='" + LstLeft.Items[i] + "'";
                cmd_insert.ExecuteNonQuery();
            }
        }
    }
    
}
Posted
Updated 23-Sep-12 23:04pm
v3

1 solution

While I didn't see your question directly I hope you clarify it and ask it, other than that you are doing everything here on the server side and you are submitting your changes to the database directly! not sure about your business needs but this really looks bad, why not to have it all done using javascript and when the user is done you submit changes to the database, I used to have a similar component (Custom Control) that I have created to achieve this.
 
Share this answer
 
Comments
darshith 24-Sep-12 5:24am    
ya i need it in client side........

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900