sqlce データベースを更新するアプリケーションを作成しました。問題は、何かを削除しようとすると、「InvalidArgument='1' の値は 'index' に対して有効ではありません」と表示されることです。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlServerCe;
namespace Database_Application
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public SqlCeConnection conn = new SqlCeConnection(@"Data Source=C:\automail.sdf");
////////////////////////////////////Methodes////////////////////////////////////
private void Populate()
{
SqlCeCommand cm = new SqlCeCommand("SELECT * FROM Emails ORDER BY principalID", conn);
listView1.Items.Clear();
try
{
SqlCeDataReader dr = cm.ExecuteReader();
while (dr.Read())
{
ListViewItem it = new ListViewItem(dr["principalID"].ToString());
it.SubItems.Add(dr["query"].ToString());
it.SubItems.Add(dr["email"].ToString());
it.SubItems.Add(dr["subject"].ToString());
listView1.Items.Add(it);
}
dr.Close();
dr.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Application.ExitThread();
}
}
////////////////////////////////////Methodes////////////////////////////////////
// Insert button (for data insert)
private void button1_Click(object sender, EventArgs e)
{
if ( txtEmail.Text == "" || txtQuery.Text == "")
{
MessageBox.Show("Fill in all the information first!");
}
else
{
SqlCeCommand cmd = new SqlCeCommand("INSERT INTO Emails(principalID, email, query, subject) VALUES(@principalID, @email, @query, @subject)", conn);
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@principalID", txtPrincipal.Text);
cmd.Parameters.AddWithValue("@email", txtEmail.Text);
cmd.Parameters.AddWithValue("@query", txtQuery.Text);
cmd.Parameters.AddWithValue("@subject", txtSubject.Text);
try
{
int affectedrows = cmd.ExecuteNonQuery();
if (affectedrows > 0)
{
Populate();
MessageBox.Show("Email added successfully!");
txtEmail.Clear();
txtPrincipal.Clear();
txtQuery.Clear();
txtSearch.Clear();
txtSubject.Clear();
}
else
{
MessageBox.Show("Failed to add email!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
//if form shown fill the listview with the new information of the database
private void Form1_Shown(object sender, EventArgs e)
{
try
{
conn.Open();
Populate();
}
catch (SqlCeException ex)
{
MessageBox.Show(ex.Message);
Application.ExitThread();
}
}
//open form 2 and hide this
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
Form2 form2 = new Form2();
form2.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//if listview selected enable button, else disable
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
//enable delete button
button3.Enabled = true;
ListViewItem itm = listView1.SelectedItems[0];
string principalid = itm.SubItems[0].Text;
string query = itm.SubItems[1].Text;
string email = itm.SubItems[2].Text;
string subject = itm.SubItems[3].Text;
txtPrincipal.Text = principalid.ToString();
txtEmail.Text = email.ToString();
txtQuery.Text = query.ToString();
txtSubject.Text = subject.ToString();
}
else
{
button3.Enabled = false;
}
}
private void button3_Click(object sender, EventArgs e)
{
}
//delete record button
private void button3_Click_1(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count <= 0)
{
MessageBox.Show("Select a record!");
}
else
{
ListView.SelectedListViewItemCollection items = this.listView1.SelectedItems;
//for (int i = 0; i < listView1.SelectedItems.Count; i++)
foreach(ListViewItem item in items)
{
SqlCeCommand cm = new SqlCeCommand("DELETE * FROM Emails WHERE query ='" + listView1.SelectedItems[1].Text + "'", conn);
try
{
cm.ExecuteNonQuery();
Populate();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
// if button clicked, transfer information out of the records selected to form2
private void button2_Click_1(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure you want to close this application?\nThis will terminate all systems which are active at the moment","Close", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
}
if (result == DialogResult.No)
{
e.Cancel = true;
}
}
private void button4_Click(object sender, EventArgs e)
{
//try
//{
// SqlCeCommand cm = new SqlCeCommand("SELECT * FROM Emails where principalID like " + txtSearch.Text, conn);
//}
//catch (Exception ex)
//
// MessageBox.Show(ex.Message);
//}
}
private void txtSearch_TextChanged(object sender, EventArgs e)
{
}
}
}
エラーがスローされます
SqlCeCommand cm = new SqlCeCommand("DELETE * FROM Emails WHERE query ='" + listView1.SelectedItems[1].Text + "'", conn);
誰か助けてくれませんか?私はすでにパラメーターを試しましたが、まだうまくいきません...ツールを使用してCSVファイルをデータベースにインポートする前はうまくいきましたが、データベース構造は同じであるため、選択したすべてを削除できるはずです.