C# 已经有一个与该命令相关联的打开的数据读取器,必须首先关闭该读取器。?8

C# 已经有一个与该命令相关联的打开的数据读取器,必须首先关闭该读取器。?8,c#,asp.net,C#,Asp.net,我正在尝试以二进制格式将图像文件保存到db中。然后检索这些二进制文件并在我的网页上显示图像。使用下面给定的代码时,产生了一个错误。请帮帮我 public partial class Default3 : System.Web.UI.Page { static SqlConnection con = new SqlConnection(@"connectionString"); protected void Page_Load(object sender, EventArgs e)

我正在尝试以二进制格式将图像文件保存到db中。然后检索这些二进制文件并在我的网页上显示图像。使用下面给定的代码时,产生了一个错误。请帮帮我

public partial class Default3 : System.Web.UI.Page
{
    static SqlConnection con = new SqlConnection(@"connectionString");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            filldropdown();
        }
    }

    private void filldropdown()
    {
        SqlCommand cmd = new SqlCommand("Select EmpID from Tbl_Emp", con);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlDataReader dr = cmd.ExecuteReader();
        TextBox3.Items.Clear();
        if (dr.HasRows)
        {

            while (dr.Read())
            {
                TextBox3.Items.Add(dr["EmpID"].ToString());
            }
        }
        con.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("insert into Tbl_Emp values(@id,@name,@image)", con);
        cmd.Parameters.AddWithValue("@id", TextBox1.Text);
        cmd.Parameters.AddWithValue("@name", TextBox2.Text);

        int img = FileUpload1.PostedFile.ContentLength;

        byte[] msdata = new byte[img];

        FileUpload1.PostedFile.InputStream.Read(msdata, 0, img);

        cmd.Parameters.AddWithValue("@image", msdata);

        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        cmd.ExecuteNonQuery();

        con.Close();
        filldropdown();
        Response.Write("Data Saved ....");

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("select * from Tbl_Emp where EmpID=@id", con);
        cmd.Parameters.AddWithValue("@id", TextBox3.Text);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows && dr.Read())
        {
            TextBox1.Text = dr["EmpID"].ToString();
            TextBox2.Text = dr["EmpName"].ToString();
            Image1.ImageUrl = "Handler.ashx?EmpID=" + TextBox3.Text;
        }
        else
        {
            Response.Write("Record With This ID Note Found");
        }
    }
}
资料来源:

<div>
      Enter ID  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
      Enter Name <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
      Enter Pic  <asp:FileUpload ID="FileUpload1" runat="server" /><br />
      <asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" /><br >
      <asp:Image ID="Image1"  runat="server" Height="137px" Width="130px" /><br /> 
      <asp:DropDownList ID="TextBox3" runat="server">
      </asp:DropDownList>
      <asp:Button ID="Button2" runat="server" Text="Search" onclick="Button2_Click" />

输入ID
输入名称
输入Pic


数据表:


在打开新对象之前,必须先关闭SqlDataReader对象,因为它是运行时对象

像这样亲密的身份

 dr.Close();

这是正确的答案

public partial class Default3 : System.Web.UI.Page
{
    static SqlConnection con = new SqlConnection(@"connectionString");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            filldropdown();
        }
    }

    private void filldropdown()
    {
        SqlCommand cmd = new SqlCommand("Select EmpID from Tbl_Emp", con);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlDataReader dr = cmd.ExecuteReader();
        TextBox3.Items.Clear();
        if (dr.HasRows)
        {

            while (dr.Read())
            {
                TextBox3.Items.Add(dr["EmpID"].ToString());
            }
        }
        dr.Close();
        con.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("insert into Tbl_Emp values(@id,@name,@image)", con);
        cmd.Parameters.AddWithValue("@id", TextBox1.Text);
        cmd.Parameters.AddWithValue("@name", TextBox2.Text);

        int img = FileUpload1.PostedFile.ContentLength;

        byte[] msdata = new byte[img];

        FileUpload1.PostedFile.InputStream.Read(msdata, 0, img);

        cmd.Parameters.AddWithValue("@image", msdata);

        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        cmd.ExecuteNonQuery();

        con.Close();
        filldropdown();
        Response.Write("Data Saved ....");

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("select * from Tbl_Emp where EmpID=@id", con);
        cmd.Parameters.AddWithValue("@id", TextBox3.Text);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows && dr.Read())
        {
            TextBox1.Text = dr["EmpID"].ToString();
            TextBox2.Text = dr["EmpName"].ToString();
            Image1.ImageUrl = "Handler.ashx?EmpID=" + TextBox3.Text;
        }
        else
        {
            Response.Write("Record With This ID Note Found");
        }
        dr.Close();
    }
}

处理清理SQL连接、命令和读卡器的最佳方法是使用
using
语句。此外,您实际上不需要根据发布的代码将连接保持为静态字段。下面是我在每种方法中的基本操作

protected void Some_Action(object sender, EventArgs e)
{
    using(SqlConnection con = new SqlConnection(@"connectionString"))
    {
        con.Open();
        using(SqlCommand cmd = new SqlCommand("Query Here", con))
        {
            // Do stuff with the command here like setting Parameters.
            using(SqlDataReader dr = cmd.ExecuteReader())
            {
                // Do stuff with the reader here
            }
        }
    }
}

这将确保即使发生异常,您的连接、命令和读卡器也会被释放。

最好使用
Using
,它将自动
释放您打开的连接/读卡器

但您可以在现有代码中这样使用

private void filldropdown()
    {
        SqlCommand cmd = new SqlCommand("Select EmpID from Tbl_Emp", con);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlDataReader dr = cmd.ExecuteReader();
        TextBox3.Items.Clear();
        if (dr.HasRows)
        {

            while (dr.Read())
            {
                TextBox3.Items.Add(dr["EmpID"].ToString());
            }
        }
        con.Close();
    dr.Close
    }





protected void Button2_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("select * from Tbl_Emp where EmpID=@id", con);
        cmd.Parameters.AddWithValue("@id", TextBox3.Text);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows && dr.Read())
        {
            TextBox1.Text = dr["EmpID"].ToString();
            TextBox2.Text = dr["EmpName"].ToString();
            Image1.ImageUrl = "Handler.ashx?EmpID=" + TextBox3.Text;
        }
        else
        {
            Response.Write("Record With This ID Note Found");
        }
    dr.Close();
    }

为什么要发布两次基本相同的答案,而不是编辑这一个?发布一个只包含两个现有答案的答案真的有意义吗?@juharr实际上是由于互联网连接。在回答过程中,我只看到了一个不完整的答案
private void filldropdown()
    {
        SqlCommand cmd = new SqlCommand("Select EmpID from Tbl_Emp", con);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlDataReader dr = cmd.ExecuteReader();
        TextBox3.Items.Clear();
        if (dr.HasRows)
        {

            while (dr.Read())
            {
                TextBox3.Items.Add(dr["EmpID"].ToString());
            }
        }
        con.Close();
    dr.Close
    }





protected void Button2_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("select * from Tbl_Emp where EmpID=@id", con);
        cmd.Parameters.AddWithValue("@id", TextBox3.Text);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows && dr.Read())
        {
            TextBox1.Text = dr["EmpID"].ToString();
            TextBox2.Text = dr["EmpName"].ToString();
            Image1.ImageUrl = "Handler.ashx?EmpID=" + TextBox3.Text;
        }
        else
        {
            Response.Write("Record With This ID Note Found");
        }
    dr.Close();
    }