System.NullReferenceException: Object reference not set to an instance of an object [duplicate]

我与影子孤独终老i 提交于 2019-12-02 14:51:34
Bindas

Your View state is null Check for

if(ViewState["Edit"]!= null)
{
 if (ViewState["Edit"].ToString() == "new")
}
Sergio

If ViewState["Edit"] hasn't been set, .ToString() will throw an exception.

Try one of these:

  • if (ViewState["Edit"]!= null && ViewState["Edit"].ToString() == "new") //best one
  • if (ViewState["Edit"] + "" == "new")

Well, either ViewState is null or ViewState["Edit"] is returning null...

if (ViewState["Edit"] != null && ViewState["Edit"].ToString() == "new") {

or if whats supposed to be in the viewstate is a string, this should work:

if ((string)ViewState["Edit"] == "new") {

Try

protected void btnSaveDetails_Click(object sender, EventArgs e) 
{         
DataRow row = null;          
 if (ViewState["Edit"] != null && ViewState["Edit"].ToString() == "new") 
 {
 }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!