Click here to Skip to main content
13,004,203 members (67,688 online)
Rate this:
Please Sign up or sign in to vote.
See more:
private void button1_Click_1(object sender, EventArgs e)
   if (tabControl1.SelectedTab == tabPage1)
      SqlConnection sconn = new SqlConnection();
      sconn.ConnectionString = "Data Source=.\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=depom";;
      SqlCommand ins = new SqlCommand();
      ins.Connection = sconn;
      ins.CommandText = "update .....";
      SqlCommand ons = new SqlCommand();
      ons.Connection = sconn;
      ons.CommandText = "insert into ......";
         "....", malzemeDataGridView.Rows[i].Cells["nadi"].Value.ToString());
   else if (tabControl1.SelectedTab == tabPage2)
      SqlConnection sconna = new SqlConnection();
      sconna.ConnectionString = "Data Source=.\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=depom";
      SqlCommand ups = new SqlCommand();
      ups.Connection = sconna;
      ups.CommandText = "update ....";
      SqlCommand dps = new SqlCommand();
      dps.Connection = sconna;
      dps.CommandText = "...";
      SqlCommand pons = new SqlCommand();
      pons.Connection = sconna;
      pons.CommandText = ".....";
      MessageBox.Show("try out");

There is a tabControl with three tabPages. in pages there are datagrids. I am pretty sure the sql statements work individually at their own tab page. I need to use those statements with one button out of tabControl. also if statement works i tested it with "
MessageBox.Show("tab page 1");
" and
MessageBox.Show("tab page 2);

and also the button works at tabpage1 but why it isnt working on tabpage2?

i hope you understand my question.

thanks in advance...
Posted 22-Sep-12 1:19am
Updated 22-Sep-12 12:34pm
Wes Aday 22-Sep-12 7:26am
Probably, mostly because you are not doing anything with the data.
Christiaan Rakowski 22-Sep-12 9:35am
I assume the syntax errors in there are because of copy-pasting it into the formatter, but there appear to be some strings where they shouldn't be.

Is button1 inside, or outside of the tabs. (aka is there only 1 button, or are there 3, 1 per page) If it is the second option, are those buttons attached to correct event?
tolgasenol 23-Sep-12 1:31am
button1 is outside of the tabs but in the same form. i tried MessageBox.Show for each tabPage it worked. Also i made a button control inside the second tab with same code it is working.

What do you mean buttons attached to correct event? if you need i can paste all code for button1. thanks to all of you for your reply.

Lets make this way.
There are three buttons and a tabcontrol with three tabpages in a form. buttons are outside of the tabcontrol. i want button1 is visible when tabpage1 selected, button2 visible when tabpage2 selected and button3 visible when tabpage3 selected.

if (tabControl1.SelectedTab == tabPage1)
button1.Visible = true;
else if (tabControl1.SelectedTab == tabPage2)
button2.Visible = false;
else if (tabControl1.SelectedTab == tabPage3)
button3.Visible = false;

that isnt working too.
tolgasenol 23-Sep-12 1:32am
thanks to your reply. but i tried a button inside the tabPage it worked.
Christiaan Rakowski 23-Sep-12 4:45am
What I meant with all buttons attached to the correct event does not apply to your situation, you only have one button and not 3.

There is nothing wrong with the rest of the code as far as I can see.
Maybe it is because of some double used variables for the SQL code, so the query becomes invalid and does not give the correct result?
The simplest way to check this is by cleaning up your code and putting all the code in helper methods so that the code in button1_Click will be like this:

if (tabControl1.SelectedTab == tabPage1)
else if (tabControl1.SelectedTab == tabPage2)
else if (tabControl1.SelectedTab == tabPage3)

If that didn't help, maybe you could upload the whole project somewhere where I can download it, and I will take a closer look at it.
supernorb 28-Sep-12 0:06am
When you say the button doesn't work with tabpage2 just because of the else clause. You should check if all the code in that else block work well. This is not a problem with the button or tabpage2.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170627.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100