对象不能从 DBNull 转换为其他类型。下面是相关的3段代码
Default3Bookbook=newBook();//book.BookId=Request.QueryString["BookId"];//添加书籍idbook.B...
Default3
Book book = new Book();
//book.BookId = Request.QueryString["BookId"];
//添加书籍id
book.BookId = txtBookId.Text.Trim();
//添加书籍名
book.BookName = txtBookName.Text.Trim();
//添加小说类型
book.Category = CategoryManager.GetCategoryById(Convert.ToInt32(ddlCategory.SelectedValue));
//添加小说介绍
book.Content = txtContent.Text.Trim();
//添加小说的点击率和字数(默认为0)
book.SupportCount = 0;
book.WordCount = 0;
//添加作者
book.Author = AuthorManager.GetAuthorId(Convert.ToInt32(ddlCategory0.SelectedValue));
//将书籍返还到数据库
BookManager.GetAddBook(book);
Response.Write("<script>alert('添加成功')</script>");
Response.Redirect("Default.aspx"); 展开
Book book = new Book();
//book.BookId = Request.QueryString["BookId"];
//添加书籍id
book.BookId = txtBookId.Text.Trim();
//添加书籍名
book.BookName = txtBookName.Text.Trim();
//添加小说类型
book.Category = CategoryManager.GetCategoryById(Convert.ToInt32(ddlCategory.SelectedValue));
//添加小说介绍
book.Content = txtContent.Text.Trim();
//添加小说的点击率和字数(默认为0)
book.SupportCount = 0;
book.WordCount = 0;
//添加作者
book.Author = AuthorManager.GetAuthorId(Convert.ToInt32(ddlCategory0.SelectedValue));
//将书籍返还到数据库
BookManager.GetAddBook(book);
Response.Write("<script>alert('添加成功')</script>");
Response.Redirect("Default.aspx"); 展开
2个回答
展开全部
//book.BookId = Request.QueryString["BookId"];
改为://book.BookId = Request.QueryString["BookId"]==null?"":Request.QueryString["BookId"];
//添加小说类型
book.Category = CategoryManager.GetCategoryById(Convert.ToInt32(ddlCategory.SelectedValue));
改为:book.Category = CategoryManager.GetCategoryById(Convert.ToInt32(ddlCategory.SelectedValue))==null?"":CategoryManager.GetCategoryById(Convert.ToInt32(ddlCategory.SelectedValue));
//添加作者
book.Author = AuthorManager.GetAuthorId(Convert.ToInt32(ddlCategory0.SelectedValue));
改为:
book.Author = AuthorManager.GetAuthorId(Convert.ToInt32(ddlCategory0.SelectedValue))==null?"":
AuthorManager.GetAuthorId(Convert.ToInt32(ddlCategory0.SelectedValue));
第三段代码中(GetBookByID方法),else代码段中的“return null”去掉
改为://book.BookId = Request.QueryString["BookId"]==null?"":Request.QueryString["BookId"];
//添加小说类型
book.Category = CategoryManager.GetCategoryById(Convert.ToInt32(ddlCategory.SelectedValue));
改为:book.Category = CategoryManager.GetCategoryById(Convert.ToInt32(ddlCategory.SelectedValue))==null?"":CategoryManager.GetCategoryById(Convert.ToInt32(ddlCategory.SelectedValue));
//添加作者
book.Author = AuthorManager.GetAuthorId(Convert.ToInt32(ddlCategory0.SelectedValue));
改为:
book.Author = AuthorManager.GetAuthorId(Convert.ToInt32(ddlCategory0.SelectedValue))==null?"":
AuthorManager.GetAuthorId(Convert.ToInt32(ddlCategory0.SelectedValue));
第三段代码中(GetBookByID方法),else代码段中的“return null”去掉
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你可以先判断从数据库取得的是不是DBNull,然后再转换类型。注意判断是不是DBNull不是用if(xxx==null)而是用if(xxx==System.DbNull.Value)
追问
追答
不是把cmd和DBNull相比啊,而是book.BookId=(string)reader["BookID"];这样的要先判断,不要强转,如果不是DBNull才转换
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询