0

I have this code to show/hide Canvas with some text boxes, and when I press a button it submits data from TextBox1 to database. The problem is that I don't know how to access TextBox1 in C# code behind.

For example this is some of my XAML code:

<ContentControl  Background="{x:Null}" >
  <ContentControl.Template>
     <ControlTemplate>
      <StackPanel Grid.Row="3" Height="500" Name="stack1" Width="280">
        <Canvas x:Name="canvas1"  Height="400" >
              <TextBox Height="23" Name="TextBox1"  Width="70" />

    <Button Content="Submit" Name="submit_button" Click="submit_button_Click" />
        </Canvas>

  <ToggleButton x:Name="toggleshowhide" Content="Show/Hide" IsChecked="True" Height="50" />
      </StackPanel>
     <ControlTemplate.Triggers>
      <Trigger SourceName="toggleshowhide" Property="IsChecked" Value="True">
        <Setter TargetName="canvas1" Property="Visibility" Value="Visible" />
          </Trigger>
      <Trigger SourceName="toggleshowhide" Property="IsChecked" Value="False">
        <Setter TargetName="canvas1" Property="Visibility" Value="Hidden" />
          </Trigger>
    </ControlTemplate.Triggers>
      </ControlTemplate>
  </ContentControl.Template>
     <Button Content="Button" Height="23" Name="submit" Width="74" />
</ContentControl>

And this is what I'm trying to achieve:

private void submit_button_Click(object sender, RoutedEventArgs e)
{
      OleDbCommand cmd = new OleDbCommand("INSERT INTO Table VALUES (this.TextBox1.Text), con); 

      cmd.Connection = con;

         int temp = cmd.ExecuteNonQuery();

        if (temp > 0)
        {
            MessageBox.Show("OK", "Info !");
        }
        else
        {
            MessageBox.Show("Some text !", "Error");
        }

If anyone can help I`ll really appreciate it. :)

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
  • We need to see your namespace declarations. – Dour High Arch Oct 13 '12 at 00:39
  • xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" – user1742537 Oct 13 '12 at 00:55

3 Answers3

1

When accessing elements generated from a ControlTemplate, you need to use the FindName method of the Template. In this case, give your ContnetControl a name:

<ContentControl  Background="{x:Null}" x:Name="MyContentControl">

And in the code behind, you can access the value of any of the generated elements by name using the FindName method:

TextBox tb = (TextBox)MyContentControl.Template.FindName( "TextBox1", MyContentControl);

For more information, see: How to: Find ControlTemplate-Generated Elements

Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
0
string v = this.TextBox1.Text;
OleDbCommand cmd = new OleDbCommand('INSERT INTO Table VALUES (' + v + ');');

BTW this is pretty bad idea, read about sql injection...

Z .
  • 12,657
  • 1
  • 31
  • 56
  • I cannot use TextBox1.Text, it doesn`t recognize it, like its not there – user1742537 Oct 13 '12 at 00:28
  • Here is the picture of my app, hope it shows you what i want to achieve: http://fsrmo.comule.com/uploads/samplepicture.jpg – user1742537 Oct 13 '12 at 00:38
  • whole code or just this part,beause there is a lot of xaml code there :) ? – user1742537 Oct 13 '12 at 00:42
  • just the relevant part from the xaml and c#... just couple of lines of each – Z . Oct 13 '12 at 00:44
  • you changed the name of the button to "submit" – Z . Oct 13 '12 at 00:48
  • No, i gave you just example in code above, these button and textboxes names are not actually in my code,they are just demo of what I want to achieve. – user1742537 Oct 13 '12 at 00:50
  • private void submit_Click(object sender, RoutedEventArgs e) { OleDbCommand cmd = new OleDbCommand("INSERT INTO Literatura VALUES ('" + this.dodaj.Text + "', '" + this.sampletextbox2.Text + "', '" + this.t3.Text + "','" + this.t4.Text + "','" + this.t5.Text + "','" + this.t6.Text + "','" + this.t7.Text + "','" + this.t8.Text + "')", con); cmd.Connection = con; int temp = cmd.ExecuteNonQuery(); if (temp > 0) { MessageBox.Show("Some text"); } else { MessageBox.Show("Some text!"); } } – user1742537 Oct 13 '12 at 00:53
  • in this case this.dodaj.Text is mine TextBox1 :) – user1742537 Oct 13 '12 at 00:56
  • you read the values from dodaj, sampletextbox2, t3, t4, t5, t6, t7 and t8 but not submit... – Z . Oct 13 '12 at 00:58
  • then change the name of the textbox in the xaml to be dodaj, not submit – Z . Oct 13 '12 at 00:58
  • It is already like that, I`m just giving you sample names to understand better because button names and textbox names are not in english. I should probably upload whole code somewhere in txt and send you link to better understand, if you are willing to help :) – user1742537 Oct 13 '12 at 01:02
0

give your TextBox an x:Name not a Name ... does that even Compile ?

xaml :

<TextBox x:Name="TextBox1" />

cs :

var s = TextBox1.Text;
eran otzap
  • 12,293
  • 20
  • 84
  • 139