Click here to Skip to main content
15,903,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Please can anyone help me in doing this

There is 1 dropdownlist and 1 listbox and 1 insert button.

dropdownlist will contain all the choices and if user choose one and hit the insert button it will be inserted in the listbox, and if user would like to insert another item it will be inserted again in the listbox.

and then lets say that the listbox now contain all of the items chosen by the user.

if save button was clicked it will all be saved into the database with the column name "Inserted".


I managed to finish the first part which is selecting items from dropdownlist into the list box.
But, I am having trouble in saving the data from list box(multiple data) to the database.


Please can someone help me....


C#
protected void usersavebtn_Click(object sender, EventArgs e)
        {

            string connectionString = ConfigurationManager.AppSettings["ApplicationServices"];
            SqlConnection myconnection = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("sp_InsertAdmin_User", myconnection);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@Userid", Convert.ToInt32(usersddl.SelectedValue));
            cmd.Parameters.AddWithValue("@Tier", userbranchgrplistbox.SelectedValue);
            cmd.Parameters.AddWithValue("@Branch", userbranchlistbox.SelectedValue);
            cmd.CommandType = CommandType.StoredProcedure;
            myconnection.Open();
            cmd.Connection = myconnection;
            cmd.ExecuteNonQuery();
            myconnection.Close();

        }     


above is the code i have tried...

Tier dropdown is text but i have to store its id in the database..
Posted

Consider having your data in (an independent) structured data array, one field of the structure being a 'selected' field. Now, as items are selected (or de-selected), you need only change the flag on your array and handle populating your droplist and listbox. The list box and drop-list only need contain the visible portion and a reference.

I've used this successfully even for stateless data, such as an web page (javaScript handles the visibility, php handles the finale to the database).

At this point, you can enter you data into the database via a loop on your data array. Your data array can contain any amount and diversity of data in the structure. You can simply loop through your array and operate on the selected vs. non-selected elements as you like.

Much of this could be done via storage in the control structure, instead, but accessing data in a normal array (or ArrayList if you prefer) is much easier.

Your storage array structurce can look like {userid, tier, branch, isSelected}, and let me reemphasize that there is only a single array containing all data you wish to use in your data transfer.
 
Share this answer
 
v2
Comments
babli3 16-Apr-13 3:48am    
hi can you please help me with an example...
This is generically .NET -> I prefer C++, you seem to be using C#. The following is somewhere in between. Principle is the same

public struct youList {
int userid;
int tier;
int branch
bool isSelected;
};

ArrayList dataSet = new ArrayList();

// Load your array list:
foreach (yourData as d) {
struct youList = new y; // new object with you data
y.userid = d.userid; // fill w/your data
... etc ...
y.isSelected = false; // this may be d.isSelected if you load from existing dbase data

dataSet.Add(y)
}

// From this point, you iterate through the list and
// Add data to your drop list OR listBox based upon
// whether isSelected is true or false; Useful for
// loading a previous session.

/* Now, when an item is selected from the drop list you
1 - get it's unique identifier (userid?)
2 - set isSelected to 'true' in dataset.
3 - remove from droplist
4 - add to listBox
Optionally, you could clear both controls and
repopulate them based upon the isSelected flag
instead of 3, 4 above

When transferring to database, simply loop the array
and use appropriate SQL to insert/update/delete items
based upon userID and isSelected.

There are many variations on this to improve efficiency
for specific situations. The ArrayList is a handy way to
handle data in a consistent manner and allow you to create
generic methods to handle this for any select routine in
any type of control scenario.
*/
 
Share this answer
 
Comments
babli3 16-Apr-13 10:00am    
Thanks
C#
string selectedIDs = string.Empty;
if (lstBox.Items != null && lstBox.Items.Count > 0)
{
   foreach (ListItem item in lstBox.Items)
   {
       selectedIDs += item.Value + ",";
   }
   selectedIDs = selectedIDs.Trim();
}



here you will get data like 1,2,3,4.

So in your stored procedure you can split comma separated values and insert one by one in stored procedure.
 
Share this answer
 
v2
Comments
babli3 16-Apr-13 10:00am    
Thanks

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