作者denby (等比)
看板C_Sharp
标题[问题] 并未将物件参考设定为物件的执行个体
时间Wed Feb 10 12:26:44 2010
碰到的问题挺难确定要去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="
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)
1F:→ optimist9266:用即时运算去找,错误那行一定有一个是null去找为什 02/10 12:48
2F:→ optimist9266:是null, 02/10 12:48
3F:→ denby:怎麽用即时运算呢? 设计模式中没办法用 也没用过Orz 02/10 12:50
4F:→ denby:用逐步执行 找到L是null 但是,不是有宣告了吗? 02/10 12:55
5F:→ ssccg:L = (Label)FindControl("L" + n); 02/10 13:15
6F:→ ssccg:你Page里要已经有ID为L1 ~ L10的Label 02/10 13:16
7F:→ denby:确定是有的 02/10 13:17
附上page的部分
※ 编辑: denby 来自: 220.143.23.190 (02/10 13:31)
8F:→ denby:@_@ L4变成L14 这个是问题的原因吗? 02/10 14:09
9F:→ juriolegend:用datelist+sqldatasource去系结radiobuttonlist 02/11 02:14
10F:→ juriolegend:应该比较容易吧 02/11 02:14
11F:→ denby:不过我的流程并不是那样耶 只能顺着自己的意思罗XD 02/11 19:49