我正在尝试为虚构的音乐商店构建一个应用程序,但我一直在@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);
}
}
}
}
答案 0 :(得分: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);
答案 1 :(得分:0)
您的代码对我来说很好。我会尝试在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循环结束。