我有一个视图模型类Paymentcart
,在其中创建了另外两个类payment
和cart
的对象。
付款车类
namespace Temple.Models
{
public class paymentcart
{
public cart cart { get; set; }
public payment payment { get; set; }
}
}
购物车类:
public class cart
{
public long cid { get; set; }
public long vid { get; set; }
public long userid { get; set; }
public long count { get; set; }
public long tempid { get; set; }
public string name { get; set; }
public string star { get; set; }
public string dates { get; set; }
public string vname { get; set; }
public string vrate { get; set; }
public string totalamount { get; set; }
public int rows { get; set; }
}
类别付款:
public class payment
{
public long cid { get; set; }
public long vid { get; set; }
public long userid { get; set; }
public long tempid { get; set; }
public string amt { get; set; }
public string cname { get; set; }
public long number { get; set; }
public long securitycode { get; set; }
public string expdate { get; set; }
public string totalamount { get; set; }
}
但是,当我从数据库中输入值时,它显示“ Null Exception”错误(但数据库确实会返回值)。
这是控制器代码:
List<paymentcart> qlist = new List<paymentcart>();
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("getcart", con))
{
cmd.CommandType = CommandType.StoredProcedure;
var q = new paymentcart();
q.cart = new cart();
cmd.Parameters.AddWithValue("@uid", uid);
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
q.cart.tempid = Convert.ToInt64(rd["TdId"]);
q.cart.userid = Convert.ToInt32(rd["UserID"]);
q.cart.cid = Convert.ToInt32(rd["cid"]);
q.cart. vid = Convert.ToInt32(rd["v_id"]);
q.cart.name = Convert.ToString(rd["name"]);
q.cart. star = Convert.ToString(rd["star"]);
q.cart. dates = Convert.ToString(rd["dates"]);
q.cart.vname = Convert.ToString(rd["vname"]);
q.cart. vrate = Convert.ToString(rd["vrate"]);
qlist.Add(q);
}
}
}
return qlist;
我在此处附有截图:Error Page
我不知道使用的是正确的方法;请帮我解决这个问题。
答案 0 :(得分:2)
您创建了paymentcart
的实例,这是正确的,但是您错过了创建的实例
cart
类,在实例化paymentcart
类对象的同时,创建cart
类的对象
类似
var q = new paymentcart();
q.cart = new Cart(); //This will resolve error related to object instantiation
//^^^ This was missing, I used naming conventions to differentiate between class name and object name
//Your code goes here
cmd.Parameters.AddWithValue("@uid", uid);
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
奖金:始终遵循naming conventions声明类名,属性名和变量名
答案 1 :(得分:0)
请尝试以下代码:
public List<paymentcart> ViewCart(int uid)
{
List<paymentcart> qlist = new List<paymentcart>();
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("getcart", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@uid", uid);
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
var q = new paymentcart();
q.cart = new cart();
q.cart.tempid = Convert.ToInt64(rd["TdId"]);
q.cart.userid = Convert.ToInt32(rd["UserID"]);
q.cart.cid = Convert.ToInt32(rd["cid"]);
q.cart. vid = Convert.ToInt32(rd["v_id"]);
q.cart.name = Convert.ToString(rd["name"]);
q.cart. star = Convert.ToString(rd["star"]);
q.cart. dates = Convert.ToString(rd["dates"]);
q.cart.vname = Convert.ToString(rd["vname"]);
q.cart. vrate = Convert.ToString(rd["vrate"]);
qlist.Add(q);
}
}
}
return qlist;
}