2

I'm trying to output the result of my SQL query to a div using the InnerHTML. but I can't get more than one record to show up. How do I make it so that all of my records are displayed in the DIV? On my page I have the DIV contained within a UpdatePaneland I only 1 row is displayed even when the query returns more than one row.

string sql = "Select * From Events";
SqlCommand command = new SqlCommand(sql, conn);
command.CommandType = CommandType.Text;
conn.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
    divout1.InnerHtml += "Name: " + reader["Name"].ToString() + "<br />" +
        "Date: " + reader["Date"].ToString() + "<br />" +
        "Location: " + reader["Location"].ToString() + "<br />";
    divout.Visible = true;
}
2
  • 4
    By any chance are showing divout instead of divout1? Commented Mar 30, 2011 at 18:49
  • This isn't the problem, but you need to escape the strings from the database before you put them in the div. Commented Mar 30, 2011 at 18:53

5 Answers 5

1

Well, at first look, unless your query returns only one row, it should get all data on your div.

try to put a break point on the last bracket of your while statement and see what happens on your div after first iteration.

for other way, try to put the div out of the updatepanel to see what appens?

Sign up to request clarification or add additional context in comments.

Comments

1

Why not use a StringBuilder to build your sql and then output the contents of the StringBuilder:

StringBuilder sb = new StringBuilder(1024);
string sql = "Select * From Events";
SqlCommand command = new SqlCommand(sql, conn);
command.CommandType = CommandType.Text;
conn.Open();
reader = command.ExecuteReader();

while (reader.Read())
{
    sb.AppendLine("Name: " + reader["Name"].ToString() + "<br />" +
        "Date: " + reader["Date"].ToString() + "<br />" +
        "Location: " + reader["Location"].ToString() + "<br />");
}

if(sb.Length > 0) {
    divout.InnerHtml = sb.ToString();
    divout.Visible = true;
}

Comments

1

Why not try something like this, may be concatenating inside the InnerHtml is screwing up or something. also be sure you really have more than one row.

    var sb = new StringBuilder();  
    var formatString = "Name: {0} <br />Date: {1}<br />Location: {2}<br />"; 
    while (reader.Read())
    {
        sb.AppendFormat(formatString, reader["Name"].ToString(), reader["Date"].ToString(), reader["Location"].ToString());
    }
    divout1.InnerHtml = sb.Tostring();
    divout1.Visible = true;

Comments

0

Try:

            string sql = "Select * From Events";
            SqlCommand command = new SqlCommand(sql, conn);
            command.CommandType = CommandType.Text;
            conn.Open();
            reader = command.ExecuteReader();
            string s = string.Empty;
            while (reader.Read())
            {
                s += "Name: " + reader["Name"].ToString() + "<br />" +
                    "Date: " + reader["Date"].ToString() + "<br />" +
                    "Location: " + reader["Location"].ToString() + "<br />";

            }
            divout1.innerHtml = s;
            divout1.Visible = true;

1 Comment

Although using a StringBuilder is better
0

You should probably encode the strings coming from the Database since you're directly outputting it as raw HTML.

divout1.InnerHtml += "Name: " + Server.HtmlEncode(reader["Name"].ToString()) + "<br />" 
                   + "Date: " + Server.HtmlEncode(reader["Date"].ToString()) + "<br />"  
                   + "Location: " + Server.HtmlEncode(reader["Location"].ToString()) + "<br />";

see HttpServerUtility.HtmlEncode

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.