title: How can I call a SQL function in C#?
body: <p>I have created a function in SQL, now I need to use that function in my C# application.</p>
  
  <p>I tried using something like this, but it seems I'm doing it wrong since I'm getting:</p>
  
  <pre><code>Must declare the scalar value '@2064734117'
  </code></pre>
  
  <p>...when I give <code>2064734117</code> as the first parameter and <code>1</code> as the second parameter. Here is the code I'm talking about:</p>
  
  <pre><code>SqlConnection con = new SqlConnection(clsDb.connectionString);
  string query = string.Format("select Function1(@{0},@{1}) ",
    int.Parse(e.CurrentRow.Cells["CodeMeli"].Value.ToString()),1);
  con.Open();
  SqlCommand cmd = new SqlCommand(query,con);
  SqlDataAdapter READER = new SqlDataAdapter();
  READER.SelectCommand = cmd;
  DataTable table = new DataTable();
  READER.Fill(table);
  radGridView1.DataSource = table;
  con.Close();
  </code></pre>
  
  <p>And my function takes two integer parameters and returns a table. I checked it in Visual Studio and it worked, but I couldn't get it to work in my application.</p>
  
  <p>And this is my function declaration: </p>
  
  <pre><code>ALTER FUNCTION dbo.Function1
  (
  /*
  @parameter1 int = 5,
  @parameter2 datatype
  */
  @ID int,
  @clsTypeID int
  )
      RETURNS TABLE/* @table_variable TABLE (column1 datatype, column2 datatype) */
      AS
           /*BEGIN */
      /* INSERT INTO @table_variable
         SELECT ... FROM ... */
  RETURN SELECT  * FROM tblCLASS2 
          WHERE STNID = @ID AND CLASSTYPEID =  @clsTypeID  
  /*END */
  /*GO*/
  </code></pre>