Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,
I am stuck at this codde. I am trying it from two weeks. Please read that whole code and tell me the problem. The error is, that it's creating exception at oOrdersDataAdapter.Update(oDS, "info"); (unable to fine datatable "info").
I am making insert button with 3 tier approach with connectionless approach. It is my last hope now.

In Database layer the Code is:
public class stddbl
    {
        OleDbConnection MAconn;

    
        public static string connectionString= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=d:\\db.mdb";
        
public DataSet dal_search(stdprops p)
        {
            
                MAconn = new OleDbConnection();
                MAconn.ConnectionString = connectionString;
                MAconn.Open();
                DataSet oDS = new DataSet();
                string query = "SELECT * FROM info";

                OleDbDataAdapter oOrdersDataAdapter = new OleDbDataAdapter(query, connectionString);

                OleDbCommandBuilder oOrdersCmdBuilder = new OleDbCommandBuilder(oOrdersDataAdapter);

                oOrdersDataAdapter.Fill(oDS);

                oOrdersDataAdapter.Update(oDS, "info"); //there is error no  datatable info
                return oDS;
                
                  
        }


In Business Layer:
public class stdbo
{
    stddbl d = new stddbl();

    public DataSet bll_search(stdprops p)
    {
        //OleDbDataAdapter da = new OleDbDataAdapter();
        DataSet da = new DataSet();
        da = d.dal_search(p);
        return da;

    }



in Presentation Layer:
private void button1_Click(object sender, EventArgs e)
       {
           p.Name = textBox1.Text;
           p.Cell = textBox2.Text;

           DataSet oDS = obj.bll_search(p);
               //OleDbDataAdapter oDS = obj.bll_search();
               DataTable pTable = oDS.Tables["Table"];
               pTable.TableName = "info";

               // Insert the Data
               DataRow oOrderRow = oDS.Tables["info"].NewRow();
               oOrderRow["name"] = textBox1.Text;
               oOrderRow["cell"] = textBox2.Text;
               oDS.Tables["info"].Rows.Add(oOrderRow);
               //oDS.Tables["Orders"].Rows.Add(oOrderRow);
               //oOrdersDataAdapter.Update(oDS, "info");
               MessageBox.Show("inserted");


       }



I have the properties class as well:-
public class stdprops
    {


        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string cell;

        public string Cell
        {
            get { return cell; }
            set { cell = value; }
        }
    }
}
Posted
Updated 20-May-11 5:38am
v2
Comments
HimanshuJoshi 20-May-11 11:38am    
Edited to add pre blocks around code.

1 solution

I'm not sure why you're doing an update call in the dal_search method, at that point you're just populating data and returning a dataset....it should probably read...

C#
public DataSet dal_search(stdprops p)
{    
    DataSet searchResults = new DataSet();
    string query = "SELECT * FROM info"; // Build up you SQL params? We're not using the stdprops object at all
    
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        connection.Open();               
        using (OleDbCommand command  = New OleDbCommand(query, connection))
        {
            using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
            {
                adapter.Fill(searchResults);                
                return searchResults;
            }            
        } 
    }                        
}


You only need to use the command builder object if you were going to modify the data and commit the results within the same procedure, have a look here

http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.update.aspx[^]

If you're just returning data from the method, you don't need this
 
Share this answer
 
v3
Comments
codegeekalpha 20-May-11 12:04pm    
i am completly not able to get u
Dylan Morley 20-May-11 12:07pm    
You don't need the line

oOrdersDataAdapter.Update(oDS, "info"); //there is error no datatable info

Remove this line

You are not updating data, you are reading it

Also, your original code has many things wrong with it (you open a connection, then don't use it?! You leave the connection ope)
codegeekalpha 20-May-11 12:16pm    
IF I REMOVE THIS LINE THERE IS SOME LOGICAL ERROR THAT AND DATA IS NOT INSERTED IN DATABASE
Dylan Morley 20-May-11 12:19pm    
There is no data being inserted in the dal_search routine! It's doing a read operation, that's all

You need to create a seperate method to do the data update, e.g

public void UpdateData(DataSet data)
{
// do the data update
}
codegeekalpha 20-May-11 12:26pm    
i am simplye inserting data in the database.

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