質問

Hi I have been creating an order form for a project and having difficulty getting the day number on to a data row. I have the below code but am getting the error 'ExecuteReader requires an open and available Connection. The connection's current state is closed.'. any help on this would be appreciated.

 private void cmbSelectDay_SelectedIndexChanged(object sender, EventArgs e)
    {

        //variable for case statement
        int day = 0;

        string Sql = "Select deliveryDayNo from standardOrderDetails order by deliveryDayNo";
        SqlConnection conn = new SqlConnection(connStr);  // ...
        SqlCommand cmd = new SqlCommand(Sql, conn); //there is an error happening here that im not too sure about that would need to be fixed first. but once its fixed this should all work.
        SqlDataReader DR = cmd.ExecuteReader();


            while (DR.Read())
            {
                cmbSelectDay.Items.Add(DR[0]);

            }


                //case statement to take the selected value of the combo box and assign a new value to the variable "day" acordingly.
                switch(cmbSelectDay.SelectedText.ToString())
                {
                    case "Monday":
                        {
                            day = 1;
                            break;
                        }
                    case "Tuesday":
                        {
                            day = 2;
                            break;
                        }
                    case "Wednesday":
                        {
                            day = 3;
                            break;
                        }
                    case "Thursday":
                        {
                            day = 4;
                            break;
                        }
                    case "Friday":
                        {
                            day = 5;
                            break;
                        }
                    case "Saturday":
                        {
                            day = 6;
                            break;
                        }

                }


                // DataRow dr = dsLeprechaun.Tables["standardOrderDetails"].Rows.Find(day);
                DataRow dr = dsLeprechaun.Tables["standardOrderDetails"].Rows.Find(day.ToString());
                dgvOrder.Rows.Add(dr["deliveryDayNo"].ToString());


            lblSelectCategory.Visible = true;
            lstBoxSelectCategory.Visible = true;


    }
役に立ちましたか?

解決

So... open the connection:

using(SqlConnection conn = new SqlConnection(connStr))
using(SqlCommand cmd = new SqlCommand(Sql, conn))
{
    conn.Open();
    using(SqlDataReader DR = cmd.ExecuteReader())
    {
        // consume data
    }
}

You might also want to look into tools that help with ADO.NET, such as "dapper" - for example, if I assume that this data is a string, we could shorten all of this to just:

using(SqlConnection conn = new SqlConnection(connStr))
{
    var days = conn.Query<string>(
        "Select deliveryDayNo from standardOrderDetails order by deliveryDayNo"
    ).ToList();
    // now add the items from "days"
}

他のヒント

You need to Open the Connection Object conn before calling the ExecuteReader() method.

Try This:

SqlConnection conn = new SqlConnection(connStr); 
SqlCommand cmd = new SqlCommand(Sql, conn); 
conn.Open();                                 //Add this statement
SqlDataReader DR = cmd.ExecuteReader();

you have to open the connection

conn.Open();
using(SqlDataReader reader = cmd.ExecuteReader())
  {
    while(reader.Read())
    {
      //code here
    }
  }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top