title: Execute Query from another function
body: <p>I want to execute a query on a button click event.</p>
  
  <p>But that query is written in another function.</p>
  
  <p>Here is my code, and it's not working. What is my problem? </p>
  
  <pre><code>namespace MCE_Member_Registration
  {
      public partial class registration_form_view : System.Web.UI.Page
      {
          SqlConnection conn = new SqlConnection("ConnectionString");
          SqlCommand cmd;
          protected void Page_Load(object sender, EventArgs e)
          {
              createform();
          }
  
          protected void createform() {
              NameValueCollection nvc = Request.Form;
              surname.Text = nvc["txt_surname"];
              cmd.CommandText = "Insert into mce_applicants_information values(N'" + nvc["txt_surname"] + "')";
          }
  
          protected void confirm_Click(object sender, EventArgs e)
          {
              conn.Open();
              cmd.ExecuteNonQuery();
              conn.Close();
          }
      }
  }
  </code></pre>