Only one time, thank you for help !
Public string panda(string lola = @"Server=.\SQLEXPRESS; DataBase=panda; Integrated Security=true;")
{
SqlConnection panda = new SqlConnection(lola);
panda.Open();
return lola;
}
public string Show_details(string Command = "Select name From panda")
{
SqlConnection cn = new SqlConnection(panda());
SqlCommand Show;
SqlDataReader read;
Show = new SqlCommand(Command, cn);
cn.Open();
read = Show.ExecuteReader();
while (read.Read())
{
listBox1.Items.Add(read["name"]);
}
return Command;
}
private void button3_Click(object sender, EventArgs e)
{
Show_details();
}
I'm looking for how to make the reader read data and post it in the listbox only one time !
If I understood your question correctly, you only want to go inside the reader loop once. 'While', no pun intended, there are more efficient ways to go about this, you could declare a bool flag to see if you've hit the loop yet. Once inside the loop, change it to false so when the next time while condition is evaluated, it will evaluate to false ending the loop. See below.
public string Show_details(string Command = "Select name From panda")
{
SqlConnection cn = new SqlConnection(panda());
SqlCommand Show;
SqlDataReader read;
Show = new SqlCommand(Command, cn);
cn.Open();
read = Show.ExecuteReader();
// Declare flag to see if you've hit the reader yet.
bool hasntYetRead = true;
// Add a second condition to determine if to cursor through again
while (read.Read() && hasntYetRead )
{
listBox1.Items.Add(read["name"]);
// Change the flag to false
hasntYetRead = false;
}
return Command;
}