Asp.net如何解决错误

时间:2011-03-17 12:07:06

标签: asp.net

我正在设计我的网页,因为我输入了书籍ID并输入了Nameofthebook。然后单击按钮堆栈的书可用是转到另一页然后不可用显示错误消息是书不可用我的项目(图书馆管理系统)

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Bookcheck : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string constr = null;
        constr = ConfigurationManager.ConnectionStrings["librarymanagementconnetionstring"].ConnectionString;
        SqlConnection cnn = new SqlConnection(constr);
        cnn.Open();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter("SELECT*FROM BOOKREGISTRATIONDETAILS WHERE bookId='" + txtid.Text.Trim() + "'AND Nameofbook='" + txtnb.Text.Trim() + "'", constr);
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            Response.Redirect("Issueofbook.aspx");
        }
        else
        {
            Msg.Text = "NO book available";
            Msg.Visible = true;
        }
    }       
}      

错误:
对象引用未设置为对象的实例。

3 个答案:

答案 0 :(得分:5)

我注意到在 librarymanagementconnetionstring 中,连接拼写错误。这是您的web.config中连接字符串的正确条目吗?

答案 1 :(得分:2)

我会说这是行

if (ds.Tables[0].Rows.Count > 0)

给出错误。

如果您的DataSet没有表格,则ds.Tables可能为空。

如果有可能,那么你需要为此编码。

如果没有行ds.Tables[0].Rows将不为空,您的测试应该有效。

答案 2 :(得分:1)

更改

if (ds.Tables[0].Rows.Count > 0)

if(ds.HasRows)