-1
protected void Button1_Click(object sender, EventArgs e)
{
        SqlConnection MyConnection = new SqlConnection("Data Source=VIJAYSTIWARI\\SQLEXPRESS;Initial Catalog=earthquake;User ID=sa;Password=HereIsPwd;");

        MyConnection.Open();

        SqlCommand MyCommand = new SqlCommand("SELECT * FROM eq", MyConnection);
        SqlDataReader mydr = MyCommand.ExecuteReader();

        if (mydr.HasRows)
        {
             while(mydr.Read())
             {
                 TextBox7.Text = mydr.GetString(1);
             }
        }

        MyConnection.Close();
    }

My table's name is eq and it contains this data:

country
---------
india
japan
mexico
afghanistan
australia
hungary
FLICKER
  • 6,439
  • 4
  • 45
  • 75
  • I mean to say that I have to iterate SqlDataReader in such a manner that its values will be displayed in textbox one after another ( I.e. when first time button will be clicked then textbox should display first country. when we again click the button then first country name should be completely disappear from the textbox and it will be replaced by second country's name. – vijaydidmca Mar 29 '18 at 17:17
  • ,take a look at my anser – Software Dev Mar 29 '18 at 17:21
  • You do not want to keep your sql reader open like this. Do as the answer proposes and save your sql results to a list. Then use that list when performing your ui operations. – user7396598 Mar 29 '18 at 22:27

1 Answers1

1

Just change TextBox7.Text = mydr.GetString(1); to :

 TextBox7.Text = TextBox7.Text + "," + mydr.GetString(1);

If your textbox supports multiline,then do this :

 TextBox7.Text = TextBox7.Text + Environment.NewLine + mydr.GetString(1);

or create a list and then you can use buttons to display data.Sample :

List<string> countries = new List<string>
......
while(mydr.Read())
         {
             countries.Add(mydr.GetString(1));
 ......

Then on button_Click,use this :

int myint = 0;
textBox1.Text = countries.Items[myint]
myint = myint + 1;
Software Dev
  • 5,368
  • 5
  • 22
  • 45
  • hi zack thanks for quick response. I am trying to say that after next button click first country's name should be completely disappear and next country's name should take it's place. – vijaydidmca Mar 29 '18 at 17:21
  • see my updated answer and use the last method provided :) – Software Dev Mar 29 '18 at 17:22
  • I have to capture table values in textboxes, so that it will be convenient to further process them. – vijaydidmca Mar 29 '18 at 17:23
  • In my code, i stored the data from the sqlDataReader in a list of string,you can always use the list..It is a better option..Try out my solution first and then let me know :) – Software Dev Mar 29 '18 at 17:24
  • hi zack. there is some more data(I have), which is integer, decimal form and I believe that there should be some other method to get them in textboxes from database tables – vijaydidmca Mar 29 '18 at 17:28
  • Can u explain a bit more ?? You can follow my method , it will always work :) – Software Dev Mar 29 '18 at 17:30