Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
im doing a project on insert update and delete using three tier architecture...

coming to insert it was working fine and coming to update and delete... im getting an error called... "cannot find column[...]"

the code for BLL library is

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Data;

namespace BLL
{
    public class LIB2
    {
        string code;
        string language;
        DataRow drl;

        public LIB2()
        { }
        public string pcode
        {
            set
            {
                code = value;
            }
            get
            {
                return code;
            }
        }
        public string planguage
        {
            set
            {
                language = value;
            }
            get
            {
                return language;
            }
        }
        LIB1 l = new LIB1();
        public void insert()
        {
            drl = l.ds.Tables[0].NewRow();
            drl[0] = code;
            drl[1] = language;
            l.ds.Tables[0].Rows.Add(drl);
        }
        public void update()
        {
            drl = l.ds.Tables[0].Select("code=" + code)[0];
            drl[0] = code;
            drl[1] = language;
        }
        public void delete()
        {
            drl = l.ds.Tables[0].Select("code=" + code)[0];
            drl.Delete();
        }
        public void bind()
        {
            l.Bind();
        }
        public DataTable gettabledata()
        {
            return l.ds.Tables[0];
        }
      

    }
}


and code for DLL library is

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class LIB1
    {
        SqlConnection con = new SqlConnection("Persist Security Info=True;User ID=*****;Password=****;Initial Catalog=chaitanya;Server=PC01");
        SqlDataAdapter da;
        public DataSet ds;
        SqlCommandBuilder bdr;

        public LIB1()
        {
            da = new SqlDataAdapter("select * from languages", con);
            ds = new DataSet();
            da.Fill(ds, "languages");
            da.FillSchema(ds, SchemaType.Source, "languages");
            bdr = new SqlCommandBuilder(da);
        }
        public void Bind()
        {
            da.Update(ds, "languages");
        }
    }

}

and code for Presentation layer is
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;

namespace ttr
{
    public partial class tta : UserControl
    {
        LIB2 l = new LIB2();
        public tta()
        {
            InitializeComponent();
        }
        public void bind()
        {
            l.bind();
        }

        private void tta_Load(object sender, EventArgs e)
        {
            grid.DataSource = l.gettabledata();
        }

        private void Create_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "")
            {
                MessageBox.Show("Please enter all the details", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox1.Focus();
                return;
            }
            else
            {
                try
                {
                    l.pcode = textBox1.Text;
                    l.planguage = textBox2.Text;
                    textBox1.Text = string.Empty;
                    textBox2.Text = string.Empty;
                    l.insert();
                    l.bind();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void Update_Click(object sender, EventArgs e)
        {
            l.pcode = textBox1.Text;
            l.planguage = textBox2.Text;
            l.update();
            l.bind();

        }

        private void Delete_Click(object sender, EventArgs e)
        {
            l.pcode = textBox1.Text;
            l.delete();
            l.bind();
        }

        private void New_Click(object sender, EventArgs e)
        {
            textBox1.Text = string.Empty;
            textBox2.Text = string.Empty;
        }

        private void grid_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                DataGridViewRow row = this.grid.Rows[e.RowIndex];
                grid.ReadOnly = true;
                textBox1.Text = row.Cells["code"].Value.ToString();
                textBox2.Text = row.Cells["language"].Value.ToString();
            }
        }
        
    }

}

these are the code snippets i wrote..... insert command is working very fine but i got head ace with the update and delete...can you guys anyone help me in this.....
Posted
Updated 10-Jul-14 0:07am
v3
Comments
Sarvesh Kumar Gupta 10-Jul-14 6:14am    
why you are using [0] in update and delete
chaitanya556 10-Jul-14 6:16am    
[0] refers to column in data table....
navya chowdary 10-Jul-14 6:19am    
you don't get this................
chaitanya556 10-Jul-14 6:19am    
excuse me...????
George Jonsson 10-Jul-14 10:34am    
The point is; What happens in your select statement if there are no found rows?

1 solution

Change to

C#
drl = l.ds.Tables[0].Select(String.Format("code = '{0}'", code)).FirstOrDefault();
if (dr1 != null)
{
  // Do your stuff
}


Note the single quotation marks around {0}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900