How to pass parameters in proper manner in C#? <p>I have stored procedure as below:</p> <pre><code>ALTER PROCEDURE [dbo].[Insert_tblCustomer] -- Add the parameters for the stored procedure here @Username varchar(20) = null, @Password varchar(20)= null, @UserType varchar(20)='User', @FirstName varchar(50)=null, @LastName varchar(50)=null, @DateOfBirth varchar(50)=null, @Gender varchar(10)=null, @Unit_No int = null, @St_No int=null, @St_Name varchar(20)=null, @Suburb varchar(20)=null, @State varchar(20)=null, @Postcode int=null, @Email varchar(50)='', @Phone varchar(15)=null </code></pre> <p>AS... and There are 5 fields i.e. username,password,firstname,lastname and email are must fields. Now from c#, I am trying this :</p> <pre><code>dbConnection target = new dbConnection(); // TODO: Initialize to an appropriate value string _query = string.Empty; // TODO: Initialize to an appropriate value SqlParameter[] param = new SqlParameter[5]; // TODO: Initialize to an appropriate value param[0] = new SqlParameter("@Username", "hakoo"); param[1] = new SqlParameter("@Password", "hakoo"); param[2] = new SqlParameter("@FirstName", "Hakoo"); param[3] = new SqlParameter("@LastName", "Hakoo"); param[4] = new SqlParameter("@Email", "haks@abc.com"); bool expected = true; // TODO: Initialize to an appropriate value bool actual; actual = target.Execute_InsertQuery("dbo.Insert_tblCustomer", param); </code></pre> <p>But, I am getting Assertfail exception. I am newbie to use stored procedures with test. Can anyone correct me where I am wrong?</p> <p>"Added Assert part"</p> <pre><code>bool expected = true; // TODO: Initialize to an appropriate value bool actual; actual = target.Execute_InsertQuery("dbo.Insert_tblCustomer", param); </code></pre> <p>dbConnection is my created core class, where I am putting generic query :</p> <pre><code>public bool Execute_InsertQuery(string _query, SqlParameter[] param) { try { SqlCommand cmd = new SqlCommand(); cmd.Connection = OpenConnection(); cmd.CommandText = _query; cmd.Parameters.AddRange(param); cmd.ExecuteNonQuery(); return true; } catch (Exception ex) { Console.Write(ex.StackTrace.ToString()); return false; } } </code></pre> <p>Update for getting Error : I tried to pass all parameters in this manner : </p> <pre><code>dbConnection c = new dbConnection(); System.Data.SqlClient.SqlParameter[] param = new System.Data.SqlClient.SqlParameter[15]; param[0] = new SqlParameter("@Username", "hakoo"); param[1] = new SqlParameter("@Password", "hakoo"); param[2] = new SqlParameter("@UserType", "User"); param[3] = new SqlParameter("@FirstName", "Hakoo"); param[4] = new SqlParameter("@LastName", "Hakoo"); param[5] = new SqlParameter("@DateOfBirth", "02/11/88"); param[6] = new SqlParameter("@Gender", ""); param[7] = new SqlParameter("@Unit_No", null); param[8] = new SqlParameter("@St_No", 25); param[9] = new SqlParameter("@St_Name", "anc st"); param[10] = new SqlParameter("@Suburb", "ancst"); param[11] = new SqlParameter("@State", "assadd@few"); param[12] = new SqlParameter("@Postcode", 2615); param[13] = new SqlParameter("@Email", "abc@def.com.au"); param[14] = new SqlParameter("@Phone", "165103548"); c.Execute_InsertQuery("dbo.Insert_tblCustomer", param); </code></pre> <p>But for this, I am getting this error :</p> <blockquote> <p>The parameterized query '(@Username nvarchar(5),@Password nvarchar(5),@UserType nvarchar(' expects the parameter '@Unit_No', which was not supplied. I can execute this Store procedure in SQL Server.</p> </blockquote>