Reputation: 196
I have a view model class Paymentcart
and inside that I have created object of another two classes payment
and cart
.
Class Paymentcart
namespace Temple.Models
{
public class paymentcart
{
public cart cart { get; set; }
public payment payment { get; set; }
}
}
Class cart:
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; }
}
Class payment:
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; }
}
But when I am putting value from the database, it shows "Null Exception" error (but the database does return values).
This is the controller code:
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;
I have attached a screenshot here: Error Page
I don't know am using the correct method; please help me to solve this.
Upvotes: 1
Views: 111
Reputation: 121
Please try below code:
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;
}
Upvotes: 0
Reputation: 16049
You created instance of paymentcart
, that is correct but you missed to create instance of
cart
class, while instantiating paymentcart
class object, create object of cart
class
something like
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();
Bonus : Always follow naming conventions to declare class name, property name and variable names
Upvotes: 2