Link to home
Start Free TrialLog in
Avatar of Gracie2012
Gracie2012

asked on

VS 2008 c# windows application user entry form

I want to know the best way to execute a stored procedure from a c# windows application user entry form.  Simply, insert records into a table.  The most basic.  I've tried using the binding navigation and putting code behind the "+" and that did not work so I am thinking of just going with simple text boxes which will hold the parameters to be passed to the sql procedure.  That is not working either.  Can someone point me in the direction of either how to get what I have working or a better way to do this.  I have the insert statement in the code below to make it simple but ideally I'd call my procedure instead.  I will say I am very inexperienced at c# programming.  No error, the insert just never happens.

private void button1_Click(object sender, EventArgs e)
        {
            SqlCommand dbCommand = new SqlCommand();
            //dbCommand.CommandText = "InsCustomer";  <-----stored proc
            //dbCommand.CommandType = CommandType.StoredProcedure;
            string sqlQuery = "INSERT INTO Customer (Utility, CustID)";
            sqlQuery += " VALUES (@Utility, @CustID)";
            string connectionstring = ("blah blah blah");

            using (SqlConnection dataConnection = new SqlConnection(connectionstring))
            using (SqlCommand dataCommand = new SqlCommand(sqlQuery, dataConnection))
            {
                dataConnection.Open();

                dataCommand.Parameters.AddWithValue("Utility", textBox1.Text);
                dataCommand.Parameters.AddWithValue("CustID", textBox2.Text);

            }
        }
    }
ASKER CERTIFIED SOLUTION
Avatar of momi_sabag
momi_sabag
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nishant joshi
good code by  Gracie2012 :)
but might you just missed @ before parameter adding
check code.
private void button1_Click(object sender, EventArgs e)
        {
            SqlCommand dbCommand = new SqlCommand();
            //dbCommand.CommandText = "InsCustomer";  <-----stored proc
            //dbCommand.CommandType = CommandType.StoredProcedure;
            string sqlQuery = "INSERT INTO Customer (Utility, CustID)";
            sqlQuery += " VALUES (@Utility, @CustID)";
            string connectionstring = ("blah blah blah");

            using (SqlConnection dataConnection = new SqlConnection(connectionstring))
            using (SqlCommand dataCommand = new SqlCommand(sqlQuery, dataConnection))
            {
                dataConnection.Open();

                dataCommand.Parameters.AddWithValue("@Utility", textBox1.Text);
                dataCommand.Parameters.AddWithValue("@CustID", textBox2.Text); 

            }
        }
    }

Open in new window

Avatar of Gracie2012
Gracie2012

ASKER

I had resolved this yesterday but that was the correct answer, I had not called the execute method....  I'm only giving this a C though because it does not really explain then how to do this, like I said, I'm very new..