有关对象不能从 DBNull 转换为其他类型问题

Tony088 2006-08-15 12:35:15
我想做网上购物系统,但运行就出现这种错误,请各位兄弟是什么原因


源错误:


行 118: {
行 119: DataRow r = dt.NewRow();
行 120: r[0] = Convert.ToInt32(ds1.Tables[0].Rows[i]["ID"]);
行 121: r[1] = ds1.Tables[0].Rows[i]["Sort"].ToString();
行 122: r[2] = Convert.ToInt32(ds1.Tables[0].Rows[i]["ProductID"]);


源文件: c:\inetpub\wwwroot\webapplicationdlh21\wsgw\gouwuche.aspx.cs 行: 120

堆栈跟踪:


[InvalidCastException: 对象不能从 DBNull 转换为其他类型。]
System.DBNull.System.IConvertible.ToInt32(IFormatProvider provider) +48
System.Convert.ToInt32(Object value) +36
WebApplicationDlh21.Wsgw.Gouwuche.dtBind() in c:\inetpub\wwwroot\webapplicationdlh21\wsgw\gouwuche.aspx.cs:120
WebApplicationDlh21.Wsgw.Gouwuche.dgGouwucheBind() in c:\inetpub\wwwroot\webapplicationdlh21\wsgw\gouwuche.aspx.cs:100
WebApplicationDlh21.Wsgw.Gouwuche.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\webapplicationdlh21\wsgw\gouwuche.aspx.cs:65
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731



...全文
624 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2006-08-15
  • 打赏
  • 举报
回复
使用之前要先进行判断~~~~~~~~!◎◎!◎!
孟子E章 2006-08-15
  • 打赏
  • 举报
回复
if(ds1.Tables[0].Rows[i]["ID"] != DBNull.Value)
{

r[0] = Convert.ToInt32(ds1.Tables[0].Rows[i]["ID"]);
....

}
Tony088 2006-08-15
  • 打赏
  • 举报
回复
gouwuche.aspx.cs 的具体源码是:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Web.Security;
using System.Text;

namespace WebApplicationDlh21.Wsgw
{
/// <summary>
/// Gouwuche 的摘要说明。
/// </summary>
public class Gouwuche : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblNum;
protected System.Web.UI.WebControls.DataGrid dgGouwuche;

private void Page_Load(object sender, System.EventArgs e)
{
if(Session["RegName"] == null || Convert.ToString(Session["RegName"]) == "")
{
Response.Write("<script>alert('对不起,您还没登录!');window.location='Index.aspx';</script>");
}

if(!IsPostBack)
{
if(Request["ID"] == "" || Request["ID"] == null)
{
dtBind();

if(dgGouwuche.Items.Count == 0)
{
Response.Write("<script>alert('购物车已清空,请继续购物!');window.location = 'Index.aspx'</scirpt>");
}
}
else
{
Db.Products myProducts = new Db.Products();
SqlDataReader dr = myProducts.drWhereID(Convert.ToInt32(Request["ID"]));
if(dr.Read())
{
Session["ProName"] = dr.GetString(1);

bool mIsChar = true;//是否是字符。
foreach(char c in dr.GetString(7))
{
if(!Char.IsDigit(c))
{
mIsChar = false;
break;
}
}

if(mIsChar == true)
{
Session["RetailPrice"] = dr.GetString(7);
dgGouwucheBind();
}
else
{
Session["RetailPrice"] = "0";
Response.Write("<script>alert('价格非数字,不能进行交易!');history.back(-1);</script>");
}
}
}
}
}

private void dgGouwucheBind()
{
Db.Temp myTemp = new Db.Temp();
DataSet ds = myTemp.dsSelWhereRegNameProductID(Convert.ToString(Session["RegName"]),Convert.ToInt32(Request["ID"]));

if(ds.Tables[0].Rows.Count == 0)
{
ArrayList a = new ArrayList();
a.Add(Convert.ToString(Session["RegName"]));
a.Add(Convert.ToInt32(Request["ID"]));
a.Add(1);
a.Add(Request["Sort"]);
myTemp.add(a);
}
else
{
ArrayList b = new ArrayList();
b.Add(Convert.ToString(Session["RegName"]));
b.Add(Convert.ToInt32(Request["ID"]));
b.Add(Convert.ToInt32(ds.Tables[0].Rows[0][3].ToString()) + 1);
myTemp.upd(b);
}

dtBind();
}

private void dtBind()
{
Db.Temp myTemp = new Db.Temp();
DataSet ds1 = myTemp.dsSelWhereRegName(Convert.ToString(Session["RegName"]));
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID",typeof(Int32)));
dt.Columns.Add(new DataColumn("Sort",typeof(string)));
dt.Columns.Add(new DataColumn("ProductID",typeof(Int32)));
dt.Columns.Add(new DataColumn("ProductName",typeof(string)));
dt.Columns.Add(new DataColumn("RtailPrice",typeof(string)));
dt.Columns.Add(new DataColumn("Price",typeof(Int32)));
dt.Columns.Add(new DataColumn("ProductNum",typeof(Int32)));
dt.Columns.Add(new DataColumn("Xiaoji",typeof(Int32)));
Session["Num"] = "0";
for(int i=0;i<ds1.Tables[0].Rows.Count;i++)
{
DataRow r = dt.NewRow();
r[0] = Convert.ToInt32(ds1.Tables[0].Rows[i]["ID"]);
r[1] = ds1.Tables[0].Rows[i]["Sort"].ToString();
r[2] = Convert.ToInt32(ds1.Tables[0].Rows[i]["ProductID"]);
Db.Products myProducts = new Db.Products();
SqlDataReader dr3 = myProducts.drWhereID(Convert.ToInt32(ds1.Tables[0].Rows[i]["ProductID"]));
if(dr3.Read())
{
r[3] = dr3.GetString(1);
}
r[4] = Session["RetailPrice"].ToString();
r[5] = Convert.ToInt32(Session["RetailPrice"].ToString());
r[6] = Convert.ToInt32(ds1.Tables[0].Rows[i]["ProductNum"]);
r[7] = Convert.ToInt32(Session["RetailPrice"].ToString())*Convert.ToInt32(ds1.Tables[0].Rows[i]["ProductNum"].ToString());
dt.Rows.Add(r);
Session["Num"] = Convert.ToString(Convert.ToInt32(Session["Num"]) + Convert.ToInt32(Session["RetailPrice"].ToString())*Convert.ToInt32(ds1.Tables[0].Rows[i]["ProductNum"].ToString()));
}

lblNum.Text = Session["Num"].ToString();

dgGouwuche.DataSource = dt.DefaultView;
dgGouwuche.DataBind();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgGouwuche.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgGouwuche_CancelCommand);
this.dgGouwuche.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgGouwuche_EditCommand);
this.dgGouwuche.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgGouwuche_UpdateCommand);
this.dgGouwuche.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgGouwuche_DeleteCommand);
this.dgGouwuche.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgGouwuche_ItemDataBound);
this.dgGouwuche.SelectedIndexChanged += new System.EventHandler(this.dgGouwuche_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dgGouwuche_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgGouwuche.EditItemIndex = -1;
dtBind();
}

private void dgGouwuche_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgGouwuche.EditItemIndex = e.Item.ItemIndex;
dtBind();
}

private void dgGouwuche_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Db.Temp myTemp = new Db.Temp();
myTemp.updProductNum(Convert.ToInt32(((TextBox)dgGouwuche.Items[e.Item.ItemIndex].Cells[5].Controls[0]).Text),Convert.ToInt32(dgGouwuche.DataKeys[e.Item.ItemIndex]));
Response.Write("<script>alert('修改成功!')</script>");
dgGouwuche.EditItemIndex = -1;
dtBind();
}

private void dgGouwuche_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Db.Temp myTemp = new Db.Temp();
DataSet ds1 = myTemp.dsSelWhereRegName(Convert.ToString(Session["RegName"]));
myTemp.del(Convert.ToInt32(dgGouwuche.DataKeys[e.Item.ItemIndex]));
Response.Write("<script>alert('删除成功!')</script>");
dgGouwuche.EditItemIndex = -1;
dtBind();
}

public static string Encrypt(string password) //密码加密程序,.text中也提供有的
{
// Force the string to lower case
//
password = password.ToLower();

Byte[] clearBytes = new UnicodeEncoding().GetBytes(password);
Byte[] hashedBytes = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);

return BitConverter.ToString(hashedBytes);
}

private void dgGouwuche_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemIndex<0) return;
e.Item.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='EFEBEF'");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor");

for(int i=0;i<this.dgGouwuche.Items.Count;i++)
{
this.dgGouwuche.Items[i].Cells[7].Attributes.Add("onclick","return confirm('确定删除该记录?')");
}
}

private void dgGouwuche_SelectedIndexChanged(object sender, System.EventArgs e)
{

}
}
}
Tony088 2006-08-15
  • 打赏
  • 举报
回复
非常感谢。。。。这位仁兄

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧