asp.net未处理的异常 [英] asp.net unhandled exception

查看:82
本文介绍了asp.net未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在购物车中添加项目的代码,它添加了所有项目,但问题是它不会只添加一个项目我找不到异常但仍然捕获块执行。请帮我解决这个问题。我的代码如下:



This is code to add items in the cart it add all the items but problem is it doesn't add only one item i couldn't find out the exception but still catch block executes.Please Help me to solve this.My code as follows:

try
      {
          if (e.CommandName == "Cart" && Session.Count > 0)
          {
              da = new SqlDataAdapter("select count(*) from rsa_addtocart tatc inner join rsa_Users tl on tatc.UserId=tl.UserId where tatc.productid=" + Convert.ToInt32(e.CommandArgument.ToString()) + " and tl.UserId=" + Convert.ToInt32(Session["users"].ToString()) + " and tatc.UserId=" + Convert.ToInt32(Session["users"].ToString()) + " ", con);
              int n = Convert.ToInt32(da.SelectCommand.ExecuteScalar());
              if (n == 1)
              {
                  da = new SqlDataAdapter("select tpd.ProductID,tpd.ProductName,tpd.Price,tpd.ProductImage,tl.UserId,tatc.cartId from rsa_ProductItemTable tpd inner join rsa_addtocart tatc on tpd.ProductID=tatc.productid inner join rsa_Users tl on tl.UserId=tatc.UserId where tatc.productid= @CommandID and tl.UserId= @UID  ", con);
                  da.SelectCommand.Parameters.Add("@UID", Convert.ToInt32(Session["users"]));
                  da.SelectCommand.Parameters.Add("@CommandID", Convert.ToInt32(e.CommandArgument.ToString()));
                  ds = new DataSet();
                  da.Fill(ds, "tbl_tpd");
                  if (ds.Tables.Count > 0 && ds.Tables["tbl_tpd"].Rows.Count > 0)
                  {
                      da = new SqlDataAdapter("update rsa_addtocart set UserId=@UId,productid=@productid,productname=@prodname,ProductImage=@prodImage,price=@price,cdate=getdate() where productid=@productid and cartId=@cartId and UserId=@UId ", con);
                      da.SelectCommand.Parameters.AddWithValue("@UId", Convert.ToInt32(Session["users"].ToString()));
                      da.SelectCommand.Parameters.AddWithValue("@productid", Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()));
                      da.SelectCommand.Parameters.AddWithValue("@prodname", ds.Tables[0].Rows[0][1].ToString());
                      da.SelectCommand.Parameters.AddWithValue("@prodImage", ds.Tables[0].Rows[0][3]);
                      da.SelectCommand.Parameters.AddWithValue("@price", Convert.ToDecimal(ds.Tables[0].Rows[0][2].ToString()));
                      da.SelectCommand.Parameters.AddWithValue("@cartId", Convert.ToInt32(ds.Tables[0].Rows[0][5].ToString()));
                      da.SelectCommand.ExecuteNonQuery();
                  }
              }
              da = new SqlDataAdapter("select tpd.ProductID,tpd.ProductName,tpd.Price,tpd.ProductImage from rsa_ProductItemTable tpd where tpd.ProductID=@commandId", con);
              da.SelectCommand.Parameters.AddWithValue("@commandId", Convert.ToInt32(e.CommandArgument.ToString()));
              ds = new DataSet();
              da.Fill(ds, "tbl_tpd");
              if (ds.Tables.Count > 0 && ds.Tables["tbl_tpd"].Rows.Count > 0)
              {
                  using (var command = new SqlCommand("rsa_products_sp_Insertaddcart", con))
                  {
                      int value = 1;
                      command.CommandType = CommandType.StoredProcedure;
                      command.Parameters.AddWithValue("@UserId", Session["users"]);
                      command.Parameters.AddWithValue("@productid", ds.Tables[0].Rows[0][0]);
                      command.Parameters.AddWithValue("@productname", ds.Tables[0].Rows[0][1]);
                      command.Parameters.AddWithValue("@ProductImage", ds.Tables[0].Rows[0][3]);
                      command.Parameters.AddWithValue("@price", ds.Tables[0].Rows[0][2]);
                      command.Parameters.AddWithValue("@qty", value);
                      command.Parameters.AddWithValue("@totalcost", ds.Tables[0].Rows[0][2]);

                      if (con.State != ConnectionState.Open)
                      {
                          con.Open();
                          try
                          {
                              command.ExecuteNonQuery();
                          }
                          finally
                          {
                              con.Close();
                          }
                      }
                      else
                      {
                          command.ExecuteNonQuery();
                      }
                  }

                  if (Session.Count > 0)
                  {
                      Response.Redirect("AddCart.aspx?ProductID=" + e.CommandArgument.ToString());
                  }

          else
          {
              Response.Redirect("UserLogin.aspx");
          }
          }
          }
      }
      catch (Exception ex)
      {
Response.Write("<script>alert('"+ Server.HtmlEncode(ex.Message) + "')</script>");
      }
      finally
      {
          con.Close();
      }



事先提前。


Thnks in advance.

推荐答案

在内部尝试你没有可能应该是一个catch块。



外部异常是什么,你根本不调查异常。尝试在

In the inner try you don't have a catch block at all, probably should be.

What comes to the outer exception, you don't investigate the exception at all. Try showing the message of the exception in
...
catch (Exception ex)
{
   // Show ex.Message over here
}
...


这篇关于asp.net未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆