1

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.

codeseeker
  • 196
  • 1
  • 13
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Joelius Jul 06 '19 at 08:57
  • @Joelius ,I have checked this ,bt cant find the answer through that question,I feel different – codeseeker Jul 06 '19 at 09:47

2 Answers2

2

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

  • Your class name should start with Capital letter i.e. PascalCase
  • Your variable name should start with small letter i.e. camelCase
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • Thnq sir,Bu t while displaying this values using a forloop ,It retuns only the last value in the list – codeseeker Jul 06 '19 at 11:25
  • You need to add each record to the list of PaymentCart i.e. `qList.Add(q);` – Prasad Telkikar Jul 06 '19 at 11:50
  • i didn't mean this scenario sir .This works fine,But when am displaying the data it returns last value only – codeseeker Jul 06 '19 at 11:53
  • Can you add another question where you will mention scenario which explain your implementation of display function, because your current question does not contain any information related to view – Prasad Telkikar Jul 06 '19 at 12:02
  • even though i have put this code inside while loop,It overwrites .How to overcome this?please help me – codeseeker Jul 06 '19 at 13:42
  • I can't help you here as I need to debug full code and need to check what is going wrong in your code, If you have any git repository which I can look into then provide. Sorry I can't help here just looking into your question why this code is not working – Prasad Telkikar Jul 06 '19 at 13:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196077/discussion-between-prasad-telkikar-and-codeseeker). – Prasad Telkikar Jul 06 '19 at 14:38
0

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;
        }
dev
  • 121
  • 1
  • 4
  • 14