[問題] 並未將物件參考設定為物件的執行個體

看板C_Sharp作者 (等比)時間14年前 (2010/02/10 12:26), 編輯推噓0(0011)
留言11則, 4人參與, 最新討論串1/9 (看更多)
碰到的問題挺難確定要去DB還是網設版問XD 不過用的是C#就先來問問看囉 這是一個ASP.NET網頁的其中一段 void Page_Load(object sender, EventArgs e) { string dbString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + Server.MapPath("db8_1.mdb"); OleDbConnection dbConn = new OleDbConnection(dbString); dbConn.Open(); string cmdString = "SELECT * FROM data"; OleDbCommand dbCommand = new OleDbCommand(cmdString, dbConn); OleDbDataReader myReader = dbCommand.ExecuteReader(); string[ , ] Myarr = new string[20, 7] ; int i = 0; while (myReader.Read( )) { Myarr[i, 0] = (string)myReader["Question"]; Myarr[i, 1] = (string)myReader["A"]; Myarr[i, 2] = (string)myReader["B"]; Myarr[i, 3] = (string)myReader["C"]; Myarr[i, 4] = (string)myReader["D"]; Myarr[i, 5] = (string)myReader["E"]; Myarr[i, 6] = (string)myReader["Answer"]; i++; } //到此程式執行上都沒有發生問題 //有使用非以下的方法測試了前幾個陣列和 Label還有RadioButtonList之間的繫結,也是ok的 //但是換成了以下的方法卻又發生了錯誤 Label L = new Label( ); RadioButtonList RBL = new RadioButtonList( ); for (int n = 1; n <= 10; n++) { L = (Label)FindControl("L" + n); RBL = (RadioButtonList)FindControl("RBL" + n); L.Text = (string)Myarr[n-1, 0]; //執行時發生錯誤的地方。 "並未將物件參考設定為物件的執行個體" //在學期中是採用L.Text = (string)myReader.Read["Question"]; 並沒有此問題的產生 //不過這次需求不太一樣,換從陣列讀取值,但是問題似乎又不在陣列上 難道是L在宣告上有什麼錯誤發生嗎? RBL.Items[0].Text = (string)Myarr[n-1, 1]; RBL.Items[1].Text = (string)Myarr[n-1, 2]; RBL.Items[2].Text = (string)Myarr[n-1, 3]; RBL.Items[3].Text = (string)Myarr[n-1, 4]; RBL.Items[4].Text = (string)Myarr[n-1, 5]; RBL.Items[0].Value = "A"; RBL.Items[1].Value = "B"; RBL.Items[2].Value = "C"; RBL.Items[3].Value = "D"; RBL.Items[4].Value = "E"; } <html xmlns="" rel="nofollow">http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="L1" runat="server"></asp:Label> <asp:RadioButtonList ID="RBL1" runat="server"> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> </asp:RadioButtonList> <asp:Label ID="L2" runat="server"></asp:Label> <asp:RadioButtonList ID="RBL2" runat="server"> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> </asp:RadioButtonList> <asp:Label ID="L3" runat="server"></asp:Label> <asp:RadioButtonList ID="RBL3" runat="server"> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> </asp:RadioButtonList> <asp:Label ID="Ll4" runat="server"></asp:Label> <asp:RadioButtonList ID="RBL4" runat="server"> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> </asp:RadioButtonList> <asp:Label ID="L5" runat="server"></asp:Label> <asp:RadioButtonList ID="RBL5" runat="server"> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> </asp:RadioButtonList> <asp:Label ID="L6" runat="server"></asp:Label> <asp:RadioButtonList ID="RBL6" runat="server"> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> </asp:RadioButtonList> <asp:Label ID="L7" runat="server"></asp:Label> <asp:RadioButtonList ID="RBL7" runat="server"> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> </asp:RadioButtonList> <asp:Label ID="L8" runat="server"></asp:Label> <asp:RadioButtonList ID="RBL8" runat="server"> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> </asp:RadioButtonList> <asp:Label ID="L9" runat="server"></asp:Label> <asp:RadioButtonList ID="RBL9" runat="server"> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> </asp:RadioButtonList> <asp:Label ID="L10" runat="server"></asp:Label> <asp:RadioButtonList ID="RBL10" runat="server"> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> <asp:ListItem></asp:ListItem> </asp:RadioButtonList> </div> </form> </body> </html> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.143.23.190 忘了補發生什麼事 ※ 編輯: denby 來自: 220.143.23.190 (02/10 12:28)

02/10 12:48, , 1F
用即時運算去找,錯誤那行一定有一個是null去找為什
02/10 12:48, 1F

02/10 12:48, , 2F
是null,
02/10 12:48, 2F

02/10 12:50, , 3F
怎麼用即時運算呢? 設計模式中沒辦法用 也沒用過Orz
02/10 12:50, 3F

02/10 12:55, , 4F
用逐步執行 找到L是null 但是,不是有宣告了嗎?
02/10 12:55, 4F

02/10 13:15, , 5F
L = (Label)FindControl("L" + n);
02/10 13:15, 5F

02/10 13:16, , 6F
你Page裡要已經有ID為L1 ~ L10的Label
02/10 13:16, 6F

02/10 13:17, , 7F
確定是有的
02/10 13:17, 7F
附上page的部分 ※ 編輯: denby 來自: 220.143.23.190 (02/10 13:31)

02/10 14:09, , 8F
@_@ L4變成L14 這個是問題的原因嗎?
02/10 14:09, 8F

02/11 02:14, , 9F
用datelist+sqldatasource去繫結radiobuttonlist
02/11 02:14, 9F

02/11 02:14, , 10F
應該比較容易吧
02/11 02:14, 10F

02/11 19:49, , 11F
不過我的流程並不是那樣耶 只能順著自己的意思囉XD
02/11 19:49, 11F
文章代碼(AID): #1BSZM6tN (C_Sharp)
討論串 (同標題文章)
文章代碼(AID): #1BSZM6tN (C_Sharp)