0
データベースからデータを取得しているリストがあります。常にとなり、のレコードがデータベース検索クエリを満たしています。データベースの詳細を変更してレコードを増減すると、レコードは1つも残ってしまいます。うち、検索条件を満たしている8つのレコードが、それは言う5のうち7を返すと言うことは、あなたのwhile
ループが誤っている4データベースからのinadequeteレコードを返すループ
using (SqlConnection con = new SqlConnection("Data Source=*** Initial Catalog=***y;Persist Security Info=True;User ID=***;Password=***"))//connection string
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT SubjectName FROM dbo.mySubjects WHERE SubjectClass = @theSubjectClass", con);//query string
cmd.Parameters.Add("@theSubjectClass", SqlDbType.VarChar).Value = myClass;
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
//int t = 0;
//count = count + 1; Tried adding a counter that gets the number of raws in the table that meet the search query and create a loop based on that but it produces an error of trying to read data where there is no data to read
while (count >= 0) {
dr.Read();
List <string> Subjects = new List<string>();
Subjects.Add(dr.GetString(0));
//t = 0;
foreach (var course in Subjects)
{
var a = new HtmlGenericControl("a");
a.Attributes["class"] = "color-info";
var container = new HtmlGenericControl("div");
container.Attributes["class"] = "col-lg-4 col-sm-6";
var div = new HtmlGenericControl("div");
div.Attributes["class"] = "card card-warning wow zoomInUp animation-delay-5";
var text = new HtmlGenericControl("div");
text.Attributes["class"] = "card-block text-center";
var p = new HtmlGenericControl("p");
text.Controls.Add(p);
string manage = dr.GetValue(0) + " Videos";
var btn = new Button
{
Text = manage,
CssClass = "btn btn-warning"
};
btn.Click += new EventHandler(ExploreButtonClick);
text.Controls.Add(btn);
div.Controls.Add(text);
container.Controls.Add(div);
a.Controls.Add(container);
Row.Controls.Add(a);
count = count - 1;
}
}
con.Close();
}
私は異なるシナリオで上記のソリューションを使用しましたが、それは完璧に機能しましたが、上記のシナリオではソリューションは期待される結果の半分を生成します。もう少しデバッグさせてください –