Click here to Skip to main content
15,883,819 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form to display data, filtered by id and another form which adds values to the same database.

string SQL = "INSERT INTO entradas (id_veiculo,id_empresa,nome_condutor,empresa_visitante,empresa_visitar,visitado,ncartao,data,hora,obs,sector) VALUES (@matricula,@idempresa,@nomecondutor,@empvisitante,@empvisitar,@visitado,@ncartao,@data,@hora,@obs,@sector)";

           using (var cn = new MySqlConnection("server=localhost;user id=root;password=12345;persistsecurityinfo=True;database=portaria;allowuservariables=True"))
           {
               using (var cmd = new MySqlCommand(SQL, cn))
               {
                   cmd.Parameters.AddWithValue("@matricula", txtmatricula.Text);
                   cmd.Parameters.AddWithValue("@idempresa", #);
                   cmd.Parameters.AddWithValue("@nomecondutor", txtnomecondutor.Text);
                   cmd.Parameters.AddWithValue("@empvisitante", txtempvis.Text);
                   cmd.Parameters.AddWithValue("@empvisitar", comboBox1.SelectedValue);
                   cmd.Parameters.AddWithValue("@visitado", txtpessoavisitar.Text);
                   cmd.Parameters.AddWithValue("@ncartao", txtncartao.Text);
                   cmd.Parameters.AddWithValue("@data", DateTime.Now.ToString("yyyy-MM-dd"));
                   cmd.Parameters.AddWithValue("@hora", DateTime.Now.ToShortTimeString());
                   cmd.Parameters.AddWithValue("@obs", txtobs.Text);
                   cmd.Parameters.AddWithValue("@sector", txtsector.Text);
                   cn.Open();

                   try
                   {
                       cmd.ExecuteNonQuery();
                   }
                   catch (MySqlException e)
                   {
                       // Do some logging or something.
                       MessageBox.Show("There was an error accessing your data. DETAIL: " + e.ToString());
                   }
               }
           }


Look for the # symbol on the parameter of "@idempresa"
#here i want something to check the selected tab value

What I have tried:

i've not tryied anything yet because i'm a bit confused about this topic :/
Posted
Updated 1-Mar-16 0:05am
Comments
VR Karthikeyan 1-Mar-16 5:47am    
what do you mean by selected tab value? Do you want the index of the selected tab?
Scribling Doodle 1-Mar-16 5:57am    
i have several tabs, each one according to a company with an id which is on another form, for example... I have a tab with selectedindex value of 1, and i want to be able to set that 1 into my database on the other form. Like textbox use textBox1.Text to insert the value from the textbox into database, i want the selectedindex value, residing in another form.With that in mind i want to be able to insert in my database the SelectedIndex value of the selected tab, need more explanation? Glad to ear from you soon!

1 solution

Hi, Create a class and a property like the following, I assuming you are having TabControl in Form1,
C#
public static class SharedObjects
{
  private static Form _objForm1;
  
  //Property for holding reference of Form1
  public static Form objForm1 
  {
    get { return _objForm1; }
    set { _objForm1 = value; }
  }
}

Now, just assign the reference of Form1 to objForm1 in Load event of Form1 like follows,
C#
SharedObjects.objForm1 = this;

Now we have the reference of Form1 in SharedObjects class, we can use this anywhere inside the project. So you can access the tabcontrol in Form1 in Form2 like following,
C#
cmd.Parameters.AddWithValue("@idempresa", SharedObjects.objForm1.YourTabControl.SelectedIndex);

Try this,
 
Share this answer
 
v2
Comments
Scribling Doodle 1-Mar-16 6:06am    
My problem is that it doesnt show up the tabcontrol object. Because its on another form... How can i sort of "import" the tabcontrol of one form to another? That's a bigger question :/
Sinisa Hajnal 1-Mar-16 8:14am    
The answer is you don't. You pass the parameter from that page either via session object, URL parameter when navigating away from it or any other mechanism of transferring data between the pages. You do NOT access tab control from one page and get its value into another.

The above solution would be cmd.Parameters.AddWithValue("@idempresa", QueryString["tabIndex"]); or cmd.Parameters.AddWithValue("@idempresa", Session["tabIndex"]); assuming you saved the value of selected index before navigating away from the page
Scribling Doodle 1-Mar-16 8:55am    
i'm confused right know, you mean that's impossible to use tabcontrol.SelectedIndex according to my needs?

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



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