开发者

SQL Error Must declare the scalar variable

        private void FillInvoiceList()
    {
        DataTable distinctInvoice = new DataTable();
        using (SqlConnection conn = new SqlConnection(C开发者_如何学JAVAonfigurationManager.ConnectionStrings["APOS_CONNECTION_STRING"].ConnectionString))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("Select distinct svc_tag from data where rep_name = @value");
            cmd.Parameters.AddWithValue("@value", this.DropDownList1.SelectedItem.Text);
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd.CommandText, conn.ConnectionString);
            sqlDataAdapter.Fill(distinctInvoice);
        }
        foreach (DataRow row in distinctInvoice.Rows)
        {
            this.ListBox1.Items.Add(row["svc_tag_dim_invoice_num"].ToString());
        }
    }

I have this code and I get this error when I call the Fill(DistinctInvoice)

Must declare the scalar variable "@value"

My FillInvoiceList() Method is being called from a SelectedIndexChanged event from the DropDownList1. The value of DropDownList1.SelectedItem.Text seems to be correct.

Thanks for any help.


The error is here :

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd.CommandText, conn.ConnectionString);

You're setting the SQLDataAdapter to use the original CommandText, not the SQLCommand itself. Change it to :

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd, conn.ConnectionString);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜