title: Passing parameter to Stored Procedure from C#
body: <p>I'm new to Stored Procedures, What I normally did before was to have a database class and passing sql Query to the select or update method. Now I have following class for My database</p>
  
  <pre><code>class DatabaseService
      {
  
          static SqlConnection connection;
  
          public void getConnection()
          {
  
              try
              {
                  connection = new SqlConnection("Data Source=PC-DILUKSHAN\\SQLEXPRESS;Initial Catalog=SPTESTDB;User ID=user;Password=1234");
                  connection.Open();
  
              }
              catch (Exception e)
              {
  
                  throw e;
              }
  
          }
          public DataTable executeSelectQuery(String sql)
          {
              getConnection();
  
              try
              {
                  SqlCommand cmd = new SqlCommand(sql, connection);
                  cmd.CommandType = CommandType.StoredProcedure;
                  // Create a DataAdapter to run the command and fill the DataTable
                  SqlDataAdapter da = new SqlDataAdapter();
                  da.SelectCommand = cmd;
                  DataTable dt = new DataTable();
                  da.Fill(dt);
  
                  return dt;
  
              }
              catch (Exception e)
              {
  
                  throw e;
              }
              finally
              {
  
                  connection.Close();
              }
  
          }
  
          public void executeUpdateQuery(String sql)
          {
              getConnection();
              try
              {
                  SqlCommand cmd = new SqlCommand(sql, connection);
                  cmd.CommandType = CommandType.StoredProcedure;
                  cmd.ExecuteNonQuery();
  
              }
              catch (Exception e)
              {
  
                  throw e;
              }
              finally
              {
                  connection.Close();
  
              }
  
          }
      }
  </code></pre>
  
  <p>This methods of my database class was created for common use, to all select and update transactions, but adding those params to class itself does make it specific for that transaction, right? can i add the parameter inside the form load event?</p>
  
  <p>Now in my WinForm Load event I'm populating a grid with data through a Stored Procedure but i need to pass @cusid to it. Code looks like below where i get an error saying "Procedure or function 'spCustomerParam' expects parameter '@cusid', which was not supplied."</p>
  
  <pre><code> private void Form1_Load(object sender, EventArgs e)
          {
              DataTable dt = Program.db.executeSelectQuery("spCustomerParam");
              dataGridView1.DataSource = dt;
  
          }
  </code></pre>