3

Right now I have a Select Tag retrieving all the Title of the documents from the Database:

string QueryString = "SELECT TITLE FROM DOC";
SqlConnection myconnection = new SqlConnection(ConnectString);
SqlDataAdapter mycommand = new SqlDataAdapter(QueryString,myconnection);
DataSet ds = new DataSet();
mycommand.Fill(ds, "DOC");

test.DataSource = ds;
test.DataTextField = "TITLE";
test.DataValueField = "TITLE";
test.DataBind();

The problem is that I need to store all this data in text so that I can use the MailMessage Class and send it via Email.

Any Thoughts?

Pvxtotal
  • 79
  • 7
  • can you describe more? what did you mean by all data? in your example, you just use the title and bind it to a dropdown, you need more information? or you need some structural data such as list? your end user has to choose at least one title is it correct? – Alireza Yadegari Sep 28 '18 at 15:15
  • @AlirezaYadegari I'm creating a system that Select all the Titles of the documents and send only the titles of the documents everyday via Email. So I need to use the MailMessage Class and SMTP Client to send. The problem is that the 'Body' of the email only accepts Text. So I created a ListBox that retrieves all titles from my Database and I need to store all the data that I received from my List in Text, in my Textbox (input tag), the user has no interaction. – Pvxtotal Sep 28 '18 at 15:24
  • ok, I just can't understand the last step, you have whatever you want in the server, why you need al data into a textbox? you can do it like this string.join(",",datatable.AsEnumerable().select(x => x.Filed("title"))) and now you have your string and i need to pass this string as your body message .for sending email if you have static template i suggest create HTML file and read it from File.ReadAllText and replace create the string which I describe it above and inside wherever you want. am I Understand your question right? – Alireza Yadegari Sep 28 '18 at 15:35

1 Answers1

1

You can convert DataSet to list

var titleList = ds.Tables[0].AsEnumerable()
.Select(dr => new {Name = dr.Field<string>("TITLE")}).ToList();

Then you can concatenate it into single string:

var titles = titleList.Aggregate((current, next) => current + ", " + next);
Eugene
  • 1,487
  • 10
  • 23
  • Thanks for the answer, I just didn't understand this part: (current, next) => current + ", " + next); – Pvxtotal Sep 28 '18 at 16:21
  • As I understand, you need to get get string with all the titles. Aggregate concatenate all the string values from the list. You can read detailed explanation here https://stackoverflow.com/questions/7105505/linq-aggregate-algorithm-explained – Eugene Sep 28 '18 at 16:24