Inserting Comma Seperated Values in Sql Server 2005

  • I'm Using Asp.Net with C#..

    i'm having One ListBox control in which I'm getting Data Dynamically.. I want to Insert the Selected data (or ALL DATA) from ListBox Control into My Table(Get_Data) in Sql Server Database.. Here I'm displaying NAMES(EMP_Names) in LISTBOX Control.. I want to Insert there ID(EMP_ID) values instead of NAMES..

    EVEN some times MULTIPLE SELECTIONS will also been done in LISTBOX Control.. I want to INSERT those MULTIPLE values in ONE COLUMN as COMMA SEPERATED VALUES (ex:1,2,3,4,5,6,7----)..

    How to do this..

    Please give me the code..

    Thank You..

  • Aswanth (5/29/2008)


    I'm Using Asp.Net with C#..

    i'm having One ListBox control in which I'm getting Data Dynamically.. I want to Insert the Selected data (or ALL DATA) from ListBox Control into My Table(Get_Data) in Sql Server Database.. Here I'm displaying NAMES(EMP_Names) in LISTBOX Control.. I want to Insert there ID(EMP_ID) values instead of NAMES..

    EVEN some times MULTIPLE SELECTIONS will also been done in LISTBOX Control.. I want to INSERT those MULTIPLE values in ONE COLUMN as COMMA SEPERATED VALUES (ex:1,2,3,4,5,6,7----)..

    How to do this..

    Please give me the code..

    Thank You..

    I think this article may help you out.

    http://www.sqlservercentral.com/articles/TSQL/62867/

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • I'm not sure if I'm understanding you problem correctly, but just iterate throught the items in the ListBox, building a string as you go and then pass that to a stored procedure that accepts that string as a variable and inserts it into the table. If you must store the string as a comma separated value, then I would do this in your .NET application:

    protected void btnSaveSelection_Click(object sender, EventArgs e)

    {

    // Get the corresponding ids for the selected names

    string ids = string.Empty;

    for (int i = 0; i < lstNames.Items.Count; i++)

    {

    // If the name was selected, store in variable

    if (lstNames.Items.Selected)

    {

    ids += (ids == string.Empty) ? lstNames.Items.Value : "," + lstNames.Items.Value;

    }

    }

    // Call stored procedure to insert the concatenated string

    clsDB db = new clsDB();

    db.SaveSelectedNames(ids);

    }

    clsDB is your database class that contains a call to the stored procedure, passing the string of ids. This is just the 10,000 foot perspective. Obviously, you would include some error handling, but you get the idea. Not sure if this is what you're looking for??

  • Ho to Fetch the same values from the DB please provide code ....

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply