Link to home
Start Free TrialLog in
Avatar of kimmie8000
kimmie8000Flag for United States of America

asked on

How do you append data to a field (SQL) using LINQ

I have an array that could go up to 308 values.

In the SQL table where this data is stored is a multi-line text box.  So, the user in the web application can store any text.  They useable stored the data like this.

Apple
Orange
Carrot

Here is the problem.  I have been given a spreadsheet to update this particular <table> field with up to 308 values.  I know that I can just move in the value to the variable and do submitchanges(); and that will work.  However, I don't know how to do this with overwriting every value except the last in the field 308?

Thank you in advance.  
foreach (Test d in match)
                             {
                                 string[] StringArray = new string[5];
                                 StringArray[0] = b.EpicInbasket1;
                                 StringArray[1] = b.EpicInbasket2;
                                 StringArray[2] = b.EpicInbasket3;
                                 StringArray[3] = b.EpicInbasket4;
                                 StringArray[4] = b.EpicInbasket5;

                                 IEnumerable<string> result2 = from str in StringArray where str[0] != null select str;

                                 foreach (string str in result2)
                                   {
                                     // HOW DO I APPEND THIS AND NOT OVERWRITE??
                                     d.Message = (str);
                                     
                                     // submit changes to the database.
                                     tm.SubmitChanges();

                                     
                                    }

                                }

Open in new window

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi kimmie8000;

Try it like this.

Fernando

foreach (string str in result2)
{
    // HOW DO I APPEND THIS AND NOT OVERWRITE??
    // US THE += OPERATOR
    d.Message += str;
     
    // submit changes to the database.
    tm.SubmitChanges();    
}

Open in new window

Avatar of dd12
dd12

Hi ,

I guess this is what you are after :
 private void btnStringBuilderTest_Click(object sender, RoutedEventArgs e)
        {
            string[] StringArray = new string[5];
            StringArray[0] = "Apple";
            StringArray[1] = "Ball";
            StringArray[2] = "Cat";
            StringArray[3] = "Dog";
            StringArray[4] = "Elephant";

            IEnumerable<string> result2 = from str in StringArray select str;
            System.Text.StringBuilder sb = new StringBuilder();

            foreach (string str in result2)
            {            
                sb.Append(str + "\n");
               
            }
           // MessageBox.Show(sb.ToString());
          tm.SubmitChanges();

        }

If you don't want them on new line remove ' + "\n" '.

Byt, "from str in StringArray where  select str" is more appropriate since your "where" clause always returns true.
ASKER CERTIFIED SOLUTION
Avatar of dd12
dd12

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kimmie8000

ASKER

Both of you provided the answer, but dd12 you provided the solution to my problem.  I am new to LINQ, and I seriously did not want to write a stored procedure.  I am going to accept dd12 answer because he actually went all the way and provided the solution.  
I don't know if this will need to go to the web master.  However, dd12 went the extra step and finiished my logic.  For that, thank you very much!!  

I am learning LINQ and this one escaped me.  I know how to do it in SQL, but I did not want to call on a stored procedure, when we have linq for C#.  

Thanks again!!