[問題] 並未將物件參考設定為物件的執行個體
碰到的問題挺難確定要去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
02/10 12:48, 1F
→
02/10 12:48, , 2F
02/10 12:48, 2F
→
02/10 12:50, , 3F
02/10 12:50, 3F
→
02/10 12:55, , 4F
02/10 12:55, 4F
→
02/10 13:15, , 5F
02/10 13:15, 5F
→
02/10 13:16, , 6F
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
02/10 14:09, 8F
→
02/11 02:14, , 9F
02/11 02:14, 9F
→
02/11 02:14, , 10F
02/11 02:14, 10F
→
02/11 19:49, , 11F
02/11 19:49, 11F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 9 篇):