Problem in delete in access
-
i am getting the error Exception Details: System.InvalidOperationException: Fill: SelectCommand.Connection property has not been initialized. on this line OleDbCom.Fill(ds,"users"); my all code is as under. ------------------------------ <%@ Page Language="C#" Debug = "True"%> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.OleDb" %> // Insert page code here OleDbConnection OleDbCon; void page_Load(Object sender, EventArgs e) { OleDbConnection OleDbCon = new OleDbConnection ("Provider=Microsoft.Jet.OleDb.4.0; Data Source=d:\\access1.mdb"); BindGrid(); } void dg_delete(Object sender, DataGridCommandEventArgs e) { String deleteCmd = "DELETE from users where username = @uid"; OleDbCommand OleDbCom = new OleDbCommand(deleteCmd, OleDbCon); OleDbCom.Parameters.Add(new OleDbParameter("@Id", OleDbType.VarChar, 11)); OleDbCom.Parameters["@uid"].Value = dg.DataKeys[(int)e.Item.ItemIndex]; OleDbCom.Connection.Open(); try { OleDbCom.ExecuteNonQuery(); Message.Text = "Record Deleted"; } catch (OleDbException) { Message.Text = "ERROR Could not delete record"; Message.Style["color"] = "red"; } OleDbCom.Connection.Close(); BindGrid(); } public void BindGrid() { OleDbDataAdapter OleDbCom = new OleDbDataAdapter("select * from users", OleDbCon); DataSet ds = new DataSet(); OleDbCom.Fill(ds,"users"); dg.DataSource=ds.Tables["users"].DefaultView; dg.DataBind(); }
Mazhar Hussain
-
i am getting the error Exception Details: System.InvalidOperationException: Fill: SelectCommand.Connection property has not been initialized. on this line OleDbCom.Fill(ds,"users"); my all code is as under. ------------------------------ <%@ Page Language="C#" Debug = "True"%> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.OleDb" %> // Insert page code here OleDbConnection OleDbCon; void page_Load(Object sender, EventArgs e) { OleDbConnection OleDbCon = new OleDbConnection ("Provider=Microsoft.Jet.OleDb.4.0; Data Source=d:\\access1.mdb"); BindGrid(); } void dg_delete(Object sender, DataGridCommandEventArgs e) { String deleteCmd = "DELETE from users where username = @uid"; OleDbCommand OleDbCom = new OleDbCommand(deleteCmd, OleDbCon); OleDbCom.Parameters.Add(new OleDbParameter("@Id", OleDbType.VarChar, 11)); OleDbCom.Parameters["@uid"].Value = dg.DataKeys[(int)e.Item.ItemIndex]; OleDbCom.Connection.Open(); try { OleDbCom.ExecuteNonQuery(); Message.Text = "Record Deleted"; } catch (OleDbException) { Message.Text = "ERROR Could not delete record"; Message.Style["color"] = "red"; } OleDbCom.Connection.Close(); BindGrid(); } public void BindGrid() { OleDbDataAdapter OleDbCom = new OleDbDataAdapter("select * from users", OleDbCon); DataSet ds = new DataSet(); OleDbCom.Fill(ds,"users"); dg.DataSource=ds.Tables["users"].DefaultView; dg.DataBind(); }
Mazhar Hussain
Hi, you need to call the "Open()" method on the connection object OleDbConnection OleDbCon = new OleDbConnection ("Provider=Microsoft.Jet.OleDb.4.0; Data Source=d:\\access1.mdb"); OleDbCon.Open(); and then do the rest of the processing... hope that solves your problem.... regards, Aryadip. Cheers !! and have a Funky day !!
-
Hi, you need to call the "Open()" method on the connection object OleDbConnection OleDbCon = new OleDbConnection ("Provider=Microsoft.Jet.OleDb.4.0; Data Source=d:\\access1.mdb"); OleDbCon.Open(); and then do the rest of the processing... hope that solves your problem.... regards, Aryadip. Cheers !! and have a Funky day !!