3

I have a datatable method that returns info but i have a problem with my parameters it keeps throwing out a Must declare the variable '@SUPartnerNo' Error. i have tried to google but none of the solutions i have found could help. and i dont want to embed my parameters directly into my query as its not good practice

public DataTable SearchInvoiceHeader(string SUPartnerNo, string invoiceNo, string bdate, string edate)
{
    DataTable dt = new DataTable();

    try
    {
        StringBuilder mySelectQuery = new StringBuilder(9000);


        mySelectQuery.AppendLine(@"SELECT I.[InvoiceNo]
                                    ,I.[SUPartnerNo]
                                    ,I.[OrderNo]
                                    ,I.[RefNo]
                                    ,I.[DocType]
                                    ,I.[SAP_Type]
                                    ,I.[SUName]
                                    ,I.[InvoiceDate]
                                    ,I.[PaymentDate]
                                    ,I.[BaseLineDate]
                                    ,I.[DeliveryDate]
                                    ,I.[DeliveryNoteNo]
                                    ,I.[TotalAmount]
                                    ,I.[StartTime]
                                    ,p.ProcessType
                                    ,s.DocDate
                                    ,s.IDocNo
                                    ,s.TotalValue
                                FROM [dbo].[mainhead] I WITH (NOLOCK) 
                                LEFT JOIN [INV_Processed] p WITH (NOLOCK) 
                                ON p.InvoiceNo = I.InvoiceNo
                                AND p.PartnerNo = I.SUPartnerNo
                                LEFT JOIN dbo.INV_SAP s WITH (NOLOCK) 
                                ON s.InvoiceNo = I.InvoiceNo
                                AND s.SupplierNo = I.SUPartnerNo
                                WHERE
                                (I.SUPartnerNo =@SUPartnerNo) OR  (I.InvoiceNo = @InvoiceNo) 
                                and I.[StartTime] between  @bdate and @edate");
        SqlConnection myConnection;

        myConnection = new SqlConnection(ConnectionString);

        SqlCommand myCommand = new SqlCommand(mySelectQuery.ToString());

        myCommand.Parameters.AddWithValue("@SUPartnerNo", SUPartnerNo);
        myCommand.Parameters.AddWithValue("@InvoiceNo", invoiceNo);
        myCommand.Parameters.AddWithValue("@bdate", bdate);
        myCommand.Parameters.AddWithValue("@edate", edate);
        myCommand.Connection = myConnection;
        myConnection.Open();

        SqlDataAdapter mytable = new SqlDataAdapter(mySelectQuery.ToString(), myConnection);
        myCommand.ExecuteNonQuery();
        mytable.Fill(dt);


        myConnection.Close();
0

1 Answer 1

6

You are adding the parameter to the wrong command. The myCommand object you create is not used by the Fill method. Add the parameter to the SqlDataAdapter instead:

mytable.SelectCommand.Parameters.AddWithValue(...)

You can omit anything related to myCommand from your code, including the ExecuteNonQuery() call.

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

1 Comment

Or perhaps mytable.SelectCommand = myCommand; (and don't specify the Connection property of myCommand).

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.