Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL
<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
 

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 23-Sep-12 22:58pm
Edited 23-Sep-12 23:04pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
darshith at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 404
1 Nirav Prabtani 389
2 Abhinav S 210
3 PIEBALDconsult 160
4 Dave Kreskowiak 155
0 OriginalGriff 7,580
1 Sergey Alexandrovich Kryukov 6,806
2 Maciej Los 3,919
3 Peter Leow 3,693
4 CHill60 2,742


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 24 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100