简体   繁体   中英

How to display different sql data in a text box when a button is clicked

My MySql database consists of a column id which contains numbers from 1 to 30. I want to display the number 1 to 30 in a text box each time a button is pressed. However, my code displays only first row that is 1.

I have tried following code:

SqlConnection Conn = new SqlConnection("Data Source=SUMIT;Initial Catalog=Project;Integrated Security=True"); 
SqlCommand Comm1 = new SqlCommand("Select * from id", Conn); 
Conn.Open(); 
SqlDataReader DR1 = Comm1.ExecuteReader();

if (DR1.Read()) {
    textBox3.Text = DR1.GetValue(0).ToString();
}
Conn.Close();

I suspect the reason is you have all that code in your button click event. So every time you click the button, it creates a new connection, executes the query and shows you the first record.

I recommend you reorganize your code.

For example, call the SetupData() function once(maybe on load time or anywhere as long as it is executed only once) then click the button click event will only contain the if statement.

SqlDataReader DR1;
public void SetupData(){
     SqlConnection Conn = new SqlConnection("Data Source=SUMIT;Initial          Catalog=Project;Integrated Security=True"); 
     SqlCommand Comm1 = new SqlCommand("Select * from id", Conn); 
     Conn.Open(); 
     DR1 = Comm1.ExecuteReader();
}

 void OnButtonClick(object sender, EventArgs e)
 {
            if (DR1.Read()) {
                textBox3.Text = DR1.GetValue(0).ToString();
 }

There are few ways of doing this.

First Using SQL

Keep the count of the number outside the button click function

int BtnCount = 0;

and in Button Click

SqlConnection Conn = new SqlConnection("Data Source=SUMIT;Initial Catalog=Project;Integrated Security=True"); 
SqlCommand Comm1 = new SqlCommand("Select * from id WHERE ID = " + BtnCount  , Conn); 
Conn.Open(); 
SqlDataReader DR1 = Comm1.ExecuteReader();

if (DR1.Read()) {
    textBox3.Text = DR1.GetValue(0).ToString();
}
Conn.Close();
BtnCount ++;
if(BtnCount>30)
{
    BtnCount = 0;
}

Secondly you can call all the data in the collection / list / array which will also enhance the speed of your system

Create the list of strings

List<string> ListData = new List<string>();

Populate the list at the constructor like this

SqlConnection Conn = new SqlConnection("Data Source=SUMIT;Initial Catalog=Project;Integrated Security=True"); 
SqlCommand Comm1 = new SqlCommand("Select * from id WHERE ID = " + BtnCount  , Conn); 
Conn.Open(); 
SqlDataReader DR1 = Comm1.ExecuteReader();

while(DR1.Read()) 
{
    ListData.Add(DR1.GetValue(0).ToString());
}
Conn.Close();

And on Button Click show the content of the list by BtnCount

Note : It is just to give you the idea I havent run the program to test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM