Witam,
to jest mój pierwszy post na forum. Chciałbym pozdrowić wszystkich tu obecnych, jednocześnie prosząc o małą pomoc. Piszę aplikacje służącą do inwentaryzacji magazynu za pomocą skanera kodów kreskowych.Korzystam z SQLCE, ponieważ plik z bazą danych musi znajdować się w folderze aplikacji. W firmie, w której ma ona funkcjonować nie ma WIFI ;) Zrobiłem bazę danych produktów, wyświetla się ona na panelu. Jednakże przy napisaniu funkcji pozwalającej na usunięcie, edycje rekordów występuję błąd :
System.ArgumentOutOfRangeException: 'Indeks był spoza zakresu. Musi mieć wartość nieujemną i mniejszą niż rozmiar kolekcji.Nazwa parametru: index'"
Tutaj kod :
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 ShoesInventoryManager
{
public partial class AddNewItemRecord : Form
{
private void LoadData()
{
SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Users\kamil\Desktop\Projekt do pracy\ShoesInventoryManager\ProductDB.sdf");
SqlCeDataAdapter sda = new SqlCeDataAdapter("Select *from [ProductDBS] ",con);
DataTable dt = new DataTable();
sda.Fill(dt);
foreach (DataRow row in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells["IDC"].Value = n + 1;
dataGridView1.Rows[n].Cells["MarkC"].Value = row["MARK"].ToString();
dataGridView1.Rows[n].Cells["ModelC"].Value = row["MODEL"].ToString();
dataGridView1.Rows[n].Cells["SerialC"].Value = row["MODEL"].ToString();
dataGridView1.Rows[n].Cells["SizeC"].Value = row["SIZE"].ToString();
dataGridView1.Rows[n].Cells["BarcodeC"].Value = row["BARCODE"].ToString();
}
}
private void ClearData()
{
txtMark.Clear();
txtModel.Clear();
txtSize.Clear();
txtBarcode.Clear();
txtSerial.Clear();
txtMark.Focus();
}
private bool Validation()
{
ErrorProvider errorProvider1 = new ErrorProvider();
bool result = false;
if (string.IsNullOrEmpty(txtMark.Text))
{
errorProvider1.Clear();
errorProvider1.SetError(txtMark, "Mark Required");
}
else if (string.IsNullOrEmpty(txtModel.Text))
{
errorProvider1.Clear();
errorProvider1.SetError(txtModel, "Model Required");
}
else if (string.IsNullOrEmpty(txtSerial.Text))
{
errorProvider1.Clear();
errorProvider1.SetError(txtSerial, "Serial Required");
}
else if (string.IsNullOrEmpty(txtSize.Text))
{
errorProvider1.Clear();
errorProvider1.SetError(txtSize, "Size Required");
}
else if (string.IsNullOrEmpty(txtBarcode.Text))
{
errorProvider1.Clear();
errorProvider1.SetError(txtBarcode, "Barcode Required");
}
else if(IfShoesExistBarcode(txtBarcode.Text) == false)
{
errorProvider1.Clear();
errorProvider1.SetError(txtBarcode, "Barcode exist in DataBase!");
}
else if (IfShoesExistSerial(txtSerial.Text) == false)
{
errorProvider1.Clear();
errorProvider1.SetError(txtSerial, "Serial exist in DataBase!");
}
else
{
errorProvider1.Clear();
result = true;
}
return result;
}
private bool IfShoesExistBarcode(string barcode)
{
SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Users\kamil\Desktop\Projekt do pracy\ShoesInventoryManager\ProductDB.sdf");
SqlCeDataAdapter sda=new SqlCeDataAdapter("Select 1 from [ProductDBS] Where [Barcode] = '" + barcode + "'",con);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
return false;
else
return true;
}
private bool IfShoesExistSerial(string serial)
{
SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Users\kamil\Desktop\Projekt do pracy\ShoesInventoryManager\ProductDB.sdf");
SqlCeDataAdapter sda = new SqlCeDataAdapter("Select 1 from ProductDBS Where Serial = '" + serial + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
return false;
else
return true;
}
public AddNewItemRecord()
{
InitializeComponent();
LoadData();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (Validation())
{
if (IfShoesExistBarcode(txtBarcode.Text) && IfShoesExistSerial(txtSerial.Text))
{
SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Users\kamil\Desktop\Projekt do pracy\ShoesInventoryManager\ProductDB.sdf");
con.Open();
SqlCeDataAdapter da = new SqlCeDataAdapter(@"INSERT INTO ProductDBS (Mark,Model,Size,Barcode,Serial) VALUES('" + txtMark.Text + "','" + txtModel.Text + "','" + txtSize.Text + "','" + txtBarcode.Text + "','"+txtSerial.Text+"')", con);
DataTable dt = new DataTable();
da.Fill(dt);
LoadData();
ClearData();
con.Close();
}
}
}
Tutaj mam błąd:
private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
txtMark.Text = dataGridView1.SelectedRows[0].Cells["MarkC"].Value.ToString();
txtModel.Text = dataGridView1.SelectedRows[0].Cells["ModelC"].Value.ToString();
txtSerial.Text = dataGridView1.SelectedRows[0].Cells["SerialC"].Value.ToString();
txtSize.Text = dataGridView1.SelectedRows[0].Cells["SizeC"].Value.ToString();
txtBarcode.Text = dataGridView1.SelectedRows[0].Cells["BarcodeC"].Value.ToString();
btnAdd.Enabled =false;
btnUpdate.Enabled = true;
btnDelete.Enabled = true;
}
}
}
Bardzo proszę o pomoc.
?