3

I think I'm missing an 'USING" statement in my class as I'm getting an error when I try to set the commandType to stored procedure. When I type 'cmd.CommandType =', Intellisense fails to find the 'CommandType.StoredProcedure (Note: the function is only partly roughed out). Thanks in advance!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;


namespace LegacyForms.Personal
{
    public partial class FormBuilder : System.Web.UI.Page
    {
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //Get the DB connection:
            string ConnString = ConfigurationManager.AppSettings["AssociatedBank2011ConnectionString"];
            SqlConnection conn = new SqlConnection(ConnString);



        SqlCommand cmd = new SqlCommand("uspInsertPersonalAccountApplcation", conn);
        cmd.Commandtype = **get error here!**
        cmd.Parameters.AddWithValue("@AccountType", AcctType);
        cmd.Parameters.AddWithValue("@AccountSubType", AcctSubType);
        cmd.Parameters.AddWithValue("@CheckingOption", CheckOption);

        conn.Open();
        cmd.ExecuteNonQuery();

        conn.Close();
    }
}

}

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Susan
  • 1,822
  • 8
  • 47
  • 69

3 Answers3

2
using System.Data;

You need to reference System.Data. See the MSDN Reference for the CommandType Enumeration. Direct quote:

Namespace: System.Data
Assembly: System.Data (in System.Data.dll)

1

I'd also recommend the other using statement for your SqlConnection and SqlCommand objects. Since they both implement the IDisposable interface, you can do the following:

string ConnString = ConfigurationManager.AppSettings["AssociatedBank2011ConnectionString"];
using (SqlConnection conn = new SqlConnection(ConnString))
using (SqlCommand cmd = new SqlCommand("uspInsertPersonalAccountApplcation", conn))
{
    cmd.Commandtype = CommandType.StoreProcedure;
    cmd.Parameters.AddWithValue("@AccountType", AcctType);
    cmd.Parameters.AddWithValue("@AccountSubType", AcctSubType);
    cmd.Parameters.AddWithValue("@CheckingOption", CheckOption);

    conn.Open();
    cmd.ExecuteNonQuery();
}

That way, in the case that your code works correctly or throws an exception in the using block, your SqlConnection and SqlCommand will clean up after themselves.

Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55
0

In such situations you can press CTRL + . (ctrl + dot) to get a suggestion like do you want to add using System.Data...

P.S. Teach a men to fish ...

Tibi
  • 1,507
  • 1
  • 9
  • 10