Hi friends,
Pls, help me.
I have designed One textbox1.text and two listboxs lstLeft,lstright
in lstleft ,I have five values like
a
b
c
d
e
If I move the lstleft value a,b,c into lstright side. now I want to save remaining d,e one single column in database and moved lstright values a,b,c into on field in database single coloumn.
give me sample
Loading

Dan16 reputationPosted Aug 21, 2008, 11:22 AM EDT
Save the listbox items as a single string with comma-separated values. Then when you retrieve them, split the string out using the .Split() function:
private
void SaveListBoxes(){
SqlCommand myCmd = new SqlCommand("spInsertValues", connectionString);myCmd.CommandType =
CommandType.StoredProcedure;myCmd.Parameters.Add(SqlParameter(
"@paramLeft", SqlDbType.VarChar, 50)).Value = BuildStrings(lstLeft);myCmd.Parameters.Add(SqlParameter(
"@paramRight", SqlDbType.VarChar, 50)).Value = BuildStrings(lstRight);myCmd.ExecuteNonQuery();
}
private void RetrieveListBoxes(){
SqlCommand myCmd = new SqlCommand("spGetValues", connectionString);myCmd.CommandType =
CommandType.StoredProcedure; // //GET YOUR VALUES FROM THE DATABASE HERE.... // string strLeft = "d,e"; string strRight = "a,b,c"; //PopulateListBox(strLeft, lstLeft);
PopulateListBox(strRight, lstRight);
}
private void PopulateListBox(string values, ListBox lst){
string[] pieces = values.Split(','); for (int i = 0; i < pieces.Length; i++){
lst.Items.Add(pieces[i]);
}
}
private string BuildStrings(ListBox lst){
string myString = ""; foreach (ListItem li in lst.Items)myString += li.Text +
",";myString = myString.Remove(myString.Length - 1, 1);
//remove the last comma return myString;}
kavitha m11 reputationPosted Aug 22, 2008, 6:18 AM EDT
Hi Dan,
Thanks a lot. It's working fine.