I want to get the value of the a string from the method I called so I will be able to create a list using InnerHtml.
Here is an example code.
protected void Button1_Click(object sender, EventArgs e)
{
//MailBorrow();
int count = 0;
string[] names = new string[30];
SqlCommand cmdgetnames = new SqlCommand("SELECT Distinct[names] FROM Employees", MyConN);
using (SqlDataReader ReadeR = cmdgetnames.ExecuteReader())
while (ReadeR.Read())
{
names[count] = ReadeR[0].ToString();
count++;
}
foreach (string person in names)
{
Method(person);
Sample.InnerHtml += **INEEDTHISBACK**;
}
}
private void Method(string person)
{
string html = "<h2>"+ person +",</h2>";
SqlCommand cmdgetforPM = new SqlCommand("SELECT [tools] FROM Equipment WHERE owner = '" + person + "'", MyConN);
using (SqlDataReader ReadeR = cmdgetforPM.ExecuteReader())
while (ReadeR.Read())
{
html+= "<p>"+ Reader[tools].ToString() +"</p><br />"
}
**INEEDTHISBACK** = html;
}
I new here, please give me some advise if you think my coding is bad. Thanks! :-)
Why not? Change the return type to string
and return the value that you have generated. Few more things that you have to note here is,
StringBuilder
instead for string The modified method will looks like the following:
private string Method(string person)
{
StringBuilder htmlBuilder = new StringBuilder();
string querySql ="SELECT [tools] FROM Equipment WHERE owner = @person";
using (SqlCommand cmdSql = new SqlCommand(querySql, conObject))
{
cmdSql.Parameters.Add("@person", SqlDbType.VarChar).Value = person;
using (SqlDataReader reader = cmdSql.ExecuteReader())
{
htmlBuilder.Append("<h2>"+ person +",</h2>");
while (reader.Read())
{
htmlBuilder.Append("<p>"+ reader[tools].ToString() +"</p><br />");
}
}
}
return htmlBuilder.ToString();
}
In the Calling method also you can use stringBuilder instead for string, and hence avoid continuous modification of innetHtml text. Changes in the calling method like this:
StringBuilder innerHtml = new StringBuilder();
foreach (string person in names)
{
innerHtml.Append(Method(person));
}
Sample.InnerHtml = innerHtml.ToString();