Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL WinForm
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 "
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
Edited 22-Sep-12 12:34pm
0
v3
Comments
Wes Aday at 22-Sep-12 7:26am
   
Probably, mostly because you are not doing anything with the data.
tolgasenol at 23-Sep-12 1:32am
   
thanks to your reply. but i tried a button inside the tabPage it worked.
Christiaan Rakowski at 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 at 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 at 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.
supernorb at 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
0 Sergey Alexandrovich Kryukov 359
1 Nirav Prabtani 273
2 OriginalGriff 229
3 _Amy 165
4 Maciej Los 145
0 OriginalGriff 7,744
1 Sergey Alexandrovich Kryukov 6,956
2 Maciej Los 3,994
3 Peter Leow 3,708
4 CHill60 2,742


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2014
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