1

I want to execute a query on a button click event.

But that query is written in another function.

Here is my code, and it's not working. What is my problem?

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();
        }
    }
}
2
  • This query executes empty. I thought it is because of that SqlCommand cmd thing is in another function... Commented Mar 14, 2013 at 9:37
  • it's a bad design because you are setting an object in class level. Commented Mar 14, 2013 at 9:38

3 Answers 3

1

I'm not sure if this solves your problem. But if you really need another method to create your command, let it return it.

protected SqlCommand  GetCommand() 
{
    SqlCommand cmd = new SqlCommand("Insert into blahblah values(blahblah)", connection);
    return cmd;
}

protected void Button1_Click() {
    connection.Open();
    GetCommand().ExecuteNonQuery();
    connection.Close();
}

Note that this is not best-practise due to several reasons. The connection should be closed even if an exception occured so use using statement instead. But that would be a problem in this approach since the connection is a field.

So i would prefer the all-in-one method approach which also uses parameters tro prevent sql-injection attacks:

protected void Button1_Click() 
{
    ExecuteBlahBlahCommand("blahblah");
}

private void ExecuteBlahBlahCommand(string blaColumnVal)
{
    const string sql = "Insert into blahblah values(@blaColumn)";
    using (var con = new SqlConnection(connectionString))
    using (var cmd = new SqlCommand(sql, con))
    {
        cmd.Parameters.AddWithValue("@blaColumn", blaColumnVal);
        con.Open();
        cmd.ExecuteNonQuery();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I suggest you to use CommandText property and not contructor, because instance of cmd is created before this code, so you adjust your property

protected void CreateQuery() {

    cmd.CommandText = "Insert into blahblah values(blahblah)";
}

protected void Button1_Click() {

    connection.Open();
    CreateQuery();

    cmd.ExecuteNonQuery();
    connection.Close();
}

Comments

0

Answering the question itself - Any variable you declare inside a function cannot be seen outside that function. You need to declare the SqlCommand in the correct scope...

For instance:

SqlCommand cmd;
protected void CreateQuery() 
{
   cmd = new SqlCommand("Insert into blahblah values(blahblah),connection)";
}

protected void Button1_Click() 
{
  CreateQuery();
  connection.Open();
  cmd.ExecuteNonQuery();
  connection.Close();
}

This will declare the variable in the class level, and be accessible to all other methods in that class.

I'll just mention that @Tim Schmelter's answer is a good solution that might better suit your needs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.