当前位置:编程学习 > C#/ASP.NET >>

特急????异常详细信息: System.InvalidOperationException: ExecuteReader 要求已打开且可用的连接。连接的当前状

************** 异常文本 **************
System.InvalidOperationException: ExecuteReader 要求已打开且可用的连接。连接的当前状态为已关闭。
   在 System.Data.OleDb.OleDbConnection.CheckStateOpen(String method)
   在 System.Data.OleDb.OleDbCommand.ValidateConnection(String method)
   在 System.Data.OleDb.OleDbCommand.ValidateConnectionAndTransaction(String method)
   在 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
   在 System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
   在 System.Data.OleDb.OleDbCommand.ExecuteReader()
   在 StoreMIS.Material.btDel_Click(Object sender, EventArgs e) 位置 C:\Users\Administrator\Desktop\ch07 仓库管理信息系统\StoreMIS\StoreMIS\Material.cs:行号 195
   在 System.Windows.Forms.Control.OnClick(EventArgs e)
   在 System.Windows.Forms.Button.OnClick(EventArgs e)
   在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
   在 System.Windows.Forms.Button.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


源码为:



private void btDel_Click(object sender, System.EventArgs e)
{
if (dataGrid1.CurrentRowIndex>=0 && dataGrid1.DataSource!=null && dataGrid1[dataGrid1.CurrentCell]!=null)
{
               
string sql ="select * from ininfo where MID='"+ds.Tables["material"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim()+"'";
OleDbCommand cmd = new OleDbCommand(sql,oleConnection1);
                OleDbDataReader dr;
              this.oleConnection1 = new OleDbConnection(StoreMIS.database.dbConnection.connection);
dr = cmd.ExecuteReader();
                
if (dr.Read())
{
MessageBox.Show("删除快件'"+ds.Tables["material"].Rows[dataGrid1.CurrentCell.RowNumber][1].ToString().Trim()+"'失败,请先删除该快件入库信息!","提示");
dr.Close();
}
else
{
dr.Close();
string sql1="delete * from materialinfo where MID = '"+ds.Tables["material"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim()+"'";
cmd.CommandText = sql1;
cmd.ExecuteNonQuery();
MessageBox.Show("删除快件'"+ds.Tables["material"].Rows[dataGrid1.CurrentCell.RowNumber][1].ToString().Trim()+"'成功!","提示");
}
}
else
MessageBox.Show("没有指定快件信息!","提示");
}


--------------------编程问答--------------------   this.oleConnection1 = new OleDbConnection(StoreMIS.database.dbConnection.connection);
dr = cmd.ExecuteReader();

这里是不是要加上一句
this.oleConnection1.Open(); --------------------编程问答-------------------- using(OleDbConnection conn= new OleDbConnection(StoreMIS.database.dbConnection.connection))
{
conn.open();
using(OleDbCommand cmd = new OleDbCommand(sql,conn))
{}
}
if (Conn.State == ConnectionState.Closed)
                    {
                        Conn.Open();
                    }

--------------------编程问答--------------------  this.oleConnection1 = new OleDbConnection(StoreMIS.database.dbConnection.connection);
this.oleConnection1.Open();
dr = cmd.ExecuteReader();
--------------------编程问答-------------------- 再不行,会不会是这个顺序问题
  this.oleConnection1 = new OleDbConnection(StoreMIS.database.dbConnection.connection);

OleDbCommand cmd = new OleDbCommand(sql,this.oleConnection1);
this.oleConnection1.Open();
dr = cmd.ExecuteReader(); --------------------编程问答-------------------- 你既然写了CLOSE,前面也要OPEN --------------------编程问答-------------------- 要求已打开且可用的连接。连接的当前状态为已关闭

OleDbCommand cmd = new OleDbCommand(sql,oleConnection1);
OleDbDataReader dr;
this.oleConnection1 = new OleDbConnection(StoreMIS.database.dbConnection.connection);
dr = cmd.ExecuteReader();


肯定是执行:dr = cmd.ExecuteReader();时报错的,你没有打开连接呀!
要先open连接
this.oleConnection1 = new OleDbConnection(StoreMIS.database.dbConnection.connection);
OleDbCommand cmd = new OleDbCommand(sql,oleConnection1);
oleConnection1.Open();
OleDbDataReader dr;
dr = cmd.ExecuteReader();

--------------------编程问答-------------------- this.oleConnection1 = new OleDbConnection(StoreMIS.database.dbConnection.connection);
dr = cmd.ExecuteReader();

这里是不是要加上一句
this.oleConnection1.Open(); --------------------编程问答-------------------- private void btDel_Click(object sender, System.EventArgs e)
{
if (dataGrid1.CurrentRowIndex>=0 && dataGrid1.DataSource!=null && dataGrid1[dataGrid1.CurrentCell]!=null)
{
   
string sql ="select * from ininfo where MID='"+ds.Tables["material"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim()+"'";
OleDbCommand cmd = new OleDbCommand(sql,oleConnection1);
  OleDbDataReader dr;
  this.oleConnection1 = new OleDbConnection(StoreMIS.database.dbConnection.connection);
try
{
cmd.Connection.Open();
dr = cmd.ExecuteReader();
   
if (dr.Read())
{
MessageBox.Show("删除快件 '"+ds.Tables["material"].Rows[dataGrid1.CurrentCell.RowNumber] [1].ToString().Trim()+"'失败,请先删除该快件入库信息!","提示");
dr.Close();
}
else
{
dr.Close();
string sql1="delete * from materialinfo where MID = '"+ds.Tables["material"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim()+"'";
cmd.CommandText = sql1;
cmd.ExecuteNonQuery();
MessageBox.Show("删除快件'"+ds.Tables["material"].Rows[dataGrid1.CurrentCell.RowNumber][1].ToString().Trim()+"'成功!","提示");
}
}
else
MessageBox.Show("没有指定快件信息!","提示");
}
catch(Exception ex)
{
 MessageBox.Show(ex.Message); 
}
finally
{
  cmd.Connection.Close();
}
} --------------------编程问答--------------------
this.oleConnection1 = new OleDbConnection(StoreMIS.database.dbConnection.connection);
this.oleConnection1.Open();
dr = cmd.ExecuteReader();
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,