否则,当读者有错误时条件不起作用

时间:2018-06-23 05:19:46

标签: c# asp.net if-statement sqldatareader

有人可以告诉我为什么else条件在下面的代码中不起作用

asp.net Web应用程序中的链接按钮后面的代码中包含以下代码:参数化的SqlCommand从SQL Server数据库中获取一行,如果条件正常,则SqlDataReader rdr1.HasRows正常,否则条件不工作。

代码已更新

protected void LinkButton1_Click(object sender, EventArgs e)
{
    string comid = DropDownList4.SelectedValue.ToString();

    using (SqlConnection con = new SqlConnection(cs))
    {
        SqlCommand cmd = new SqlCommand("Select * from Commercials Where id =" + comid, con);
        con.Open();

        SqlDataReader rdr = cmd.ExecuteReader();

        while (rdr.Read())
        {
            string dur = rdr["duration"].ToString();
            Char delimiter = '/';
            string[] dd = DateTime.Parse(rdr["rodate"].ToString()).ToString("dd/MM/yyyy").Split(delimiter);

            if (DropDownList3.SelectedValue.ToString().Contains("BOL NEWS") == true && DropDownList1.SelectedValue.ToString().Contains("After Headlines") == true)
            {
                SqlConnection con0 = new SqlConnection(cs);
                string sql01 = "Select * from CTS Where air_time=(Select max(air_time) from CTS where air_date=@airdate and air_time Like @airtime and channel=@channel and Slot=@slot) and air_date=@airdate1";
                con0.Open();

                SqlCommand cmd1 = new SqlCommand(sql01, con0);
                cmd1.Parameters.AddWithValue("@airdate", TextBox1.Text);
                cmd1.Parameters.AddWithValue("@channel", DropDownList3.SelectedValue.ToString());
                cmd1.Parameters.AddWithValue("@airtime", DropDownList2.SelectedValue.ToString().Substring(0, 2) + "%");
                cmd1.Parameters.AddWithValue("@slot", DropDownList1.SelectedValue.ToString().Remove(0, 3));
                cmd1.Parameters.AddWithValue("@airdate1", TextBox1.Text);

                SqlDataReader rdr1 = cmd1.ExecuteReader();

                while (rdr1.Read())
                {
                    string startTime0 = rdr1["air_time"].ToString();
                    string addsec = rdr1["duration"].ToString();

                    if (rdr1.HasRows)
                    {
                        DateTime startTime1 = DateTime.ParseExact(startTime0, "HH:mm:ss", null);
                        string startHeadlines_ = startTime1.AddSeconds(int.Parse(addsec)).ToString("HH:mm:ss");

                        using (SqlConnection con2 = new SqlConnection(cs))
                        {
                            string type = "Commercial";
                            string year = dd[2].ToString().Substring(dd[2].ToString().Length - 2);
                            string HouseId = "CH1COM001" + rdr["rono"] + rdr["duration"] + "S" + dd[1] + dd[0] + year;
                            string sql1 = "Insert into CTS(air_date,air_time,HouseNumber,rono,Title,duration,Slot,type,channel)Values('" + TextBox1.Text + "','" + startHeadlines_ + "','" + HouseId + "','" + rdr["rono"] + "','" + rdr["slug"] + "','" + rdr["duration"] + "','" + DropDownList1.SelectedValue.Remove(0, 3) + "','" + type + "','" + DropDownList3.SelectedValue.ToString() + "')";

                            con2.Open();

                            SqlCommand InsertCmd = new SqlCommand(sql1, con2);
                            InsertCmd.ExecuteNonQuery();

                            con2.Close();
                        }
                    }
                    else 
                    {
                        DateTime startTime = DateTime.ParseExact(DropDownList2.SelectedValue.ToString(), "HH:mm:ss", null);
                        string startHeadlines = startTime.AddSeconds(210).ToString("HH:mm:ss");

                        using (SqlConnection con1 = new SqlConnection(cs))
                        {
                            string type = "Commercial";
                            string year = dd[2].ToString().Substring(dd[2].ToString().Length - 2);
                            string HouseId = "CH1COM001" + rdr["rono"] + rdr["duration"] + "S" + dd[1] + dd[0] + year;
                            string sql = "Insert into CTS(air_date,air_time,HouseNumber,rono,Title,duration,Slot,type,channel)Values('" + TextBox1.Text + "','" + startHeadlines + "','" + HouseId + "','" + rdr["rono"] + "','" + rdr["slug"] + "','" + rdr["duration"] + "','" + DropDownList1.SelectedValue.ToString().Remove(0, 3) + "','" + type + "','" + DropDownList3.SelectedValue.ToString() + "')";

                            con1.Open();

                            SqlCommand InsertCmd = new SqlCommand(sql, con1);
                            InsertCmd.ExecuteNonQuery();

                            con1.Close();
                        }
                    }
                }
                con0.Close();
            }
        }

        con.Close();
    }
}

0 个答案:

没有答案