Friday, September 19, 2008

Move Items from one ListBox to another ListBox in asp.net web application

Movement of items from one ListBox to another ListBox in asp.net is a typical issue sometimes programmers face. However in windows application it seems to be a common practice. It is due to the postback nature of asp.net web page. But we can do it. In my case I had to explore the situation in one of my projects where I had to convert a windows based project into web based one. Anyway I have presented below the way I use to code to move items from one asp.net ListBox control to another asp.net ListBox control.

Fig: Moving items from one asp.net ListBox control to another asp.net ListBox control

The left side ListBox in the above figure is listBoxFrom and another is listBoxTo. The top textbox is txtAddItem and top button is btnAddItem. The textbox and the button are used to add new items into the left ListBox. The remove button is used to remove the selected item from the ListBox .

Add Item in Source ListBox

public void AddItemInSourceListBox()
{
//do not let add empty into the source listbox
if (txtAddItem.Text == "")
{
return;
}

ListItem item = new ListItem();
item.Value = txtAddItem.Text;
item.Text = txtAddItem.Text;

listBoxFrom.Items.Add(item);
}

We have two buttons to move items left and right as indicated by the arrows in the button text. The left button is btnAdd and the right button is btnRemove. We should handle the button click event handler to move items from one ListBox to another.

//when btnAdd i.e. the button with double left arrow is clicked
//this moves item from source ListBox to destination ListBox
protected void btnAdd_Click(object sender, EventArgs e)
{
if (listBoxFrom.SelectedIndex == -1)
{
return;
}

ListItem item = new ListItem();
item.Value = listBoxFrom.SelectedItem.Value;
item.Text = listBoxFrom.SelectedItem.Text;

listBoxTo.Items.Add(item);
listBoxFrom.Items.Remove(item);

}

//when btnRemove i.e. the button with double right arrow is clicked
//this moves item from destination ListBox to source ListBox


protected void btnRemove_Click(object sender, EventArgs e)
{
if (listBoxTo.SelectedIndex == -1)
{
return;
}

ListItem item = new ListItem();
item.Value = listBoxTo.SelectedItem.Value;
item.Text = listBoxTo.SelectedItem.Text;

listBoxFrom.Items.Add(item);
listBoxTo.Items.Remove(item);
}

Happy Programming!

kick it on DotNetKicks.com

Thursday, September 18, 2008

Fill asp.net DropdownList and ListBox controls with DataSource programmatically

DropdownList and Listbox both are asp.net data controls. They can be directly bound to a data source. But often programmers choose to fill them dynamically using code (in C# or VB.NET !).

I am listing the simple trick to programmatically bind asp.net DropdownList and ListBox controls.

Lets say myTable is the DataTable filled from the method FillMyDataTable. Now we can
start out the journey.

public void FillDropdownList()
{
DataTable table = new DataTable();
table = FillMyDataTable(); //a method that returns required DataTable in our case
dropdownlistDDL.DataSource = table;
dropdownlistDDL.DataValueField = "myID_Column";
dropdownlistDDL.DataBind();
}


Here dropdownlistDDL is the DropdownList and myID_Column is the field to be filled in
our DropdownList.

Now we are going to bind an asp.net ListBox control to the DataTable.

public void FillListBox()
{
DataTable table = new DataTable();
table = FillMyDataTable(); //a method that returns required DataTable in our case
listboxLB.DataSource = table;
listboxLB.DataValueField = "myID_Column";
listboxLB.DataBind();
}


Here listboxLB is the ListBox and myID_Column is the field to be filled in our ListBox.

We see that both the asp.net ListBox and DropdownList controls follow the same pattern
of binding to a data source programmatically.

Happy Programming!
kick it on DotNetKicks.com

Monday, September 15, 2008

Create unique id in oracle table using trigger and sequence

In one of my recent projects, I had to insert into a table where one column had to be unique and auto generated. Further I also had to maintain the data so that it was in serial with previously inserted data.

In oracle this can be accomplished by the combined use of Trigger and Sequence. I am going to describe the technique in this post.

Sequence: A sequence can generate unique number. User can limit the minimum and maximum value and also the starting value of the generated number. In other words a sequence is like a unique number generator whose nature can be controlled by a user. One sequence can be maintained per table so that we can provide unique and serial id for each table.

Trigger: A trigger can be fired before insert, update or delete operations in any table. This can be designed to help apply business logic while using data manipulations using insert, update and delete plsql programming.

Create a sequence

CREATE SEQUENCE SEQ_EMPLOYEE INCREMENT BY 1 START WITH 10000 MINVALUE 10000 MAXVALUE 99999 NOCYCLE NOCACHE NOORDER;

Create a Trigger on Employee Table

CREATE OR REPLACE TRIGGER TRIGGER_EMPLOYEE
BEFORE INSERT ON EMPLOYEE
FOR EACH ROW
begin
select SEQ_EMPLOYEE.nextval into :new.EMPLOYEE_ID from dual;
end;

Insert into Employee Table

INSERT INTO Employee (Name, Address, Post)
VALUES ('sangam','New Road-10', 'Software Developer')

Note: Since the trigger fetches unique number from sequence and inserts into Employee_id field, we need not to put this column in Insert statement as above.

Happy Oracling!!

Popular Posts

Recent Articles