المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : problem in DELETING rows from gridview and database table using a check box column and a DELETE button.



C# Programming
07-24-2009, 09:00 PM
First of all when I select multiple rows to delete then it gives me an error:
"Uncomitted new rows cannot be deleted."

Secondly, if I select only one row at a time and click the delete button then it deletes all the rows from the database table.


[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using Dotnet67.Sales.DAL;
using Dotnet67.Sales.Items;
using Dotnet67.Sales.Persons;
using System.Data.SqlClient;

namespace Dotnet67.Sales.DAL
{
public class DALHelper
{
private readonly string CONSTRING3 = "CASHIER1";
private readonly string CONSTRING5 = "DELETE1";

// THE FOLLOWING IS THE CODE WHERE I GET ERRORS
public void DeleteUser(int id)
{
SqlTransaction tran = con.BeginTransaction();
SqlConnection con = new SqlConnection("server=.; Database=Dotnet67;uid=sa;pwd=123;");
SqlCommand com = new SqlCommand(CONSTRING5, con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@CashierID", id);

con.Open();
com.ExecuteNonQuery();
con.Close();

}

public void InsertIntoCashier(string[] str,int[] val)
{
SqlConnection con = new SqlConnection("server=.; Database=Dotnet67;uid=sa;pwd=123;");
SqlCommand com = new SqlCommand(this.CONSTRING3, con);
com.CommandType = CommandType.StoredProcedure;

com.Parameters.AddWithValue("@CashierID", val[0]);
com.Parameters.AddWithValue("@CashierName", str[0]);
con.Open();
try
{
com.ExecuteNonQuery();
}
finally
{
con.Close();
}
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dotnet67.Sales.DAL;
using Dotnet67.CommonTypes;
using Dotnet67.WinControls;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Dotnet67.Sales.WinUI
{
public partial class ManageCashier : Form
{
public ManageCashier()
{
InitializeComponent();
}

private readonly string CONSTRING5 = "DELETE1";

private void ManageCashier_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'dotnet67DataSet8.Cashier_2' table. You can move, or remove it, as needed.
this.cashier_2TableAdapter.Fill(this.dotnet67DataSet8.Cashier_2);

}

//This is the button for inserting values into grid view which is running perfect.
private void btnDB_Click(object sender, EventArgs e)
{
DALHelper dH = new DALHelper();

string[] str = new string[3];
int[] values = new int[3];
str[0] = txtName.Text;

dH.InsertIntoCashier(str, values);

dataGridView1.*******();
dataGridView1.*******Edit();
this.cashier_2TableAdapter.Fill(this.dotnet67DataSet8.Cashier_2);

}

// THIS IS THE DELETE BUTTON ON MY FORM WHERE I AM GETTING PROBLEM
private void btnDELETE_Click(object sender, EventArgs e)
{
DALHelper dH = new DALHelper();
List<int> rowsToDelete = new List<int>();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (Convert.ToBoolean(dataGridView1["column1", i].Value))
{
rowsToDelete.Add(i);
}
foreach (int rowIndex in rowsToDelete)
{
dataGridView1.Rows.RemoveAt(rowIndex);
}

dH.DeleteUser(i);
}
}

}

}





[\code]