vs2010 asp.net网页点击修改按钮报错用户未处理NullReferenceException

报错用户未处理NullReferenceException
未将对象引用设置到对象的实例

该怎么处理呢 ?需要添加哪些代码呢

img

vs2010 asp.net网页点击修改按钮报错用户未处理NullReferenceException

img

img

img

DropDownList1.Items.FindByValue(dr["GoodsTypeID"].ToString()).Selected = true;
//DropDownList1.Items.FindByText(dr["GoodsTypeName"].ToString()).Selected = true;//这句不需要,上面已经通过listitem的值找到需要的项实现选中了,这里不需要再通过text找listitem。报错是因为DropDownList1不存在和GoodsTypeName相同的listitem对象,所以FindByText返回null导致,再设置selected属性就报错了

img


有其他问题可以继续交流~

是不是字段名写错了啊,颜色都不一样

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using System.Web.SessionState;
public partial class AdminManger_ModifyGoods : System.Web.UI.Page
{
SQLDataBase data = new SQLDataBase();

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {


        DropDownList1.DataSource = data.GetDataReader("select * from GoodsType");
        DropDownList1.DataValueField = "id";
        DropDownList1.DataTextField = "name";
        DropDownList1.DataBind();
        string sql = "select * from Goods where GoodsID=" + Request.QueryString["id"].ToString();
        getdata(sql);
    }
}
private void getdata(string sql)
{
    SqlDataReader dr = data.GetDataReader(sql);
    if (dr.Read())
    {
        title.Text = dr["GoodsName"].ToString();
        TextBox2.Text = dr["GoodsPrice"].ToString();
        TextBox3.Text = dr["GoodsNum"].ToString();
        content1.Value = dr["GoodsRemark"].ToString();
        pic.Text = dr["GoodsPhoto"].ToString();
        Image1.ImageUrl = "../files/" + dr["GoodsPhoto"].ToString();
        DropDownList1.Items.FindByValue(dr["GoodsTypeID"].ToString()).Selected = true;
        DropDownList1.Items.FindByText(dr["GoodsTypeName"].ToString()).Selected = true;



    }

}
protected void Button3_Click(object sender, EventArgs e)
{
    string res;
    upload up = new upload();
    res = up.Up(file1, "../files/");
    this.Label1.Visible = true;
    this.Label1.Text = up.Resup[Convert.ToInt32(res)];
    this.pic.Text = up.s;
    Image1.ImageUrl = "../files/" + pic.Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
    string sql = "update Goods set GoodsName='" + title.Text.ToString().Trim() + "',GoodsPrice='" + TextBox2.Text + "',GoodsNum='" + TextBox3.Text + "',GoodsRemark='" + content1.Value + "' ,GoodsPhoto='" + pic.Text + "',GoodsTypeID='" + DropDownList1.SelectedValue + "',GoodsTypeName='" + DropDownList1.SelectedItem.Text + "'  where GoodsID=" + Request.QueryString["id"].ToString();
    data.RunSql(sql);
    Alert.AlertAndRedirect("修改成功", "GoodsManger.aspx");
}

}

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632