Hi Friends.
I have an project with vs2005 and sqlserver2005.There is a page contain a combobox , a text box and two button.The combox items are my cost list that maybe thems count increase by user.how can I manage it?

more about it:

if person clicks the add button we have to add textboxe's text as an item for combobox.
I did it like below:

cmbList.Items.Add(txtName.Text);

problem is that ofter programs running the added items have to remains for next play.How Can i save my new list for next plays?
And I have to add this item as a database's culomn which I did so:

sql = "ALTER TABLE tblCost cost"; 
sql += cmbList.Items.count.Tostring() + " float";

this sql command will plays.here is no problem.
so my tables culomns are named as items index's in my combobox and my tables culomns named as cost0, cost1, ... and new coulumns name is "cost"+(new indexe's number) _The items that added currently_.
the problem starts if the user delete one Item.so my combobox item's counts will increase and it makes error in altering the table.
so how can i improve it?

follow the steps

1. make different table for your dropdownlist.
2. write the insert query in the button click event
insert into tablename values ('+textbox1.text+"')
3. now give the datasource of the new dropdownlist table to the dropdownlist
write the following code in page_load event
sqldataAdapter ad=ne ("select * from tablename",connectionstring);
dataset ds=new dataset();
dropdownlist1.datasource=ds;
dropdownlist1.databind();

I have no problem in here.at first my change have to stores.for example the user wants insert a new cost to list.it will be add to the list compile time but at the next program playing my list is free.how i save it for my next plays.

other thing was my tables coulomns name.them's name is rely to the index of comboboxe's new item.
for example it has 3 cost in its list.
cost1
cost2
cost3
we removed the cost 2.
tables coulomn's name are
cost1
cost3
if we write the below code sqlserver will returns an error.and it is true.

sql = "ALTER TABLE tblCost cost";
      sql += cmbList.Items.count.Tostring() + " float";

because it want to add a new coulomn to tbl by name cost2.
while this name is exist.
I have to see at last item of combobox.here is cost3.
my new table's name have to be cost4.how can i earn it's name?

why do you need names for your columns? why not just place the value in the table and have an auto increment id?

I think what MARKAND911 and Diamonddrake are trying to suggest is that instead of adding new columns to your table you should have a seperate table in your database to store your costs.
It is a very poor design to add columns the way you are. You shouldnt be adding and removing columns this way.
Consider this;
-You add 2 rows to your table. Then you add 2 costs for the first row. What values will be in Cost1 and Cost2 for the second row?
-You add 2 rows to your table. You add 3 costs to each row. Then you delete one of the costs from row 1. Your code will delete the column. What happens to the value in that column in row 2??

If you are storing more than one cost per item then you need to model your tables to reflect the many-to-one relationship.
Without knowing the other columns you have this is jsut a rough guide:

Current Design:
Item Table
ItemID int pKey,
ItemName string,
Cost1 float,
Cost2 float
....
....

New Table Design:

Item Table
ItemID int pKey,
ItemName string

Cost Table
CostID int pKey,
ItemID int fKey,
Cost float


Now you can retrieve all costs for an item using sql query: "SELECT * FROM Costs WHERE ItemID = @ItemID".

Edited 6 Years Ago by Geekitygeek: n/a

lets Know more about my project.a corporation has a List of payments.
for example upkeep , digging and... .in each of them there are thousands factors and cheques witch have to add.some times they wants to add a new payment parameter into theme's list.I have no problem with theme's payments ID or adding them into my tables.
I have problem adding or deleting a new payment subject.
every of Costs Items are consider of many records.
I cannot read and write English good.It may be I say my mean wrong or find out your means wrong.Sorry If it is.

In the design view right click the combo box and select edit items and write the items you want in drop down menu

This article has been dead for over six months. Start a new discussion instead.