title: Execute stored procedure from WPF
body: <p>I am doing a simple wpf application of registration process. The registration details given by user must be stored in server. I want to use a stored procedure for this. I am using SQL server 2008. I created the table and stored procedure in server for storing the input from user. I am not able to use it in my wpf app. The following is C# code.</p>
  
  <p>Registration.xaml.cs:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  using System.Data.SqlClient;
  using System.Text.RegularExpressions;
  using System.Data; 
  
  namespace storedprocedure
  {
  /// &lt;summary&gt;
  /// Interaction logic for MainWindow.xaml
  /// &lt;/summary&gt;
  public partial class Registration : Window
  {
      public Registration()
      {
          InitializeComponent();
      }
  private void Login_Click(object sender, RoutedEventArgs e)
      {
  
          Login login = new Login();
  
          login.Show();
  
          Close();
  
      }
  
  
  
      private void button2_Click(object sender, RoutedEventArgs e)
      {
  
          Reset();
  
      }
  
  
  
      public void Reset()
      {
  
          textBoxFirstName.Text = "";
  
          textBoxLastName.Text = "";
  
          textBoxEmail.Text = "";
  
          textBoxAddress.Text = "";
  
          passwordBox1.Password = "";
  
          passwordBoxConfirm.Password = "";
  
      }
  
      private void button3_Click(object sender, RoutedEventArgs e)
      {
  
          Close();
  
      }
  
  
  
      private void Submit_Click(object sender, RoutedEventArgs e)
      {
  
          if (textBoxEmail.Text.Length == 0)
          {
  
              errormessage.Text = "Enter an email.";
  
              textBoxEmail.Focus();
  
          }
  
          else if (!Regex.IsMatch(textBoxEmail.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
          {
  
              errormessage.Text = "Enter a valid email.";
  
              textBoxEmail.Select(0, textBoxEmail.Text.Length);
  
              textBoxEmail.Focus();
  
          }
  
          else
          {
  
              string firstname = textBoxFirstName.Text;
  
              string lastname = textBoxLastName.Text;
  
              string email = textBoxEmail.Text;
  
              string password = passwordBox1.Password;
  
              if (passwordBox1.Password.Length == 0)
              {
  
                  errormessage.Text = "Enter password.";
  
                  passwordBox1.Focus();
  
              }
  
              else if (passwordBoxConfirm.Password.Length == 0)
              {
  
                  errormessage.Text = "Enter Confirm password.";
  
                  passwordBoxConfirm.Focus();
  
              }
  
              else if (passwordBox1.Password != passwordBoxConfirm.Password)
              {
  
                  errormessage.Text = "Confirm password must be same as password.";
  
                  passwordBoxConfirm.Focus();
  
              }
  
              else
              {
  
                  errormessage.Text = "";
  
                  string address = textBoxAddress.Text;
  
                  SqlConnection con = new SqlConnection("Data Source=DBSERVER\\DUBAIAIRPORT;Initial Catalog=LoginWPF;User ID=sa;Password=sa");
  
  
      con.Open(); 
  
      using (SqlCommand cmd = new SqlCommand("storedprocedure", con))
      {
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.ExecuteNonQuery(); 
      }
  
      con.Close();
  }
  
  
  
  
  
  
              }
          }
      }
  
  }
  </code></pre>
  
  <p>Below is the stored procedure created in my sql server database</p>
  
  <pre><code>CREATE PROCEDURE submitdata
  (
   @Firstname varchar(50),
   @Lastname varchar(50),
   @Email varchar(50),
   @Password varchar(50),
   @Address varchar(50)
   )
   AS
   insertinto                                                                              registrationdata(Firstname,Lastname,Email,Password,Address)values(@firstname,@lastname,               @email,@password,@address)
  </code></pre>
  
  <p>The name of database is storedprocedure.  I am using VS 2010 with .net4.0.</p>
  
  <p>Please help with the c# code i have to insert in Registration.xaml.cs.
  Thank you.</p>