【问题标题】:the parameterized query expects the parameter which was not supplied c#参数化查询需要未提供的参数 c#
【发布时间】:2014-01-17 00:51:21
【问题描述】:

我正在尝试为虚构的音乐商店构建一个应用程序,但我一直在 @Voornaamklant@Achternaamklant 上收到此应用程序:

参数化查询需要未提供的参数

我正在从我的dataGridView2 中检索数据,它从另一个dataGridView 和一些TextBoxes 获取其值。那么我在这里要做的是将dataGridView2中的所有内容存储到我的数据库表'Factuur'中。

我的dataGridView2 中有 9 行,并且所有 9 行都正确地通过了我的数据库。但是,我的表中有 10 行,因为我的第一列是 ID Auto_Incremented 字段,也许是这样?不过,我真的不认为这是造成它的原因。

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NieuwefactuurV2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.dateTimePicker1.Value = DateTime.Now;
        }

        private void afsluitenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewRow dr = dataGridView1.SelectedRows[0];
            Titel.Text = dr.Cells["titelDataGridViewTextBoxColumn"].Value.ToString();
            Prijs.Text = dr.Cells["prijsDataGridViewTextBoxColumn"].Value.ToString();
        }

        private void Aantal_TextChanged(object sender, EventArgs e)
        {
            try
            {
                double a = Convert.ToDouble(Aantal.Text);
                double b = Convert.ToDouble(Prijs.Text);

                Subtotaal.Text = (a * b).ToString();
            }

            catch
            {
            }
        }

        private void Prijs_TextChanged(object sender, EventArgs e)
        {
            try
            {
                double a = Convert.ToDouble(Aantal.Text);
                double b = Convert.ToDouble(Prijs.Text);
                double c = Convert.ToDouble(Subtotaal.Text);
                double d = Convert.ToDouble(Korting.Text);

                Subtotaal.Text = (a * b).ToString();
            }

            catch
            {
            }
        }

        private void Subtotaal_TextChanged(object sender, EventArgs e)
        {
            try
            {
                double a = Convert.ToDouble(Aantal.Text);
                double b = Convert.ToDouble(Prijs.Text);
                double c = Convert.ToDouble(Subtotaal.Text);
                double d = Convert.ToDouble(Korting.Text);
                if (d >= 1 && d < 100)
                {
                    Totaal.Text = (c - (c / 100 * d)).ToString();
                }
                if (d <= 0)
                {
                    Totaal.Text = (c).ToString();
                }
                if (d >= 100)
                {
                    Totaal.Text = (0).ToString();
                }
            }

            catch
            {
            }
        }

        private void Korting_TextChanged(object sender, EventArgs e)
        {
            try
            {
                double a = Convert.ToDouble(Aantal.Text);
                double b = Convert.ToDouble(Prijs.Text);
                double c = Convert.ToDouble(Subtotaal.Text);
                double d = Convert.ToDouble(Korting.Text);

                if (d >= 1 && d < 100)
                {
                    Totaal.Text = (c - (c / 100 * d)).ToString();
                }
                if (d <= 0)
                {
                    Totaal.Text = (c).ToString();
                }
                if (d >= 100)
                {
                    Totaal.Text = (0).ToString();
                }
            }

            catch
            {
            }
        }

        private void Add_Click(object sender, EventArgs e)
        {
            try
            {
                string str;
                string titel = Titel.Text;
                string voorupdate;
                str = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[4].Value.ToString();
                int a = Convert.ToInt32(str);
                int b = Convert.ToInt32(Aantal.Text);
                voorupdate = (a - b).ToString();
                if (a - b >= 0)
                {
                    Print.Enabled = true;
                    string vnaam = Voornaam.Text;
                    string anaam = Achternaam.Text;
                    string lpcd = LPCD.Text;
                    string dateColumn = dateTimePicker1.Text;
                    string firstColumn = Titel.Text;
                    string secondColumn = Aantal.Text;
                    string thirdColumn = Prijs.Text;
                    string fourthColumn = Subtotaal.Text;
                    string fifthColumn = Korting.Text;
                    string sixthColumn = Totaal.Text;
                    string[] row = { vnaam, anaam, firstColumn, secondColumn, thirdColumn, fourthColumn, fifthColumn, sixthColumn, lpcd, dateColumn };
                    dataGridView2.Rows.Add(row);



                    string cmd = "Update Album set [Actuele Voorraad]='" + voorupdate + "' where Titel='" + Titel.Text + "'";
                    using (SqlConnection connection = new SqlConnection("Data Source=Evan-PC;Initial Catalog=DatabaseProject;Integrated Security=True"))
                    {

                        using (SqlCommand command1 = new SqlCommand(cmd, connection))
                        {
                            connection.Open();
                            command1.ExecuteNonQuery();
                        }
                    }
                }
                else
                {
                    Print.Enabled = false;
                    MessageBox.Show("Fout");
                }
            }
            catch
            {
                MessageBox.Show("Vul waarden in");
            }
        }

        private void Remove_Click(object sender, EventArgs e)
        {
            string str;
            string str2;
            string titel;
            string voorupdate;

            titel = dataGridView2.Rows[dataGridView2.SelectedRows[0].Index].Cells[2].Value.ToString();
            str2 = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[5].Value.ToString();
            str = dataGridView2.Rows[dataGridView2.SelectedRows[0].Index].Cells[3].Value.ToString();

            int a = Convert.ToInt32(str2);
            int b = Convert.ToInt32(str);
            voorupdate = ((a + b) - b).ToString();


            string cmd = "Update Album set [Actuele Voorraad]='" + voorupdate + "' where Titel='" + titel + "'";
            using (SqlConnection connection = new SqlConnection("Data Source=Evan-PC;Initial Catalog=DatabaseProject;Integrated Security=True"))
            {

                using (SqlCommand command1 = new SqlCommand(cmd, connection))
                {
                    connection.Open();
                    command1.ExecuteNonQuery();
                }
            }



            if (this.dataGridView2.SelectedRows.Count > 0)
            {
                dataGridView2.Rows.RemoveAt(this.dataGridView2.SelectedRows[0].Index);
            }
            else
            {
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'databaseProjectDataSet.Album' table. You can move, or remove it, as needed.
            this.albumTableAdapter1.Fill(this.databaseProjectDataSet.Album);
            // TODO: This line of code loads data into the 'database_ProjectDataSet2.Album' table. You can move, or remove it, as needed.
            this.albumTableAdapter.Fill(this.database_ProjectDataSet2.Album);
        }

        private void Print_Click(object sender, EventArgs e)
        {
            try
            {
                string mail = Email.Text;
                string voor = Voornaam.Text;
                string achter = Achternaam.Text;
                string adres = Adres.Text;
                string woon = Woonplaats.Text;
                string post = Postcode.Text;
                string tele = Telefoonnummer.Text;
                try
                {
                    string addcity = "If Not Exists(select * from Woonplaats where Woonplaats='@woon') Begin insert into Woonplaats (Woonplaats)" + "Values('" + woon + "')END";

                    string dataquery = "INSERT INTO Klant([E-mail], Voornaam, Achternaam, Adres, Woonplaats, Postcode, Telefoonnummer) " +
                                       "Values('" + mail + "', '" + voor + "', '" + achter + "', '" + adres + "', '" + woon + "', '" + post + "', '" + tele + "')";

                    using (SqlConnection connection = new SqlConnection("Data Source=Evan-PC;Initial Catalog=DatabaseProject;Integrated Security=True"))
                    {

                        using (SqlCommand command1 = new SqlCommand(addcity, connection))
                        using (SqlCommand command = new SqlCommand(dataquery, connection))
                        {
                            connection.Open();
                            command1.Parameters.AddWithValue("@woon", woon);
                            command1.ExecuteNonQuery();
                            command.ExecuteNonQuery();
                            command.Parameters.Clear();
                            command1.Parameters.Clear();
                        }
                    }
                }
                catch
                {
                    string dataquery = "INSERT INTO Klant([E-mail], Voornaam, Achternaam, Adres, Woonplaats, Postcode, Telefoonnummer) " +
                                       "Values('" + mail + "', '" + voor + "', '" + achter + "', '" + adres + "', '" + woon + "', '" + post + "', '" + tele + "')";

                    using (SqlConnection connection = new SqlConnection("Data Source=Evan-PC;Initial Catalog=DatabaseProject;Integrated Security=True"))
                    {

                        using (SqlCommand command = new SqlCommand(dataquery, connection))
                        {
                            connection.Open();
                            command.ExecuteNonQuery();
                            command.Parameters.Clear();
                        }
                    }

                }
            }
            catch
            {
                MessageBox.Show("Klantgegevens niet goed ingevuld");
            }

            try
            {
                for (int i = 0; i < dataGridView2.Rows.Count; i++)
                {
                    string dataquery = "INSERT INTO Factuur VALUES(@Voornaamklant, @Achternaamklant, @Albumtitel, @Aantal, @Prijs, @Subtotaal, @Korting, @Totaal, @CDofLP, @Datuminvoeren)";

                    using (SqlConnection connection = new SqlConnection("Data Source=Evan-PC;Initial Catalog=DatabaseProject;Integrated Security=True"))
                    {

                        using (SqlCommand command2 = new SqlCommand(dataquery, connection))
                        {
                            connection.Open();
                            command2.Parameters.AddWithValue("@Voornaamklant", dataGridView2.Rows[i].Cells["vnaam"].Value);
                            command2.Parameters.AddWithValue("@Achternaamklant", dataGridView2.Rows[i].Cells["anaam"].Value);
                            command2.Parameters.AddWithValue("@Albumtitel", dataGridView2.Rows[i].Cells["Albumtitels"].Value);
                            command2.Parameters.AddWithValue("@Aantal", dataGridView2.Rows[i].Cells["Aantal2"].Value);
                            command2.Parameters.AddWithValue("@Prijs", dataGridView2.Rows[i].Cells["Prijs2"].Value);
                            command2.Parameters.AddWithValue("@Subtotaal", dataGridView2.Rows[i].Cells["Subtotaal2"].Value);
                            command2.Parameters.AddWithValue("@Korting", dataGridView2.Rows[i].Cells["Korting2"].Value);
                            command2.Parameters.AddWithValue("@Totaal", Convert.ToDecimal((dataGridView2.Rows[i].Cells["Totaal2"].Value)));
                            command2.Parameters.AddWithValue("@CDofLP", dataGridView2.Rows[i].Cells["CDLP"].Value);
                            command2.Parameters.AddWithValue("@Datuminvoeren", Convert.ToDateTime(dateTimePicker1.Text));
                            command2.ExecuteNonQuery();
                            command2.Parameters.Clear();
                        }
                    }
                }

            }

            catch
            {
                MessageBox.Show("Kan de factuur niet in de database zetten", "Fout", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

【问题讨论】:

  • 通过在“catch”上显示该消息之前添加一个断点,我可以看到我的应用程序执行的所有步骤。在我的代码底部添加了完整的错误消息。
  • 如果您需要,我可以发布我的整个应用程序代码。我没有看到我的代码是如何运行两次的。
  • 尝试将command.Parameters.Clear(); 放在命令using 块的末尾。之后command.ExecuteNonQuery
  • 我试过你的命令,法比奥,它仍然给出同样的错误信息。谢谢你无论如何尝试格兰特,我真的不明白是什么原因造成的......
  • 在您的datagridview2 中是否有最后一个空行新行

标签: c# mysql winforms datagridview parameters


【解决方案1】:

如果参数的值为null,你会得到这个错误

所以在传递给.Parameters之前检查值

if(dataGridView2.Rows[i].Cells["vnaam"].Value == null)
    command2.Parameters.AddWithValue("@Voornaamklant", DBNull.Value);
else
    command2.Parameters.AddWithValue("@Voornaamklant", dataGridView2.Rows[i].Cells["vnaam"].Value);

【讨论】:

  • 我已经通过代码中的断点检查了值,这些值都是正常的,并且数据确实被插入了。但是在插入之后,它会遇到mbox 因为错误。另外,我已经尝试了 if = NULL 代码,如果找到了 NULL 也会使用消息框,但没有成功。
  • 谢谢法比奥!很抱歉,这并不能解决 OP 的问题,但在找到您的答案之前,我已经为此苦苦挣扎了两天。是的,参数的值必须是 DBNull.Value 而不是 null
【解决方案2】:

您的代码对我来说看起来不错。我会尝试在 for 循环中放置一个断点并对其进行调试。我怀疑您从 dataGridView2.Rows.Count 返回的行比您的数据多一行。这可以解释为什么您看到插入到 Factuur 中的值是您想要查看的,但您仍然收到该错误消息。

我会在以下代码行中设置一个断点,这样当您遇到异常时,您可以找出变量 i 的值。

string dataquery = "INSERT INTO Factuur VALUES(@Voornaamklant, @Achternaamklant, @Albumtitel, @Aantal, @Prijs, @Subtotaal, @Korting, @Totaal, @CDofLP, @Datuminvoeren)";

MessageBox.Show("Kan de factuur niet in de database zetten", "Fout", MessageBoxButtons.OK, MessageBoxIcon.Error);

还有在你循环dataGridView2.Rows的for循环结束时。

【讨论】:

  • 我按照你说的做了,但我自己真的找不到,我在申请的这一部分上已经苦苦挣扎了 9 个小时,我真的找不到。
  • 好吧,我做到了,返回的值看起来都很正常,没有任何东西被放在那里两次,而且那里的值都被正常放入数据库中。错误仍然存​​在现在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
  • 2016-04-16
相关资源
最近更新 更多