public string[] str;
public string shopid;
int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//shopid=Request.QueryString["shopid"].ToString();
//databind();
//datalistbind();
tuijianbind();
}
}
void tuijianbind()
{
string sql = "select zw_recommand from MarUser where zw_id=3 ";
DataSet ds = SqlHelper.DataSet(SqlHelper.ConnectionString_news, CommandType.Text, sql, null);
str = ds.Tables[0].Rows[0]["zw_recommand"].ToString().Trim().Split('|');
DataList3.DataSource = str;
DataList3.DataBind();
}
protected void DataList3_ItemDataBound(object sender, DataListItemEventArgs e)
{
string sql = "select image,productname,introduce from product where id=" + str[i] + " ";
DataSet ds = SqlHelper.DataSet(SqlHelper.ConnectionString_news, CommandType.Text, sql, null);
Image img = (Image)e.Item.FindControl("Image1");
img.ImageUrl=ds.Tables[0].Rows[0]["image"].ToString();
Label a = (Label)e.Item.FindControl("Label1");
a.Text = ds.Tables[0].Rows[0]["productname"].ToString();
Label b = (Label)e.Item.FindControl("Label2");
b.Text = ds.Tables[0].Rows[0]["introduce"].ToString();
i++;
}上面的cs页面的代码 结果报错未将对象引用设置到对象的实例。
aspx的源码如下
<asp:DataList ID="DataList3" runat="server" RepeatColumns="5"
RepeatDirection="Horizontal" onitemdatabound="DataList3_ItemDataBound">
<table>
<tr><td><asp:Image ID="Image1" runat="server" /></td></tr>
<tr>
<td> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td></tr>
<tr><td>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> </td></tr>
</table>
</ItemTemplate>
</asp:DataList>
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 87: DataSet ds = SqlHelper.DataSet(SqlHelper.ConnectionString_news, CommandType.Text, sql, null);
行 88: Image img = (Image)e.Item.FindControl("Image1");
行 89: img.ImageUrl=ds.Tables[0].Rows[0]["image"].ToString(); //有错
行 90: Label a = (Label)e.Item.FindControl("Label1");
行 91: a.Text = ds.Tables[0].Rows[0]["productname"].ToString();//有错
但是我用response.write()又能显示出正确的
我把这些代码复制粘贴到另外一个页面,结果那个页面又是正确的,所以搞得我很郁闷,实在不知道我错在哪里 网上的那些错误,我都没有犯,还望高人指点……谢谢
public string shopid;
int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//shopid=Request.QueryString["shopid"].ToString();
//databind();
//datalistbind();
tuijianbind();
}
}
void tuijianbind()
{
string sql = "select zw_recommand from MarUser where zw_id=3 ";
DataSet ds = SqlHelper.DataSet(SqlHelper.ConnectionString_news, CommandType.Text, sql, null);
str = ds.Tables[0].Rows[0]["zw_recommand"].ToString().Trim().Split('|');
DataList3.DataSource = str;
DataList3.DataBind();
}
protected void DataList3_ItemDataBound(object sender, DataListItemEventArgs e)
{
string sql = "select image,productname,introduce from product where id=" + str[i] + " ";
DataSet ds = SqlHelper.DataSet(SqlHelper.ConnectionString_news, CommandType.Text, sql, null);
Image img = (Image)e.Item.FindControl("Image1");
img.ImageUrl=ds.Tables[0].Rows[0]["image"].ToString();
Label a = (Label)e.Item.FindControl("Label1");
a.Text = ds.Tables[0].Rows[0]["productname"].ToString();
Label b = (Label)e.Item.FindControl("Label2");
b.Text = ds.Tables[0].Rows[0]["introduce"].ToString();
i++;
}上面的cs页面的代码 结果报错未将对象引用设置到对象的实例。
aspx的源码如下
<asp:DataList ID="DataList3" runat="server" RepeatColumns="5"
RepeatDirection="Horizontal" onitemdatabound="DataList3_ItemDataBound">
<table>
<tr><td><asp:Image ID="Image1" runat="server" /></td></tr>
<tr>
<td> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td></tr>
<tr><td>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> </td></tr>
</table>
</ItemTemplate>
</asp:DataList>
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 87: DataSet ds = SqlHelper.DataSet(SqlHelper.ConnectionString_news, CommandType.Text, sql, null);
行 88: Image img = (Image)e.Item.FindControl("Image1");
行 89: img.ImageUrl=ds.Tables[0].Rows[0]["image"].ToString(); //有错
行 90: Label a = (Label)e.Item.FindControl("Label1");
行 91: a.Text = ds.Tables[0].Rows[0]["productname"].ToString();//有错
但是我用response.write()又能显示出正确的
我把这些代码复制粘贴到另外一个页面,结果那个页面又是正确的,所以搞得我很郁闷,实在不知道我错在哪里 网上的那些错误,我都没有犯,还望高人指点……谢谢
判断:
if(ds!=null)
{
if(ds.Tables[0].Rows.Count>0)
{
..//这时再执行你的取值赋值操作
}
}
{
Response.Write("asasdasasdas");
}
有值,说明ds不是null
或者
加上判断:
Image img = (Image)e.Item.FindControl("Image1");
if(img!=null&&ds.Tables[0].Rows.Count>0)
{
img.ImageUrl=ds.Tables[0].Rows[0]["image"].ToString(); //有错
}
RepeatDirection="Horizontal" onitemdatabound="DataList3_ItemDataBound">
<table>
<tr><td><asp:Image ID="Image1" runat="server" /></td></tr>
<tr>
<td> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td></tr>
<tr><td>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> </td></tr>
</table>
</ItemTemplate>
</asp:DataList>
有一个<headtemplate>店主推荐商品</headtemplate>把这个去掉就行了,但是我还是不知道为什么,这个加在datalist3中会错误,还望高手帮忙解决
protected void DataList3_ItemDataBound(object sender, DataListItemEventArgs e)
{
string sql = "select image,productname,introduce from product where id=" + str[i] + " ";
DataSet ds = SqlHelper.DataSet(SqlHelper.ConnectionString_news, CommandType.Text, sql, null);
Image img = (Image)e.Item.FindControl("Image1");
img.ImageUrl=ds.Tables[0].Rows[0]["image"].ToString();
Label a = (Label)e.Item.FindControl("Label1");
a.Text = ds.Tables[0].Rows[0]["productname"].ToString();
Label b = (Label)e.Item.FindControl("Label2");
b.Text = ds.Tables[0].Rows[0]["introduce"].ToString();
i++;
}
请在这里面修改:protected void DataList3_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string sql = "select image,productname,introduce from product where id=" + str[i] + " ";
DataSet ds = SqlHelper.DataSet(SqlHelper.ConnectionString_news, CommandType.Text, sql, null);
Image img = (Image)e.Item.FindControl("Image1");
img.ImageUrl=ds.Tables[0].Rows[0]["image"].ToString();
Label a = (Label)e.Item.FindControl("Label1");
a.Text = ds.Tables[0].Rows[0]["productname"].ToString();
Label b = (Label)e.Item.FindControl("Label2");
b.Text = ds.Tables[0].Rows[0]["introduce"].ToString();
i++;
}
}
public string shopid;
int i = 0;
不是静态变量,被初始化了?