###前提・実現したいこと
Visual Studio 2015 にて C#を使いシステム開発を行っています。
label1001 ~ label1099までの配列を作り使用したいと考えています。
しかし下記コードで配列を作成し実行すると以下のエラーが出てしまします。
またdataAdd のifが実行されず通り抜けているため、labels[1]にlabel1001の値が入っていないことが原因だと考えていますが、なぜ値入っていないのかがわかりません。
原因もしくは、labelの配列の作成方法を教えていただきたいです。
###発生している問題・エラーメッセージ
エラーが起こっている131行目とは3つ目のソースの
var dataReader = cmd.ExecuteReader();
です。
###該当のソースコード
以下のソースはすべて同じ.csファイルに記述されています。
C#
1 2 public partial class CalendarEditTR : Form 3 { 4 Label firstClicked = null; 5 Label secondClicked = null; 6 7 public int year { get; internal set; } 8 public int month { get; internal set; } 9 public int day { get; internal set; } 10 11 private Label[] labels;//配列のフィールドを作成 12 13 14 public CalendarEditTR() 15 { 16 InitializeComponent(); 17 }
C#
1 private void CalendarEditTR_Load(object sender, EventArgs e) 2 { 3 string date = year.ToString() + "/" + month.ToString() + "/" + day.ToString(); 4 5 labels = new Label[100];//配列作成 6 7 for(int i = 1; i <=99;i++) 8 { 9 labels[i] = (Controls["label"+i] as Label); 10 } 11 }
C#
1 private void dataAdd() 2 { 3 StringBuilder sb = new StringBuilder(); 4 sb.Append("Server=localhost;"); 5 sb.Append("Port=5433;"); 6 sb.Append("User Id=postgres;"); 7 sb.Append("Password=mogurin2;"); //インストール時入れた値 8 sb.Append("Database=db_football;"); 9 10 string connString = sb.ToString(); 11 using (var con = new NpgsqlConnection(connString)) 12 { 13 try 14 { 15 con.Open(); 16 var cmd = new NpgsqlCommand(@"update tb_calendar set schedule10 = :label where year = :year and month = :month and day = :day and schedule10 is null and schedule9 is not null and schedule8 is not null and schedule7 is not null and schedule6 is not null and schedule5 is not null and schedule4 is not null and schedule3 is not null and schedule2 is not null and schedule1 is not null;" 17 + @"update tb_calendar set schedule9 = :label where year = :year and month = :month and day = :day and schedule9 is null and schedule8 is not null and schedule7 is not null and schedule6 is not null and schedule5 is not null and schedule4 is not null and schedule3 is not null and schedule2 is not null and schedule1 is not null;" 18 + @"update tb_calendar set schedule8 = :label where year = :year and month = :month and day = :day and schedule8 is null and schedule7 is not null and schedule6 is not null and schedule5 is not null and schedule4 is not null and schedule3 is not null and schedule2 is not null and schedule1 is not null;" 19 + @"update tb_calendar set schedule7 = :label where year = :year and month = :month and day = :day and schedule7 is null and schedule6 is not null and schedule5 is not null and schedule4 is not null and schedule3 is not null and schedule2 is not null and schedule1 is not null;" 20 + @"update tb_calendar set schedule6 = :label where year = :year and month = :month and day = :day and schedule6 is null and schedule5 is not null and schedule4 is not null and schedule3 is not null and schedule2 is not null and schedule1 is not null;" 21 + @"update tb_calendar set schedule5 = :label where year = :year and month = :month and day = :day and schedule5 is null and schedule4 is not null and schedule3 is not null and schedule2 is not null and schedule1 is not null;" 22 + @"update tb_calendar set schedule4 = :label where year = :year and month = :month and day = :day and schedule4 is null and schedule3 is not null and schedule2 is not null and schedule1 is not null;" 23 + @"update tb_calendar set schedule3 = :label where year = :year and month = :month and day = :day and schedule3 is null and schedule2 is not null and schedule1 is not null;" 24 + @"update tb_calendar set schedule2 = :label where year = :year and month = :month and day = :day and schedule2 is null and schedule1 is not null;" 25 + @"update tb_calendar set schedule1 = :label where year = :year and month = :month and day = :day and schedule1 is null;" 26 + @"insert into tb_training2 (training_name,year,month,day) values (:label,:year,:month,:day)", con); 27 28 //cmd = new NpgsqlCommand(@, con); 29 cmd.Parameters.Add(new NpgsqlParameter("year", DbType.Int32) { Value = year }); 30 cmd.Parameters.Add(new NpgsqlParameter("month", DbType.Int32) { Value = month }); 31 cmd.Parameters.Add(new NpgsqlParameter("day", DbType.Int32) { Value = day }); 32 33 34 35 36 37 if (labels[1].BackColor == Color.Gray) 38 { 39 cmd.Parameters.Add(new NpgsqlParameter("label", DbType.String) { Value = labels[1].Text}); 40 } 41 42 43 //クエリを取得? 44 var dataReader = cmd.ExecuteReader(); 45 if (dataReader.HasRows) 46 { 47 48 } 49 50 dataReader.Close(); 51 52 53 //var cmd3 = new NpgsqlCommand(@"select schedule1 from calendar where year = :year and month = :month and day = :day;", con); 54 //cmd.Parameters.Add(new NpgsqlParameter("year", DbType.Int32) { Value = year }); 55 //cmd.Parameters.Add(new NpgsqlParameter("month", DbType.Int32) { Value = month }); 56 //cmd.Parameters.Add(new NpgsqlParameter("day", DbType.Int32) { Value = day }); 57 } 58 catch (Exception e) 59 { 60 MessageBox.Show(e.ToString()); 61 } 62 } 63 }
C#
1private void button4_Click(object sender, EventArgs e) 2 { 3 dataAdd();//画像の追加ボタン 4 }
C#
1 private void label1001_Click(object sender, EventArgs e) 2 { 3 Label clickedLabel = sender as Label; 4 if (clickedLabel != null) 5 { 6 if (clickedLabel.BackColor == Color.Gray) return; 7 if (firstClicked == null) 8 { 9 firstClicked = clickedLabel; 10 firstClicked.BackColor = Color.Gray; 11 return; 12 } 13 secondClicked = clickedLabel; 14 secondClicked.BackColor = Color.Gray; 15 firstClicked.BackColor = Color.White; 16 firstClicked = secondClicked; 17 secondClicked = null; 18 } 19 clickedLabel.BackColor = Color.Gray; 20 }
###試したこと
該当コードのlabels[1] を label1001 に変更すると実行成功
###補足情報(言語/FW/ツール等のバージョンなど)
配列は以下のサイトを参考にして作成しました。
http://secretbase.da-te.jp/e543444.html
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。