0

I'm new to C# and I want to simply pull data from SQL database into my C# application in Visual Studios. I have been using a mix of guides to do this but cannot find what is wrong. The code works if I want to recall a table without specifying parameters otherwise it fails.

This is my code currently:

string CS =  "Server = server-sql1;Database=Accounts;Trusted_connection=true";
using (SqlConnection con = new SqlConnection(CS))
{
    SqlCommand cmd = new SqlCommand("ASP_SLINVOICE", con);

    //specify that it is a stored procedure and not a normal proc
    cmd.CommandType = System.Data.CommandType.StoredProcedure;

    //list the parameters required and what they should be
    cmd.Parameters.AddWithValue("@varCURRENCY", "USD");
    cmd.Parameters.AddWithValue("@invSTART", "0");
    cmd.Parameters.AddWithValue("@invEND", "399999");
    cmd.Parameters.AddWithValue("@varCURRENCY", "0");

    con.Open();
    cmd.ExecuteNonQuery();

    using(SqlDataAdapter adap = new SqlDataAdapter(cmd))
    {
        DataTable dt = new DataTable();
        adap.Fill(dt);
        dataGridView1.DataSource = dt;
    }
}

This is the error message I get:

enter image description here

1 Answer 1

1

you define the parameter twice!

cmd.Parameters.AddWithValue("@varCURRENCY", "USD"); // #1
cmd.Parameters.AddWithValue("@invSTART", "0");
cmd.Parameters.AddWithValue("@invEND", "399999");
cmd.Parameters.AddWithValue("@varCURRENCY", "0"); // #2

remove one of them

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.