Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing code for deleting data from the database.

In the below code I am getting an exception error that no primary key is set in the database. I set the cell as a primary key to again check that but same exception appears again.

I am not sure how to do this. This code is wrong. I set the code but the code is not getting data from the cell textbox in the form. I don't know how to do this.. How it can edit it? Please help me to correct his code.

C#
try
            {
                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);

                DataTable pTable = oDS.Tables["Table"];
                pTable.TableName = "info";

                // deleting data ( main code goes from here)
                
                
                DataRow oOrderRow = oDS.Tables["info"].Rows.Find("cell"); 
                oOrderRow.Delete();
               
                oOrdersDataAdapter.Update(oDS, "info");
                MessageBox.Show("deleted");
            }
            catch (Exception ex)
            { MessageBox.Show("some prob" + ex); }


Edit: Added pre tags.
Posted
Updated 30-Apr-11 22:30pm
v3

SqlDataAdapter da;
SqlCommandBuilder cmd;
DataSet ds;
private void Form1_Load(object sender, EventArgs e)
{
    da = new SqlDataAdapter("select * from emp","Connection string");
    cmd = new SqlCommandBuilder(da);
    ds = new DataSet();
    da.Fill(ds,"myemp");
    dataGridView1.DataSource = ds.Tables[0];
}

private void delete_Click(object sender, EventArgs e)
{
    int position = 0;
    foreach (DataRow item in ds.Tables["myemp"].Rows)
    {
        if(item["id"].ToString() == textBox1.Text)
                    break;
                position++;
    }
    ds.Tables["myemp"].Rows[position].Delete();
    da.Update(ds.Tables["myemp"]);
    MessageBox.Show("successful");
}
 
Share this answer
 
Comments
codegeekalpha 1-May-11 6:26am    
thx man... it works.. thankyou very much for help
Rahul_Patel 1-May-11 6:55am    
U r welcome.......
Set the following for the adapter:

oOrdersDataAdapter.MissingSchemaAction = System.Data.MissingSchemaAction.AddWithKey;


Hope this helps.
 
Share this answer
 
v2
Comments
codegeekalpha 1-May-11 4:54am    
no helping..
dan!sh 1-May-11 5:00am    
Do you have a primary key set 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