Question

i am beginner in c# and using three layer programming. i can not update or delete records via data grid view. following is my codes. please help me.

DAl

 namespace DAL
    {
        public class TblkalaDal
        {
            SqlConnection cn=new SqlConnection("Data Source=(local); Initial Catalog=store;Integrated Security=True");
            public DataTable Getdata()
            {
                try
                {
                    DataSet ds=new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter("Sptblkala_getdata",cn);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.Fill(ds, "Sptblkala_getdata");
                    return ds.Tables["Sptblkala_getdata"];
                }
                catch (Exception)
                {
                    return null;
                    throw;
                }
            }
    public string createkala(int kala_id,string kala_name,int kala_qty,int kala_orderpoint)
    {
        try
        {
            SqlCommand cmd = new SqlCommand("Sptblkala_insert", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@kala_id", kala_id);
            cmd.Parameters.AddWithValue("@kala_name", kala_name);
            cmd.Parameters.AddWithValue("@kala_qty", kala_qty);
            cmd.Parameters.AddWithValue("@kala_orderpoint", kala_orderpoint);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            return "true";
        }
        catch (Exception ex)
        {
            return ex.Message;
            throw;
        }

    }
            public string Deletekala(int kala_id)
            {
                try
                {
                    SqlCommand dm = new SqlCommand("Sptblkala_delete", cn);
                    dm.CommandType = CommandType.StoredProcedure;
                    dm.Parameters.AddWithValue("@kala_id", kala_id);
                    cn.Open();
                    dm.ExecuteNonQuery();
                    cn.Close();
                    return "true";
                }
                catch (Exception ex)
                {
                 return   ex.Message;
                    throw;
                }

            }

            public string updatekala(int kala_id,string kala_name,int kala_qty,int kala_orderpoint)
            {
                try
                {
                    SqlCommand uk = new SqlCommand("Sptblkala_update", cn);
                    uk.CommandType = CommandType.StoredProcedure;
                    uk.Parameters.AddWithValue("@kala_id", kala_id);
                    uk.Parameters.AddWithValue("@kala_name", kala_name);
                    uk.Parameters.AddWithValue("@kala_qty", kala_qty);
                    uk.Parameters.AddWithValue("@kala_orderpoint", kala_orderpoint);
                    cn.Open();
                    uk.ExecuteNonQuery();
                    cn.Close();
                    return "true";
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
    }

BLL

namespace BLL
{
    public class bllkala
    {
        TblkalaDal tk=new TblkalaDal();
        public string createkala(int kala_id,string kala_name,int kala_qty,int kala_orderpoint)
        {
            return tk.createkala(kala_id, kala_name, kala_qty, kala_orderpoint);
        }
        public string deletekala(int kala_id)
        {
            return tk.Deletekala(kala_id);
        }
        public string updatekala(int kala_id,string kala_name,int kala_qty,int kala_orderpoint)
        {
            return tk.updatekala(kala_id, kala_name, kala_qty, kala_orderpoint);
        }
        public DataTable Getdata()
        {
          return  tk.Getdata();
        }
    }
}

presentation

namespace store
{
    public partial class kala : Form
    {
        public kala()
        {
            InitializeComponent();
        }

       bllkala bk=new bllkala();
        DataTable dt=new DataTable();

        private void btnkalainsert_Click(object sender, EventArgs e)
        {
            string check = bk.createkala(int.Parse(txtkalacode.Text), txtkalaname.Text, int.Parse(txtkqty.Text),
                                         int.Parse(txtkalapoint.Text));
            if (check=="true")
            {
                dt = bk.Getdata();
                dataGridViewkala.DataSource = dt;
            }
            else
            {
                MessageBox.Show(check);
            }
        }

        private void kala_Load(object sender, EventArgs e)
        {
            dt = bk.Getdata();
            dataGridViewkala.DataSource = dt;
        }

        private void btnkaladel_Click(object sender, EventArgs e)
        {
            dataGridViewkala.Rows.RemoveAt(dataGridViewkala.CurrentRow.Index);
            dt = bk.deletekala(int.Parse(txtkalacode.Text));
            dataGridViewkala.DataSource = dt;
        }
    }
}
Was it helpful?

Solution 2

i found the codes in presentation layer:

private void btnkaladel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure to delete?", "Deleting...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {

                dataGridViewkala.Rows.RemoveAt(dataGridViewkala.CurrentRow.Index);
                bk.deletekala(int.Parse(txtkalacode.Text));
                dataGridViewkala.DataSource = dt;
                txtkalacode.Text = null;
                txtkalaname.Text = null;
                txtkalapoint.Text = null;
                txtkqty.Text = null;
            }
        }



        private void dataGridViewkala_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtkalacode.Text = dataGridViewkala.Rows[e.RowIndex].Cells[0].Value.ToString();
            txtkalaname.Text = dataGridViewkala.Rows[e.RowIndex].Cells[1].Value.ToString();
            txtkqty.Text = dataGridViewkala.Rows[e.RowIndex].Cells[2].Value.ToString();
            txtkalapoint.Text = dataGridViewkala.Rows[e.RowIndex].Cells[3].Value.ToString();
        }

        private void btnedit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure to edit?", "editing...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                bk.updatekala(int.Parse(txtkalacode.Text), txtkalaname.Text, int.Parse(txtkqty.Text),
                              int.Parse(txtkalapoint.Text));
                dt = bk.Getdata();
                dataGridViewkala.DataSource = dt;
                txtkalacode.Text = null;
                txtkalaname.Text = null;
                txtkalapoint.Text = null;
                txtkqty.Text = null;
            }
        }


    }

OTHER TIPS

This syntax usually works: http://www.w3schools.com/sql/sql_update.asp Basically build an sql query and have the database execute it. The benefit of this method is that you can test the queries in your database management program first.

class Database
{
    DataGridView grid = new DataGridView();
    System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection();

    public Database()
    {
        grid.CellEndEdit += new DataGridViewCellEventHandler(grid_CellEndEdit);
        grid.UserDeletedRow += new DataGridViewRowEventHandler(grid_UserDeletedRow);
    }

    void grid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        string query = string.Format(
            "UPDATE Sptblkala {0}='{1}' WHERE kala_id={2}",
            grid.Columns[e.ColumnIndex].Name, grid[e.ColumnIndex, e.RowIndex].Value,
            grid[0, e.RowIndex].Value);

        try
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query,cn);
            cmd.CommandType = System.Data.CommandType.Text;
            cn.Open();
            cmd.ExecuteScalar();
            cn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.StackTrace);
        }
    }

    void grid_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
    {
        string query = "DELETE FROM Sptblkala WHERE kala_id=" + e.Row.Cells[0].Value;

        try
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query, cn);
            cmd.CommandType = System.Data.CommandType.Text;
            cn.Open();
            cmd.ExecuteScalar();
            cn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.StackTrace);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top