Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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";
      sconn.open();
      SqlCommand ins = new SqlCommand();
      ins.Connection = sconn;
      ins.CommandText = "update .....";
      ins.Parameters.AddWithValue("....");
      ins.Parameters.AddWithValue("...");
      SqlCommand ons = new SqlCommand();
      ons.Connection = sconn;
      ons.CommandText = "insert into ......";
      ons.Parameters.AddWithValue(
         "@...",malzemeDataGridView.Rows[i].Cells["dataGridViewTextBoxColumn1"].Value.ToString());
      ons.Parameters.AddWithValue(
         "....", malzemeDataGridView.Rows[i].Cells["nadi"].Value.ToString());
 //......
      ins.ExecuteNonQuery();
      ons.ExecuteNonQuery();
      sconn.Close();
   }
   else if (tabControl1.SelectedTab == tabPage2)
   {
      SqlConnection sconna = new SqlConnection();
      sconna.ConnectionString = "Data Source=.\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=depom";
      sconna.Open();
      SqlCommand ups = new SqlCommand();
      ups.Connection = sconna;
      ups.CommandText = "update ....";
      ups.Parameters.AddWithValue("...");
      ups.Parameters.AddWithValue("...");
      SqlCommand dps = new SqlCommand();
      dps.Connection = sconna;
      dps.CommandText = "...";
      dps.Parameters.AddWithValue("...");
      SqlCommand pons = new SqlCommand();
      pons.Connection = sconna;
      pons.CommandText = ".....";
      pons.Parameters.AddWithValue("....");
      pons.Parameters.AddWithValue("....");
      //.....
      pons.ExecuteNonQuery();
      ups.ExecuteNonQuery();
      dps.ExecuteNonQuery();
      sconna.Close();
   }
   else
   { 
      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 "
C#
MessageBox.Show("tab page 1");
" and
C#
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
Updated 22-Sep-12 12:34pm
v3
Comments
[no name] 22-Sep-12 7:26am    
Probably, mostly because you are not doing anything with the data.
tolgasenol 23-Sep-12 1:32am    
thanks to your reply. but i tried a button inside the tabPage it worked.
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.
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)
{
getDataForTab1();
}
else if (tabControl1.SelectedTab == tabPage2)
{
getDataForTab2();
}
else if (tabControl1.SelectedTab == tabPage3)
{
getDataForTab3();
}

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.

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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900