Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm making a 3 tier application for insert, update and delete using DataSet and DataAdapter. This is not any kind of syntax error, but there is some logical error. Please help me.

The code for database layer is:
class dbcon
    {
        public static string connectionString= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=E:\\testdb.mdb";
       
        OleDbConnection MAconn;
        OleDbCommand MAcmd;
        public DataSet insertRecord(String query)
        {
            DataSet ds = null;
            try
            {
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
                OleDbCommandBuilder cmd = new OleDbCommandBuilder(dataAdapter);
                ds = new DataSet();
                dataAdapter.Fill(ds);
                
                dataAdapter.Update(ds);
                
            }
            catch (Exception ex)
            {
                // MessageBox.Show(ex.Message);
            }
            return ds;
        }

using propertieslayer;

namespace databaselayer
{
    public class adddelBookDbl
    {
        dbcon obj=new dbcon();
        public DataSet dal_insert(adddelBookProps p)
        {
            string query = "select * from books ";
            DataSet ds = obj.insertRecord(query);

            return ds;
        }

Business layer work:
namespace businesslayer
{
    public class admenRegBll
    {
        admenRegDbl d = new admenRegDbl();
        adddelBookDbl t = new adddelBookDbl();
       
        public DataSet binsert(adddelBookProps p)
        {
           DataSet ds = t.dal_insert(p);
           return ds; 
        }

Main form Insert button coding:
protected void btnadd_Click(object sender, EventArgs e)
    {
        adddelBookProps p = new adddelBookProps();
        admenRegBll obj = new admenRegBll();
      
        DataSet ds = obj.binsert(p);
      
        DataTable dt = ds.Tables["Table"];
        dt.TableName = "books";

        // Insert the Data
        DataRow dr = ds.Tables["books"].NewRow();
        dr["bookno"] = booknotxt.Text;
        dr["isbn"] = isbntxt.Text;
        dr["booktitle"] = booktitletxt.Text;
        dr["author"] = authortxt.Text;
        dr["quantity"] = quantitytxt.Text;
        dr["price"] = pricetxt.Text;
        dr["pubyear"] = pytxt.Text;
        ds.Tables["books"].Rows.Add(dr);
    }

In properties layer:
namespace propertieslayer
{
    public class adddelBookProps
    {
        private string bookno;
        public string Bookno
        {
            get { return bookno; }
            set { bookno = value; }
        }
        private string isbn;

        public string Isbn
        {
            get { return isbn; }
            set { isbn = value; }
        }

        private string booktitle;

        public string Booktitle
        {
            get { return booktitle; }
            set { booktitle = value; }
        }

        private string author;

        public string Author
        {
            get { return author; }
            set { author = value; }
        }

        private string quantity;

        public string Quantity
        {
            get { return quantity; }
            set { quantity = value; }
        }

        private string price;

        public string Price
        {
            get { return price; }
            set { price = value; }
        }

        private string publishedyear;

        public string Published
        {
            get { return publishedyear; }
            set { publishedyear = value; }
        }
    }
Posted
Updated 15-May-11 5:28am
v2
Comments
Fabio V Silva 15-May-11 11:29am    
Edited for formatting.
You didn't say what exactly is the error you're getting either.
Abhinav S 15-May-11 11:31am    
OP says he is not getting any error. Apparently his code is not working.
Fabio V Silva 15-May-11 11:41am    
If it was working he wouldn't be here asking questions, but I still don't know what is failing...
Abhinav S 15-May-11 12:05pm    
Well good point.

1 solution

First thing you are not doing any tiers in your coding. Tiers involves for segregation of responsibilities, so that each tier perform independent of other. In other words tiers are substitutable. In you coding at button click has access to your persistence logic. I am not seeing any kind of separation.
C#
DataSet ds = obj.binsert(p); The 'p' travels down the stack but not used anywhere. 

       public DataSet dal_insert(adddelBookProps p)
        {
            string query = "select * from books ";
            DataSet ds = obj.insertRecord(query);
 
            return ds;
        }

The p coming to the above function and died.

DataSet ds = obj.binsert(p);
This down the stack calls to update the database, but after that you are altering the dataset...
C#
DataSet ds = obj.binsert(p);

DataTable dt = ds.Tables["Table"];
dt.TableName = "books";

// Insert the Data
DataRow dr = ds.Tables["books"].NewRow();
dr["bookno"] = booknotxt.Text;
dr["isbn"] = isbntxt.Text;
dr["booktitle"] = booktitletxt.Text;
dr["author"] = authortxt.Text;
dr["quantity"] = quantitytxt.Text;
dr["price"] = pricetxt.Text;
dr["pubyear"] = pytxt.Text;
ds.Tables["books"].Rows.Add(dr);

The changes are not going to be persisted.

If you are not clear about layering then can use a simple model or an readily available entity framework model/ template or the data driven template Asp.Net Dynamic Data Entities Web Applications may help. When you start a new project these templates will be available.

Determine your application is a data driven or model driven. ASP.Net has an MVC framework which works well for data driven applications.

If you want to create a tiered enterprise application these books may be helpful. Refer Martin Fowlers Pattern's of Enterprise Application Architecture. Wrox publisher's Domain Driven Design with C# and Professional Asp.Net Design Patterns.

If your application is simple one make it simple, no need confuse with tiers

Good luck
 
Share this answer
 
v2
Comments
Sandeep Mewara 15-May-11 13:06pm    
Strange you forgot to format code using PRE tags. :)
I will do, hope you don't mind.

BTW, you went on a small break? :)
Albin Abel 15-May-11 14:43pm    
Hi Sandeep. Thanks a lot for formatting. You are right , there is a little break. :)
Sandeep Mewara 15-May-11 14:45pm    
:)
Sandeep Mewara 15-May-11 13:07pm    
My 5 for the answer.
codegeekalpha 15-May-11 14:08pm    
i already made it in a simple way. which is working properly. this is my assignment.. i have to do it in three tier. when i was doint it it is in my mind that properties layer is not used anywhere.. pls any one tell me is dere any solution for this??

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



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