Reputation: 51
I'm trying to pass multiple results from (same) SQL query to one TextBox, however I'm receiving only one value (result). Why? TextBox has TextMode with Multiline.
protected void Button1_Click(object sender, EventArgs e)
{
string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\logi.accdb";
using (OleDbConnection con = new OleDbConnection(constr))
{
using (OleDbCommand cmd = new OleDbCommand("SELECT Action_time, User_name, Value_type, New_value FROM action_history"))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (OleDbDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
TextBox1.Text = sdr["Action_time"].ToString() + " " + sdr["User_name"].ToString()
+ " " + sdr["Value_type"].ToString() + " " + sdr["New_value "].ToString();
}
con.Close();
}
}
}
Upvotes: 0
Views: 146
Reputation: 1082
You should take a look at the documentation regarding the Read()
method of the OleDbDataReader. It returns a boolean value, if true than there are more rows to return. If not then it'll return false.
so instead of just using this sdr.Read();
you should be using while(sdr.Read())
. Using it like this will return all the results from the query you executed.
Also do not assign the results to the textbox like that, doing like so will overwrite the text of the textbox. Rather use a StringBuilder for creating the text you want to assign to the textbox. After all the results are appended to the StringBuilder in the while loop, you can then assign it to TextBox1.Text
.
// outside the while loop
var stringBuilder = new StringBuilder();
while (sdr.Read())
{
// inside the while loop
var result = sdr["Action_time"].ToString() + " " + sdr["User_name"].ToString()
+ " " + sdr["Value_type"].ToString() + " " + sdr["New_value "].ToString();
StringBuilder.AppendLine(result);
}
// outside the while loop
TextBox1.Text = stringBuilder.ToString()
Upvotes: 1